Confusion on Runge-Kutta method

조회 수: 6 (최근 30일)
Jesse
Jesse 2013년 11월 25일
답변: Meysam Mahooti 2021년 5월 5일
Greetings,
I am trying to implement a 4th order Runge Kutta for the following equations on the interval 1 less than or equal to t less than or equal to 2, with h = 0.01:
x' = x^-2 + log y + t^2
y' = e^y-cos(x) + (sin t)x - 1/x^3y^3
with x(2)=-2 and y(2) = 1.
Now, I started to implement it in code is a basic form:
function rungekutta
h = 0.01;
t = 1;
w = 1;
fprintf('Step 0: t = %12.8f, w = %12.8f\n', t, w);
for i = 1:100
k1 = h*f(t,w);
k2 = h*f(t+h/2, w+k1/2);
k3 = h*f(t+h/2, w+k2/2);
k4 = h*f(t+h, w+k3);
w = w + (k1+2*k2+2*k3+k4)/6;
t = t+h;
fprintf('Step %d: t = %6.4f, w = %18.15f\n', i, t, w);
end
but the tricky part is that I'm dealing with x,y, and t in both equations, but in searches and books I see, all I see is a function dependent on x (or y) and t with an initial condition, not all three at once.
Any hints or suggestions? I'm a bit stuck in neutral.
Thanks!

채택된 답변

Titus Edelhofer
Titus Edelhofer 2013년 11월 25일
Hi,
your function f should have two inputs, namely t and X, where X is a two component vector [x, y]. Therefore your w=1 should in fact be w = [-2 1];
Titus
  댓글 수: 8
Jesse
Jesse 2013년 12월 10일
Titus, I did fix this, and thank you for all of your help!
Steve Chuks
Steve Chuks 2015년 3월 6일
hi Jesse... could you assist me? I've got a similar work as to solve ODE23s using Runge-Kutta method.
i tried running that code you've got and replaced it with an 'S' function but it exploded. was that what you did?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Meysam Mahooti
Meysam Mahooti 2021년 5월 5일
https://www.mathworks.com/matlabcentral/fileexchange/55430-runge-kutta-4th-order?s_tid=srchtitle

태그

Community Treasure Hunt

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

Start Hunting!

Translated by