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
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 Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!