Confusion on Runge-Kutta method

1 view (last 30 days)
Jesse
Jesse on 25 Nov 2013
Answered: Meysam Mahooti on 5 May 2021
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!

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 25 Nov 2013
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 Comments
Jesse
Jesse on 10 Dec 2013
Titus, I did fix this, and thank you for all of your help!
Steve Chuks
Steve Chuks on 6 Mar 2015
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?

Sign in to comment.

More Answers (1)

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

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!