Problem with returning to Main function from subfunction
4 次查看(过去 30 天)
显示 更早的评论
Hi everybody,
I'm running into a problem when I want to execute a code that is schematic like the following (See below). It is supposed to produce the orbital decay of space objects. I use a ODE integrator like Runge-Kutte Fehleberg to get the rates of change. When a certain value is reached (like altitude is 0) I want to abort the integration and return the values to the main function for plotting etc. The integration and orbit decay seems to work ok but when the abort criteria is reached I get the following error:
-------------------------------------------------------------------
_Error in SimpleDecay>rates (line 135) dfdt = zeros(7,1);
Output argument "dydt" (and maybe others) not assigned during call to "C:\Users\...\SimpleDecay.m>SimpleDecay/rates".
Error in rkf45 (line 79) f(:,i) = feval(ode_function, t_inner, y_inner);
Error in SimpleDecay (line 86) [t,f] = rkf45(@rates, tspan, f0);
_--------------------------------------------------------------
I'm not sure whether I placed the abort 'if' correctly or what the problem really is so every help is highly appreciated!
Thanks in advance, David
The code is schematic like this:
function main
%Some definitions etc.
%Define timespan of integration & initial value vector
% Calling the integrator
[t,f] = rkf45(@rates, tspan, f0);
%return the solutions
a = f(:,1);
%etc.
plotting %as a subfunction call
return
function dydt = rates(ti,y)
dfdt = zeros(7,1);
initial vector definition
a = y(1);
%etc.
% Define some needed values
% Return when value is reached
if a < Re
return
end
%Calculate rates
dadt = ...
% etc.
% Load derivatives into output vector
dydt(1) = dadt;
end % end rates
end % end main
2 个评论
Andrew Newell
2012-1-11
This ODE solver is from MATLAB Central. Have you tried contacting the author? Also, have you considered using the MATLAB solvers like ode45?
采纳的回答
Walter Roberson
2012-1-12
You must assign values to any output variable used by the calling function if you are going to use "return" (explicitly or implicitly). This is basic MATLAB, not dependent on the fact that you are using an ode solver.
Early stopping for the MathWorks-provided ode* solvers is handled by passing an options structure (usually created with odeset) that includes an Events option that provides a function handle to test for a stop.
Examining the code for the rkf45 contribution, I see that it has no provision for early stopping except for this: returning a very large value from the user function will cause early termination. The easiest way to get the necessary large value would be to return infinity.
更多回答(2 个)
另请参阅
类别
在 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!