Hi everyone!
I'm trying to solve a second order ode in MATLAB using the RK4 method. I know that I need to separate the second order into a system of first order odes, which i've already done. However, I'm not familiar with matlab and am unsure of how to approach the actual graphing part, although I do know how to do RK4 manually.
also, there are two initial conditions that are given.
From what I've read online, I need to write two m-files, one that defines the system of linear equations and another that actually establishes the RK4 method. Would the below work work?
function y = rk4step(f,t,x,h)
% f - Matlab inline function f(t,y)
% t - initial time
% x - initial condition
% h - stepsize
h2 = h / 2;
k1 = f(t, x);
k2 = f(t + h2, x + h2 * k1);
k3 = f(t + h2, x + h2 * k2);
k4 = f(t + h, x + h * k3);
y = x + (k1 + 2 * k2 + 2 * k3 + k4) * h / 6
and then in a separate m-file:
function z=ydot(x,y)
z(1)=y(2);
z(2)=(1-x.^2)/4*y(1);
my ode is y'' + (1-x^2)/4*y=0, y(0) = 1, y'(0) = 0 which i've split into
y' = Y
Y' = (1-x^2)/4*y
Where and how do I define rk4step?
once I finish everything how do I get it to plot? I need y and y' against x and y against y'. I also need to plot a phase portrait.
help is appreciated! thanks in advance!