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.

 채택된 답변

Andrew Newell
Andrew Newell 2011년 2월 17일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by