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

조회 수: 4 (최근 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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by