how can i solve second order ODE with RK-4 without using a built in function in matlab?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mohammad Adeeb
am 14 Jun. 2021
Kommentiert: Mohammad Adeeb
am 14 Jun. 2021
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?
2 Kommentare
Akzeptierte Antwort
Alan Stevens
am 14 Jun. 2021
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!