Thread Subject: Solving an ODE and using interp1

Subject: Solving an ODE and using interp1

From: Anna Mummert

Date: 31 Jul, 2009 17:41:02

Message: 1 of 3

I need to interpolate some data to use in a system of ODEs and then solve the system (using ode45, or similar).

On the MathWorks documentation (see the main page on solving ODEs), they have a solution with the interpolation in the definition of the ODE. I can do this, however, I need to repeatedly call the ODE as part of a (fminsearch) parameter search. I belive that the computation would be faster if the program could do the interpolation once, outside of the ODE, instead of every time the ODE is called.

I've tried several methods of creating a function (or script) that interpolates the function once and uses the function in the ODE. I usually get the error "subscripted assignment dimension mismatch".

I have defined a function of time: f(t). When I write my ODE with the function:

function yprime=myode(t,y)
yprime(1) = f(t)*7 - ...;
yprime(2) = ....;

and try to call ode45, that is when I get the error. I understand that the ode thinks that f(t) is the entire vector of data and not the value of function f at time t.

When I test the function f(t) with an actual t value (ex: f(7)), the function works correctly.

How do I get the ode to recognize that it should use the value of f at t, and not the entire vector f(t)? Also, would moving the interpolation outside of the ode actually make the computation faster?

Subject: Solving an ODE and using interp1

From: Carlos Adrian Vargas Aguilera

Date: 31 Jul, 2009 18:05:19

Message: 2 of 3


> function yprime=myode(t,y)
> yprime(1) = f(t)*7 - ...;
> yprime(2) = ....;
>

try the following

% ---------->
X = ...
Y = ...
f = @(x) interp1(X,Y,x,'spline','extrap');
myode = @(t,y) [f(t)*7-...; ...]; % anonymous function
sol = ode45(...)
% ----------<

or if MYODE cannot be a handle function, then force it to be:

% ---------->
X = ...
Y = ...
f = @(x) interp1(X,Y,x,'spline','extrap');
myode = @(t,y) myodefunc(t,y,f);
sol = ode45(...)

% SUBFUNCTIONS
function yprime = myodefunc(x,y,f)
A = 2;
B = ...
yprime(1) = f(t)*7-B;
yprime(2) = ...
plot(yprime(1),yprime(2))...
% ----------<

I know that we can input extra arguments to the functions on ODE45 but, using anonymous functions is easier and cleaner.

Saludos, carlos

Subject: Solving an ODE and using interp1

From: Anna Mummert

Date: 6 Aug, 2009 14:31:18

Message: 3 of 3

"Carlos Adrian Vargas Aguilera" <nubeobscura@hotmail.com> wrote in message <h4vbov$dfh$1@fred.mathworks.com>...
>
> > function yprime=myode(t,y)
> > yprime(1) = f(t)*7 - ...;
> > yprime(2) = ....;
> >
>
> try the following
>
> % ---------->
> X = ...
> Y = ...
> f = @(x) interp1(X,Y,x,'spline','extrap');
> myode = @(t,y) [f(t)*7-...; ...]; % anonymous function
> sol = ode45(...)
> % ----------<
>
> or if MYODE cannot be a handle function, then force it to be:
>
> % ---------->
> X = ...
> Y = ...
> f = @(x) interp1(X,Y,x,'spline','extrap');
> myode = @(t,y) myodefunc(t,y,f);
> sol = ode45(...)
>
> % SUBFUNCTIONS
> function yprime = myodefunc(x,y,f)
> A = 2;
> B = ...
> yprime(1) = f(t)*7-B;
> yprime(2) = ...
> plot(yprime(1),yprime(2))...
> % ----------<
>
> I know that we can input extra arguments to the functions on ODE45 but, using anonymous functions is easier and cleaner.
>
> Saludos, carlos

Sorry it took so long to reply, regardless, thanks!

I've moved the ode into the same function that calculates the least squares error and changed it to have the anonymous function format. I also changed the interpolated data to the anonymous function format. Everything works! Thanks again!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
ode45 Carlos Adrian Vargas Aguilera 31 Jul, 2009 14:09:04
anonymous function Carlos Adrian Vargas Aguilera 31 Jul, 2009 14:09:04
ode Carlos Adrian Vargas Aguilera 31 Jul, 2009 14:09:04
interp1 Carlos Adrian Vargas Aguilera 31 Jul, 2009 14:09:04
rssFeed for this Thread

Contact us at files@mathworks.com