[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Second Order ODE
From: |
Ivan Sutoris |
Subject: |
Re: Second Order ODE |
Date: |
Tue, 18 Nov 2008 00:20:42 +0100 |
On Mon, Nov 17, 2008 at 7:33 PM, William Christopher Carleton
<address@hidden> wrote:
> I've been trying to get lsode to work on a second order ODE by breaking it
> into two firsts, but can't seem to figure out the syntax. Does anyone have a
> code example I could look at?
>
> x'(t)=y(t)
> y'(t)=rx(t)
>
> I was able to code it and get it to plot using a for loop, but I'd really
> like to see how it could be done with lsode. Thanks,
>
> Chris
If you are trying to solve (d^2 x / d x^2) = r * x, this is how it can be done:
r = -1;
x0 = [10; 0];
t = 0:0.01:10;
f = @(u,t) [0 1; r 0] * u;
[X STATE MSG] = lsode(f,x0,t);
plot(t,X(:,1))
Basically, after transforming to two variables, you get a system
x' = 0*x + 1*y
y' = r*x + 0*y
so the main trick is to write function f correctly so that it returns
vector [x' ; y'] when you give it vector [x; y] and time t (which is
not used here) as inputs.
Regards
Ivan Sutoris