Second Order Differential Equations to 2 First Order

3 views (last 30 days)
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.

Accepted Answer

Andrew Newell
Andrew Newell on 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 Comment
Matt Tearle
Matt Tearle on 17 Feb 2011
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);

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!