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

darova
darova 2019년 10월 14일
Try to reduce step and report if something changes
Syed Haque
Syed Haque 2019년 10월 15일
1 1.205 1.4754 1.8352 2.3193 2.9776 with h=0.09
1 1.231 1.5453 1.978 2.5814 3.4348 with h=0.10
1 1.2576 1.6193 2.1345 2.88 with h=0.11 (y1.5 with this step is missing)
something wrong with the code
darova
darova 2019년 10월 15일
Looks good as for me. What do you think?
img1.png

댓글을 달려면 로그인하십시오.

 채택된 답변

James Tursa
James Tursa 2019년 10월 15일
편집: James Tursa 2019년 10월 15일

1 개 추천

s2=F(x+h/2,y+h*s1/2); %% is it correct for modified euler
If "Improved" Euler's Method means "Modified" Euler's Method as your comment indicates, then no it is not correct.
Modified Euler is basically finding the derivative at the current point, finding the derivative one full step ahead of the current point, and then using the average of the two derivatives to make your integration step. So this:
s1=F(x,y);
s2=F(x+h/2,y+h*s1/2); %% is it correct for modified euler
y=y+h*s2;
should look something like this instead (inserting spaces for reading clarity):
s1 = F(x , y ); % derivative at current point
s2 = F(x+h, y+h*s1); % derivative at one full step ahead (using predicted point from standard Euler Method)
y = y + h*(s1 + s2)/2; % integrate forward using the average of the two derivatives
Did you really mean to use the "Midpoint" Method? What is the actual assignment you were given?

댓글 수: 2

Syed Haque
Syed Haque 2019년 10월 15일
Getting an error,assignment is to calculate y1.5 and compare error at all points upto 1.5 with actual.
James Tursa
James Tursa 2019년 10월 15일
Can you post your current code and the assignment text? Many contributors here (myself included) are not willing to open active documents like Excel or Word etc.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Syed Haque
Syed Haque 2019년 10월 16일

0 개 추천

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
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');
% COMMAND WINDOW ERROR PROMPT
>> improveuler
Not enough input arguments.
Error in improveuler (line 2)
y=y0;
develop improved euler method to solve y1.5 given y'=2xy, y1=1

댓글 수: 3

Daniel M
Daniel M 2019년 10월 16일
편집: Daniel M 2019년 10월 16일
This should not be posted as an answer. And I did not receive an error when running this code.
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
Syed Haque 2019년 10월 17일
Thanks James,could finally achieve the end result,y values are matching with calculated ones.seems all those multiple files of the same assignment was playing havoc,deleted them,pasted the script again from the forum(debugging cropped up-shouldn't have done that) and finally after three/four hits I could see the code run successfully.
Also sorry for posting earlier comment as an answer.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

질문:

2019년 10월 14일

댓글:

2019년 10월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by