how can i solve second order ODE with RK-4 without using a built in function in matlab?

1 回表示 (過去 30 日間)
this is the equation :
(d^2 y)/(dt^2 ) + 3 (dy)/dt - 30t - 10 = 0
this is my code :
h=0.01; %step size (changable according to the proplem)
t=0:h:1; %the tdomain range
y = [1;-3]; %intial condition for first equation in form of matrix (changable according to the proplem)
for i = 1:length(t)-1
K11 = RK4(t(i), y1(:, i));
K12 = RK4(t(i) + h/2, y(:, i) + h*K11/2);
K13 = RK4(t(i) + h/2, y(:, i) + h*K12/2);
K14 = RK4(t(i) + h, y(:, i) + h*K13);
y(:, i+1) = y(:, i) + h/6*(K11 + 2*K12 + 2*K13 + K14);
end
function dy1 = RK4(t, y)
f11 = y(2);
f12 = -3*y(1)+30*t+10;
dy1 = [f11;f12];
%change the matrix due to your intital conditins
%and the equation in your proplem
end
is this right?

採用された回答

Alan Stevens
Alan Stevens 2021 年 6 月 14 日
This
K11 = RK4(t(i), y1(:, i));
should be |
K11 = RK4(t(i), y(:, i));
and this
f12 = -3*y(1)+30*t+10;
should be |
f12 = -3*y(2)+30*t+10;
  5 件のコメント
Alan Stevens
Alan Stevens 2021 年 6 月 14 日
Look at your original equation. It doesn't contain y on its own.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by