Results 1 to 6 of 6

Math Help - Maple- Assigning values inside 'proc'

  1. #1
    Newbie
    Joined
    Nov 2008
    Posts
    5

    Maple- Assigning values inside 'proc'

    Hello I'm pretty new to maple. I used it 6 years ago, but, well I'm a little rusty

    Essentially I'm trying to plot a multivariable differential equation (Izhikevich model) but first I want to get my head around the 'proc' command

    so I set up the below proc command which is essentially f:=x->x^2

    parabola:=proc(x)
    x^2;
    end;

    I call
    plot(parabola,-10..10)
    and it works like a charm

    now if I do the following it should have absolutely no affect on the graph, but it raises holy hell

    y=1234;

    parabola:=proc(x)
    global y;
    y:=y+5678
    x^2;
    end;

    this time when I call
    plot(parabola,-10..10)
    the graph looks all wierd, as if the assignment to y is returning a value which is interfereing with the x^2

    Does anyone know what the problem is
    thanx
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Rhymes with Orange Chris L T521's Avatar
    Joined
    May 2008
    From
    Chicago, IL
    Posts
    2,844
    Thanks
    3
    Quote Originally Posted by paskari View Post
    Hello I'm pretty new to maple. I used it 6 years ago, but, well I'm a little rusty

    Essentially I'm trying to plot a multivariable differential equation (Izhikevich model) but first I want to get my head around the 'proc' command

    so I set up the below proc command which is essentially f:=x->x^2

    parabola:=proc(x)
    x^2;
    end;

    I call
    plot(parabola,-10..10)
    and it works like a charm

    now if I do the following it should have absolutely no affect on the graph, but it raises holy hell

    y=1234;

    parabola:=proc(x)
    global y;
    y:=y+5678
    x^2;
    end;

    this time when I call
    plot(parabola,-10..10)
    the graph looks all wierd, as if the assignment to y is returning a value which is interfereing with the x^2

    Does anyone know what the problem is
    thanx
    Try restarting. I just typed in what you have and it worked for me.

    Code:
    > restart;
    > parabola := proc(x)
          x^2;
      end:
    > plot(parabola,-10..10);


    Code:
    > y := 1234:
    > parabola := proc(x)
          global y;
          y := y + 5678;
          x^2;
      end:
    > plot(parabola,-10..10);


    --Chris
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Nov 2008
    Posts
    5
    That's strange, it worked for me as well, to a certain degree

    But there is some strange behaviour associated with 'proc'. For example if you type in the following

    >y:=2;
    >parabola:=proc(x)
    global y;
    print("x is",x);
    x^y;
    end;
    >plot(parabola,-5..5);

    I'm expecting something like
    x is -5
    x is -4
    x is -3
    ...

    but instead I get much more than the 11 calls to the function, and it's in the form of

    x is -4.972378378373...
    x is -4.596886676334...
    x is-4.3234324234323...

    This is a problem if the procedure has any side effects, like if a global variable is being updated.

    I think what's happening is that the plot function keeps calling the procedure to increase the accuracy of the graph, but with sheer disregard for side effects.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Rhymes with Orange Chris L T521's Avatar
    Joined
    May 2008
    From
    Chicago, IL
    Posts
    2,844
    Thanks
    3
    Quote Originally Posted by paskari View Post
    That's strange, it worked for me as well, to a certain degree

    But there is some strange behaviour associated with 'proc'. For example if you type in the following

    >y:=2;
    >parabola:=proc(x)
    global y;
    print("x is",x);
    x^y;
    end;
    >plot(parabola,-5..5);

    I'm expecting something like
    x is -5
    x is -4
    x is -3
    ...

    but instead I get much more than the 11 calls to the function, and it's in the form of

    x is -4.972378378373...
    x is -4.596886676334...
    x is-4.3234324234323...

    This is a problem if the procedure has any side effects, like if a global variable is being updated.

    I think what's happening is that the plot function keeps calling the procedure to increase the accuracy of the graph, but with sheer disregard for side effects.
    Try this:

    Code:
    > y := 2:
    > parabola := proc( x :: integer ) # x can only be
                                       # an integer.
        global y;
        print("x is", x," and ", x ," to the ", y," is ",x^y);
     end:
    > seq(parabola(x),x=-5..5);
    and see if this is what you want!

    --Chris
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Nov 2008
    Posts
    5
    Hey, I'll try that solution, but I already tried a similar approach, namely using "if type(x,integer) then..." but what happened was that if I called it like so

    plot(parabola,-5..5)

    then the output would be

    x is -5
    x is 5

    it would completely ignore the intermediate values

    I changed it such that I ran a for loop which updates an array of points, then I print the points.

    To its defense, I don't think maple would be able to draw such perfect graphs if it were restricted to only using integers, furthermore, for reasons of performance and memory management, I doubt maple could get away with running a program, then removing all of its side effects

    I guess we can add Maple to the long line of programming languages which de facto require you to understand all of their intracicies.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Newbie
    Joined
    Nov 2008
    Posts
    5
    So I tried the solution you gave me, but it suffers from the same problem as previously, namely that plot doesn't just deal with integers.

    I think plot expects a function with zero side effects, such that it is free to sample any parameters it wishes.

    The function I'm trying to implement, the Izhikevich model, inherently codes for millisecond precision. So the xaxis is time in milliseconds, however, the function must be called every millisecond, on the millisecond. And I just don't think plot is capable of dealing with such a function.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 0
    Last Post: November 30th 2011, 02:25 PM
  2. SAS code Proc Mixed
    Posted in the Math Software Forum
    Replies: 0
    Last Post: April 17th 2011, 12:08 AM
  3. assigning particular value based on x asis
    Posted in the Math Software Forum
    Replies: 5
    Last Post: June 11th 2010, 02:55 AM
  4. Proc mixed random: standard deviation equals 0
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: April 12th 2010, 01:45 PM
  5. Probability of assigning people to things
    Posted in the Statistics Forum
    Replies: 3
    Last Post: January 6th 2010, 08:55 AM

Search Tags


/mathhelpforum @mathhelpforum