How to solve an ODE with parameters calculated in another function?
조회 수: 4 (최근 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
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
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
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.
댓글 수: 0
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
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
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!