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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by