Second Order Differential Equations to 2 First Order

I'm trying to turn <http://latex.codecogs.com/gif.latex?\frac{\mathrm{d^2}%20\theta%20}{\mathrm{d}%20t^2}%20=%20-sin\theta> to 2 first order differential equations so I can then use ode45 to solve them. With initial conditions theta(0)=0.1 and theta primed(0)=0, <http://latex.codecogs.com/gif.latex?\frac{\mathrm{d}%20\theta%20}{\mathrm{d}%20t}=%200>
Here is my attempt at it:
function xprime=first(t,x);
xprime(1)=x(2);
xprime(2)=-sin(x);
xprime=xprime(:);
I save this as a mfile.
Then use :
ts[1,10];
x0 = [0 1]
[t,s] = ode45('first',ts,x0);
I get the error :
n an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> first at 5 xprime(2)=-sin(x);
Now I have no clue how to correct this problem. I followed the guide here http://www.mathworks.com/support/tech-notes/1500/1510.html#reduce to no avail.
Anyone got any ideas? This is the first question on my assignment questions and the rest require this to be correct so am totally stuck at the moment.
Thanks in advance.

 Akzeptierte Antwort

Andrew Newell
Andrew Newell am 17 Feb. 2011
The problem is that in
xprime(2)=-sin(x);
the LHS is a scalar while the RHS is a vector. I think what you need is
xprime(2)=-sin(x(1));

1 Kommentar

This is indeed the problem. I just want to add that it's nicer to use a function handle to pass the rate equations to ODE45:
[t,s] = ode45(@first,[t0,tf],x0);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by