How to solve an ODE with parameters calculated in another function?

3 次查看(过去 30 天)
Hi
I want to solve an ODE with parameters calculated in another function.
For example, I have the following ODE:
dy/dx = -5*y + f
where f is obtained from another function.
Then, how can I import this f into the ode solver?
I would really appreciate if anyone can help me out.
Thank you.
  6 个评论
James Tursa
James Tursa 2015-12-16
"... f depends on time, but cannot express as a function of time. ..."
This statement doesn't make sense to me. How can you possibly use f if you don't know how to calculate it as a function of time, given that it depends on time? I.e., what do you really have to work with here? Do you have a vector of preset values? Or what? I don't understand how you are getting your f values calculated.
Steven Lord
Steven Lord 2015-12-17
James, I interpreted that description as f being a vector of data, but the poster not knowing a functional relationship f = someFunction(t). It's like having:
f = [0 1 4 9 16 25];
t = [0 1 2 3 4 5];
without knowing/recognizing/being able to take advantage of the fact that f is just t.^2.

请先登录,再进行评论。

回答(3 个)

Jan
Jan 2015-12-17
It looks like your f is not continuous, but Matlab's ODE solvers fail for non smooth functions. See http://www.mathworks.com/matlabcentral/answers/59582#answer_72047
So the simple solution seems to run the integration in steps:
t = [0, 0.1, 0.2];
y0 = ???;
for k = 2:length(t)
fValue = f(t(k-1));
[T, Y] = ode45(@(t, y)@yourFcn(t, y, fValue), t(k-1:k), y0);
y0 = Y(end, :);
end
Then add some code to collect the T,Y in an array.

Steven Lord
Steven Lord 2015-12-17
Consider following the approach given in the third example on the documentation page for ODE45. That example passes two vectors, one containing time and the other values, for each of the two functions f(t) and g(t) and uses INTERP1 to interpolate the data to obtain a value at the time at which the ODE solver is evaluating the ODE function.
  1 个评论
Jan
Jan 2015-12-20
This is a good example of the problem, which occur when the function to be integrated is not smooth. What a pitty that it appears as example in the documentation.
When the tolerance is reduced to 1e-8, ODE45 rejects 58 steps at the locations, where INTERP1 creates non-smooth values:
>> Result.stats
nsteps: 203
nfailed: 58
nfevals: 1567
When the the interpolation is performed in 'pchip' mode, the integration runs with less rejected steps:
>> Result.stats
nsteps: 147
nfailed: 4
nfevals: 907
This seems to be not dramatic in this case. But when the trajectory is not stable, the differences can matter. An analysis of the sensitivity would suffer (a measurement of the variation of the results, when the initial values and/or parameters are varied).
So consider the specifications of the ODE integerators and do not integrate non-smooth functions for scientific purpusose.

请先登录,再进行评论。


Marc
Marc 2015-12-21
I would try a brute force method having your ODE function calling the function for f(....) right before you set up your dy/dx equations.

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by