MATLAB: Second Order Differential Equations to 2 First Order

differentialfirst orderMATLABsecond 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.

Best Answer

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));