[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Second Order ODE
From: |
Przemek Klosowski |
Subject: |
Re: Second Order ODE |
Date: |
Tue, 18 Nov 2008 10:18:23 -0500 (EST) |
Here's an example how to reduce a higher-order linear equation to a
linear combination of first order equations, by the standard trick of
using X = [x x' x" ..]. Let's consider a simple damped harmonic
oscillator equation of motion: x" + bx' + kx=0. We substitute X=[x,x']
(i.e. Xdot = [x",x']), and rewrite it as:
Xdot + [ b k ; -1 0 ] * X = 0
This works out because the above expression expands to
[x" , x'] + [(bx' + kx) , -x'] , which is indeed equivalent to [0 , 0].
To solve this with LSODE e.g. for b=.1 and k=1, you need to specify a
reasonable time interval that encompasses few oscillations, say upto t=20,
and take an initial condition of zero speed and non-zero initial position:
X0=[1 0]; t=0:.01:20;
and define the function that calculates Xdot given the X and time (time is
actually unused here):
function Xdot=f(X,t);
k=1; b=.1;
Xdot = [-b -k; 1 0] * X;
endfunction
The solver is then called as x=lsode('f',X0,t)
To play with it, you can use Octave one-liner
t=0:.01:20; function Xdot=f(X,t); k=1; b=0.1; Xdot = [-b -k; 1 0] * X; end;
x=lsode('f',[1 0],t);plot(t/2/pi,x)
and indeed you see the harmonic motion with period 2pi. You can up-arrow and
re-edit the line above
to see what happens for different values of b or initial condition, etc.