how can i get an improved Euler's method code for this function?

22 ビュー (過去 30 日間)
Ibrahem abdelghany ghorab
Ibrahem abdelghany ghorab 2018 年 12 月 15 日
コメント済み: Santiago Cerón 2020 年 11 月 12 日
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
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
Ibrahem abdelghany ghorab 2018 年 12 月 17 日
modified method
and i what 4Runge-kutta for this function dy = @(x,y).2*x*y;

サインインしてコメントする。

採用された回答

Are Mjaavatten
Are Mjaavatten 2018 年 12 月 17 日
There are two problems with your code:
  • The analytical solution is incorrect
  • You increment x inside the for loop. Don't. The for loop does this automatically.
Here is a corrected version:
a = 0.2;
y0 = 1;
x0 = 1;
xn = 1.5;
h = 0.1;
dy = @(x,y)a*x*y; % dy/dx
f = @(x) y0*exp(a/2*(x.^2-1)); % Correct analytic solution
y = y0;
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 : h: xn
y = y + dy(x,y)*h;
fprintf ('%f \t %f\t %f\n' ,x,y,f(x));
end
Choose a smaller step length h to for better accuracy. Alternatively try a higher order method like Runge-Kutta.
  1 件のコメント
Ibrahem abdelghany ghorab
Ibrahem abdelghany ghorab 2018 年 12 月 17 日
編集済み: Ibrahem abdelghany ghorab 2018 年 12 月 17 日
modified orImprovedEuler method
and i what 4Runge-kutta for this function dy = @(x,y).2*x*y;

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
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
Ibrahem abdelghany ghorab 2018 年 12 月 18 日
thank you very much
Santiago Cerón
Santiago Cerón 2020 年 11 月 12 日
James, how do you graph that in a plot?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by