runge kutta vs ode

1 回表示 (過去 30 日間)
Jhon
Jhon 2018 年 10 月 24 日
コメント済み: Jhon 2018 年 10 月 24 日
Hello,I need to tell what is the difference between ode23 and runge kutta using this equation:sin(x) + cos(y).I have written this code:
h=0.1;
xfinal=55;
x(1)=0;
y(1)=5;
f=@(x,y) sin(x) + cos(y);
for i= 1:ceil(xfinal/h)
x(i+1)=x(i)+h;
k1=f(x(i) , y(i) );
k2=f(x(i) + 0.5*h,y(i)+0.5*k1*h);
k3=f(x(i) + 0.5*h,y(i)+0.5*k2*h);
k4=f(x(i)+ h, y(i) + k3*h );
y(i+1)=y(i) + h/6*(k1+ 2*k2 + 2*k3 +k4);
end
plot(x,y);
hold on
[t,x] = ode23(f,[0 55] , 1)
plot(t,x)
And I got this plot :
I only spot a difference at the start,is this plot correct or did I just messed it up?

採用された回答

Jim Riggs
Jim Riggs 2018 年 10 月 24 日
編集済み: Jim Riggs 2018 年 10 月 24 日
You are very close. In your Runge-Kuta solution, you specified Y0 = 5,
Y(1)=5;
but in your ode23 solution, you specified Y0 = 1.
[t,x] = ode23(f, [0,55], 1)
If you set these to the same value, you should get the same result for both methods. E.g, try
[t,x] = ode23(f, [0,55], 5);
  1 件のコメント
Jhon
Jhon 2018 年 10 月 24 日
Ahh,yes ty idk how I missed that

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by