> # The following procedure uses Euler's method to compute > # approximate solutions to Initial Value Problems of the form > # d(y(x),x)=f(x,y) y(xo)=yo > # Meaning of the variables: > # f function in two varaibles > # xo initial x-value > # yo initial y-value ( y(xo)=yo ) > # x1 last x-value, > # h stepsize (the smaller the more precise) > # !!!!! NOTE: Use Shift-Return to start new line , this way all !!!! > # !!!!! commands will be executed at the same time !!!! > Euler:=proc(f,xo,yo,x1,h) # means Euler is a procedure using f,xo,yo,x1,h as entries > local x,y,hh,p,e,yk; # local variables, will not leave the procedure > global output,y1; # the output will be the list y(xo),y(xo+h),y(xo+2h),.... > y:=yo; # initialization > hh:=h; > e:=NULL; > for x from xo by hh to x1 do # Start of loop in which y(xo),y(xo+h),y(xo+2h),.... > p:=[x,y]; # will be computed iteratively > e:=e,p; > yk:=y; > y:=evalhf(y+hh*f(x,y)); # using linear approximation to comput next y-value > od; # end of loop > output:=[e]; > y1:=yk; > end: > # Lets use that procedure > g:=(x,y)->x+cos(y); > Euler(g,0,1,3,.1); > y1; > output; > with(plots); > display([plot(output,0..3,0..5)]);