Confusion on Runge-Kutta method
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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 个评论
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
2021-5-5
https://www.mathworks.com/matlabcentral/fileexchange/55430-runge-kutta-4th-order?s_tid=srchtitle
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!