# MATLAB

• Sep 24th 2008, 09:34 PM
Maccaman
MATLAB
Hello,

I need to solve a second order differential equation in Matlab.....which sucks because I HATE using Matlab.(Headbang)

Anyway, I cannot use the built-in matlab functions ode45, ode23s, ect, I have to employ the use of the fourth order Runge Kutta method......which I can do for a first order differential equation, but I cant think of how to use it for a 2nd order DE. I think that I first have to transform the 2nd order DE into a system of 1st order DE's.

Here is my matlab code for an IVP (1st order) using fourth order Runge Kutta method.

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

Should I transform the system of first order DE's into a vector and go from there?
• Sep 24th 2008, 10:54 PM
CaptainBlack
Quote:

Originally Posted by Maccaman
Hello,

I need to solve a second order differential equation in Matlab.....which sucks because I HATE using Matlab.(Headbang)

Anyway, I cannot use the built-in matlab functions ode45, ode23s, ect, I have to employ the use of the fourth order Runge Kutta method......which I can do for a first order differential equation, but I cant think of how to use it for a 2nd order DE. I think that I first have to transform the 2nd order DE into a system of 1st order DE's.

Here is my matlab code for an IVP (1st order) using fourth order Runge Kutta method.

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

Should I transform the system of first order DE's into a vector and go from there?

You convert your 2-nd order ODE into a first order system. So if you have an ODE for $x(t)$ , introduce the state vector:

$X(t)= {x(t) \brack x'(t)}$

Then:

$X'(t)={X_2(t) \brack f(t,x(t),x'(t))}$

where the ODE has been rewritten in the form:

$x''(t)=f(t,x(t),x'(t))$

RonL
• Sep 25th 2008, 12:53 PM
Maccaman
So if my 2nd order ode is

$x'' = \mu (1-x^2)x' -x$

then how is that in MATLAB? (Angry)

Is it something like

function z = xdot (t,x);
mu = 0.1;
z(1) = x(2);
z(2) = x(3);
z(3) = mu*(1-z(1)^2)*z(2) - z(1);
• Sep 25th 2008, 01:39 PM
Maccaman
I may as well just post this question and see if anyone can help. Im just so tired the information im getting for this question is just going in one eyeball and out the other.

The equation above is the Van de Pol equation. I need to have mu = 0.1 and use the fourth order runge kutta method with a step size of h=0.1 in the interval 0 < t< 200 using the initial conditions x(0) = 1, and x'(0) =0.
Then the initial conditions x(0) = 5, x'(0) = 0.
Then i have to plot x(t) vs. time as well as the trajectories in the phase space (x(t), x'(t) of all solns. (which by the way i am unsure of what is meant by phase space).

for the Van de pol eqn., here is my mfile in matlab

function z = xdot (t,x);
mu = 0.1;

z(1) = x(2);
z(2) = x(3);
z(3) = mu*(1-z(1)^2)*z(2) - z(1);

Here is my mfile for the 4th order runge kutta

function[t,y] = runkut4(t,x,h)
s1=xdot(t,x);
s2=xdot(t+h/2,x+h*s1/2);
s3=xdot(t+h/2,x+h*s2/2);
s4=xdot(t+h,x+h*s3);
x=x+h*(s1+2*s2+2*s3+s4)/6;

I dont know how to go on from here.

• Sep 25th 2008, 01:47 PM
CaptainBlack
Quote:

Originally Posted by Maccaman
So if my 2nd order ode is

$x'' = \mu (1-x^2)x' -x$

then how is that in MATLAB? (Angry)

Is it something like

function z = xdot (t,x);
mu = 0.1;
z(1) = x(2);
z(2) = x(3);
z(3) = mu*(1-z(1)^2)*z(2) - z(1);

A second order ODE converts to a first order ODE with a 2-D state, so no.

$X={x \brack x'}$

so for your ODE:

$
X'={x' \brack x''}={X_2 \brack \mu(1-X_1^2)X_2 - X_1}
$

So:

Code:

function z = xdot (t,x); mu = 0.1; z(1) = x(2); z(2) = mu*(1-x(1)^2)*x(2) - x(1);
RonL