Post Reply 
1st program with problems "Euler Method"
12-26-2019, 12:21 AM
Post: #1
1st program with problems "Euler Method"
I was told that an Euler's method program would be easy to make... On this forum? Somewhere... anyway that doesn't matter. I think it should be an easy program.

I'm getting an Invalid Input error and I think it has something to do with passing a function. I have this 'slopefield' app that someone wrote and I tried to pass the function the same way he did it... But it doesn't work for me in this instance.

By the way... I'm just learning about differentials AND programming. So maybe I'm doing something all wrong.

Obviously there would be a return or a print of s after the loop finishes. I didn't get that far in programming yet.

While previewing the post, I just had a thought... 'maybe I'm doing the matrix wrong.'

Code:

EXPORT eulerm(funct,X0,Y0,ch,times)
BEGIN
LOCAL a, s,check,x,y,myfunct;
myfunct:=EXPR(funct);
RETURN(myfunct(x,y));
MAKEMAT(0,times,2)▶s;
check=0;
FOR a FROM 0 TO times DO
IF check==0 THEN
s(a,0):=X0;
s(a,1):=Y0;
check:=1;
ELSE
s(a,0):=s(a-1,0)+ch;
x:=s(a,0);
y:=s(a-1,1);
s(a,1):=y+(myfunct)*ch; 
END;

END;

END;

sample input
Code:

eulerm("(3-y)*(y+1)",0,4,1,5)
Find all posts by this user
Quote this message in a reply
12-26-2019, 07:04 PM
Post: #2
RE: 1st program with problems "Euler Method"
For the first three lines of your program, I think this is what you want:

Code:
EXPORT eulerm(funct,X0,Y0,ch,times)
BEGIN
 LOCAL a, s,check,x,y;
 CAS(EVAL("myfunct(x,y):="+funct));
 RETURN(myfunct('x','y'));
END;

With those changes,

eulerm("(3-y)*(y+1)",0,4,1,5) will return (y+1)*(-y+3)

Nothing after the return statement will be executed.

-road
Find all posts by this user
Quote this message in a reply
12-26-2019, 08:44 PM
Post: #3
RE: 1st program with problems "Euler Method"
Quote:For the first three lines of your program, I think this is what you want:
Code:

Code:
EXPORT eulerm(funct,X0,Y0,ch,times)
BEGIN
 LOCAL a, s,check,x,y;
 CAS(EVAL("myfunct(x,y):="+funct));
 RETURN(myfunct('x','y'));
END;
With those changes,

eulerm("(3-y)*(y+1)",0,4,1,5) will return (y+1)*(-y+3)

Nothing after the return statement will be executed.

-road

Thank You roadrunner. I was able to get the program to run properly with your help. Then finding the debug softkey I was able to find out the matrices were incorrect too as they don't start with position [0,0].

The resulting display is good enough for me. But I would like to figure out a way to make it more user friendly. And maybe add in some functionality that allows the change to be negative. Something to tool around with in between quarters at school. The program operates fine on my physical device... But I'm getting a syntax error at s(a,2):=y+(f(x,y))*ch; right after 'f' on the PC emulated HP Prime. Like the variable isn't defined yet or something... i'm not sure.

Here is the proof of concept version of the Euler's Method program:
funct = the given equation
X0=initial x value
Y0=inital y value
ch=change in x
times = number of times the method is executed

Code:

EXPORT eulerm(funct,X0,Y0,ch,times)
BEGIN
LOCAL a, s,check,x,y;
CAS(EVAL("f(x,y):="+funct));
MAKEMAT(0,times,2)▶s[];
check=0;
FOR a FROM 1 TO times+1 DO
IF check==0 THEN
s(a,1):=X0;
s(a,2):=Y0;
check:=1;
ELSE
s(a,1):=s(a-1,1)+ch;
x:=s(a,1);
y:=s(a-1,2);
s(a,2):=y+(f(x,y))*ch; 
END;

END;
return s[];


END;
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)