how can i get an improved Euler's method code for this function?
显示 更早的评论
dy = @(x,y).2*x*y;
f = @(x).2*exp(x^2/2);
x0=1;
xn=1.5;
y=1;
h=0.1;
fprintf ('x \t \t y (euler)\t y(analytical) \n') % data table header
fprintf ('%f \t %f\t %f\n' ,x0,y,f(x0));
for x = x0 : h: xn-h
y = y + dy(x,y)*h;
x = x + h ;
fprintf (
'%f \t %f\t %f\n' ,x,y,f(x));
end
2 个评论
FastCar
2018-12-16
Euler has its limit to solve differential equations. You can change the integration step going towards the optimum step that is given by the minimum of the sum of the truncation error and step error, but you cannot improve further. What do you mean by improve?
Ibrahem abdelghany ghorab
2018-12-17
采纳的回答
更多回答(1 个)
James Tursa
2018-12-17
编辑:James Tursa
2018-12-17
The "Modified" Euler's Method is usually referring to the 2nd order scheme where you average the current and next step derivative in order to predict the next point. E.g.,
dy1 = dy(x,y); % derivative at this time point
dy2 = dy(x+h,y+h*dy1); % derivative at next time point from the normal Euler prediction
y = y + h * (dy1 + dy2) / 2; % average the two derivatives for the Modified Euler step
See this link:
4 个评论
Ibrahem abdelghany ghorab
2018-12-18
James Tursa
2018-12-18
编辑:James Tursa
2018-12-18
Not sure what you are asking. The loop is simply
for x = x0 : h: xn-h
dy1 = dy(x,y); % derivative at this time point
dy2 = dy(x+h,y+h*dy1); % derivative at next time point from the normal Euler prediction
y = y + h * (dy1 + dy2) / 2; % average the two derivatives for the Modified Euler step
fprintf ('%f \t %f\t %f\n' ,x+h,y,f(x+h));
end
Note that inside the fprintf I have used x+h, since that is the x value associated with the newly calculated y value.
Ibrahem abdelghany ghorab
2018-12-18
Santiago Cerón
2020-11-12
James, how do you graph that in a plot?
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!