How do I pass out extra parameters using ODE23 or ODE45 from the MATLAB ODE suite?
조회 수: 120 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2010년 11월 5일
편집: MathWorks Support Team
2023년 4월 27일
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
2010년 11월 5일
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
Abhishek Pandey
2016년 6월 30일
편집: MathWorks Support Team
2023년 4월 27일
Hi Sam,
If you need any assistance with your question, please feel free to contact MathWorks Technical Support.
- Abhishek
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 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!