John Doe
John Doe

Reputation: 149

Interpolation within ode45

I have a vector with discrete values that I need to pass into my ODE system and I want to use ode45 command. This vector needs to be interpolated within the solver when I use it. Is there a way this can be done?

I have a coupled system of linear ODEs.

dxdt = f(t)*x + g(t)*y
dydt = g(t)*x + h(t)*y

I have three vectors f(t), g(t) and h(t) as a function of t. I need to pass them into the the solver.

I can code up a Runge-Kutta solver in C or C++. I was suggested that doing it in Matlab would be quicker. Can someone suggest a way to do this?

Upvotes: 0

Views: 1447

Answers (1)

Simon
Simon

Reputation: 32873

Sure, you can use interp1. Suppose you have vectors fvec, gvec and tvec containing respectively the values of f, g, and the time points at which these values are taken, you can define your derivative function as:

dxydt = @(t,x) [interp1(tvec, fvec, t) * x(1) + interp1(tvec, gvec, t) * x(2)
                interp1(tvec, gvec, t) * x(1) + interp1(tvec, hvec, t) * x(2)];

and use it in ode45:

[T,Y] = ode45(dxydt, tspan, [x0; y0]);

Upvotes: 2

Related Questions