Using ODE 45 on a system of equations

2 views (last 30 days)
Jme
Jme on 16 Feb 2013
Hi, having trouble representing dx^2/d^2t = -sinx as a system of two first order ODE's in MATLAB. I have broken down the second order ODE into
y_1 = x
y_1prime = y_2
y_2prime = -siny_1
Here is my matlab code so far
function yprime = siny(t,y)
yprime = -sin(y) ;
in one M file and then the run this function
tspan=[0,5];
yzero=0.5;
[t,y]=ode45(@siny,tspan,yzero);
plot(t,y)
now im just not sure how to represent convert this single plot into a system of equations.
  2 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 16 Feb 2013
What does mean: convert this single plot into a system of equations.
Jme
Jme on 16 Feb 2013
sorry i haven't wrote that down correctly. Basically i have to change a second order ODE to into a system of two first order ODEs and plot them.

Sign in to comment.

Answers (1)

Jan
Jan on 16 Feb 2013
Your siny function does not correlate to your system of differential equations of order 1. Better:
function yprime = siny(t,y)
yprime = [y(2), -sin(y(1))];
Now you need a [1 x 2] vector as initial value also.
  1 Comment
Jme
Jme on 16 Feb 2013
Thank you, i think i've managed it, but with a slight problem. I kept getting an error 'SINY1 must return a column vector', so i created a column vector but i cannot see how to make it work with a [1 x 2] vector as i have a column vector which is a [2 x 1] vector.
tspan=[0,5];
yzero=[0 1]';
[t,y]=ode45(@siny1,tspan,yzero);
plot(t,y)
siny function is as you posted above.
Thank you for the reply

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!