# Thread: Maple- Assigning values inside 'proc'

1. ## 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

2. Originally Posted by paskari
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

3. 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.

4. Originally Posted by paskari
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

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.

6. 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.