How do I pass out extra parameters using ODE23 or ODE45 from the MATLAB ODE suite?

106 次查看(过去 30 天)
I would like to return some parameters from the ode45 solution that do not need to be integrated, but which are important to the result. I would like to have the function I pass to ODE23/ODE45 return extra parameters which the ODE solver ignores during the computation but stores for later review.

采纳的回答

MathWorks Support Team
There is no method for passing out additional parameters from the ODE function using the ODE solvers provided in the MATLAB ODE suite (e.g., ODE45). These additional parameters, however, can be passed out after ODE45 returns a solution.
For example, consider the ODE:
y' = x + k*y
where
k = x^2 + y^2
If you have a need to obtain "k" for specified "x" and "y", you can create the following ODE function:
function [dydx k] = myode(x, y)
k = x.^2 + y.^2;
dydx = x + k.*y;
The solution to the ODE can be solved using the commands:
[X Y] = ode45(@myode, [0 5], 1);
Then, "k" can be obtained via the command:
[dydx k] = myode(X, Y);
If you would like to obtain a series of values that are computed during the solution algorithm, you can declare a variable as PERSISTENT, and append the value computed during each call to the ODE function to the end of this persistent variable.
For example, consider the previous ODE. If you would like to obtain a vector of the "x" and "y" values specified for each call to the ODE function, try the following ODE function:
function [dydx xvt yvt] = myode(x, y)
persistent xv yv
xv = [xv; x];
yv = [yv; y];
k = x.^2 + y.^2;
dydx = x + k.*y;
if nargout>1
xvt = xv; yvt = yv;
end
The solution to the ODE can be solved using the commands:
[X Y] = ode45(@myode, [0 5], 1);
Then, "xv" and "yv" can be obtained via the command:
[dydx xv yv] = myode([], []);
You could also use the ASSIGNIN, EVALIN, or SAVE functions. The ASSIGNIN function allows you to assign variables into workspace, and the EVALIN function evaluates an expression in the workspace. The SAVE function saves workspace variables to disk. The following example demonstrates how to assign the variable 'dy' from the ODEFUN to the workspace. Save the following into a function file named "odefun.m":
function dy =odefun(t,y)
dy=1;
varToPassOut=2;
assignin('base','varInBase',varToPassOut);
evalin('base','varPassedOut(end+1)=varInBase');
After writing this function, execute the following at the MATLAB command prompt:
varPassedOut=0
[T,Y] = ode45(@odefun,[0 12],[0]);
varPassedOut=varPassedOut(2:end);
Note that you need to define the variable "varPassedOut" in the base workspace before calling ODE45, or you will receive an error.
  2 个评论
Stephen23
Stephen23 2023-2-25
编辑:Stephen23 2023-2-25
"How should I solve this?"
Solve what exactly? The ODE solver can evaluate many function steps that are not included in the output arrays, incuding taking steps backwards and taking very small steps. However this answer does exactly what users ask for: it stores all of those evaluated steps, including all steps that will be discarded by the ODE solver. There is no way to distinguish these steps from within your ODEFUN, and having all steps is (in most cases) useless.
So the original posted question is an example of the X-Y problem, and this answer is good example of how providing a user with exactly what they ask for does not actually give them what they need (cf. EVAL).
The robust solution was already given five years before you asked, on this very thread:
and on many many other threads on this forum, e.g.:

请先登录,再进行评论。

更多回答(0 个)

类别

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