Dear all;

Please I need your help to find the error in my code.

I want to solve an ode, with condition on step size.

ode := diff(y(x), x) = 2*x+y(x);

f:=(x,y)->2*x-y;

analyticsol := rhs(dsolve({ode, y(0) = 1}));

RKadaptivestepsize := proc (f, a, b, epsilon, N)

local x, y, n, h,k,z,R,p;

p:=2;

h := evalf(b-a)/N; ## we begin with this setpsize

x[0] := a; y[0] := 1; ## Initialisation

for n from 0 to N-1 do ##loop

x[n+1] := a+(n+1)*h; ## noeuds

k[1] := f(x[n], y[n]);

k[2] := f(x[n]+h, y[n]+h*k[1]);

k[3] := f(x[n]+h/2, y[n]+h/4*(k[1]+k[2]));

z[n+1] := z[n]+(h/2)*(k[1]+k[2]);## 2-stage runge Kutta.

y[n+1] := y[n]+(h/6)*(k[1]+k[2]+4*k[3]);

R:=abs(y[n+1]-z[n+1]); ## local erreur

hstar:=sqrt(epsilon/R)

if R=<=epsilon then

x[n] := x[n+1]+h;

y[n]:=y[n+1];

n:=n+1;

else

h:=hstar;

end if

end do;

[seq([x[n], y[n]], n = 0 .. N)];

[seq([x[n], z[n]], n = 0 .. N)];

end proc:

epsilon:=1e-8;

RKadaptivestepsize((x,y)->2*x-y,0,1, epsilon,20)