eulers improved method code error
Mostra commenti meno recenti
tried to solve ode using eulers improved method for a function of F=2xy
with step size h=0.1,y0=1,x0=1 and seeking y(1.5)
The code is as follows
function yout=improvedeuler3(F,x0,h,xfinal,y0);
y=y0;
yout=y;
for x=x0:h:xfinal-h;
s1=F(x,y);
s2=F(x+h/2,y+h*s1/2); %% is it correct for modified euler
y=y+h*s2;
yout=[yout,y];
end
end
initial conditions and conecting script is
F=@(x,y)2*x*y;
x0=1;
h=0.1;
xfinal=1.5;
y0=1;
yout=improvedeuler(F,x0,h,xfinal,y0);
%comparision with exact
x=x0:h:xfinal;
y_exact=exp((x.^2)-1);
plot(x,yout,'r',x,y_exact,'k'),legend('Improved','Exact');
I am getting a matlab output of y values different from that calculated manually
It seems there is an error in the code,unable to makeout.
1 1.2310 1.5452 1.9779 2.5814 3.4348 ------ matlaboutput
1 1.2320 1.5479 1.9832 2.5908 3.4509 ------ manual calculation
3 Commenti
darova
il 14 Ott 2019
Try to reduce step and report if something changes
Syed Haque
il 15 Ott 2019
darova
il 15 Ott 2019
Looks good as for me. What do you think?

Risposta accettata
Più risposte (1)
Syed Haque
il 16 Ott 2019
3 Commenti
James Tursa
il 16 Ott 2019
Same for me. When I put this code in a file called improveuler.m
function yout=improveuler(F,x0,h,xfinal,y0);
y=y0;
yout=y;
for x=x0:h:xfinal-h;
s1=F(x,y);
s2=F(x+h,y+h*s1);
y=y+h*(s1+s2)/2;
yout=[yout,y];
end
end
and this code in a separate file called improveuler_test.m
F=@(x,y)2*x*y;
x0=1;
h=0.1;
xfinal=1.5;
y0=1;
yout=improveuler(F,x0,h,xfinal,y0);
%comparision with exact
x=x0:h:xfinal;
y_exact=exp((x.^2)-1);
plot(x,yout,'r',x,y_exact,'k'),legend('Improved','Exact');
and then type the following at the command line
improveuler_test
I get it to run without errors and produce what looks like a reasonable plot.
Syed Haque
il 17 Ott 2019
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!