ode output function problem
显示 更早的评论
Hi,
I am using an output function for ode32s and I have a question regarding outputFcn.
Matlab ver 2018b,
On line 288 of ode32s:
feval(outputFcn,[t tfinal],y(outputs),'init',outputArgs{:});
On line 515 of ode32s however:
stop = feval(outputFcn,tout_new,yout_new(outputs,:),'',outputArgs{:});
Code is failing for too much input b/c in the latter there is one input missing with ''
Can you help me what's going on here?
采纳的回答
Steven Lord
2019-5-16
As documented in the odeset function documentation page the function you specify as your custom OutputFcn must have a certain syntax and must accept certain types of calls.
"If you write a custom output function, then it must be of the form
status = myOutputFcn(t,y,flag)
The output function must also respond appropriately to these flags:"
You haven't provided much detail about the problem you're experiencing, but I suspect your custom OutputFcn does not conform to this requirement because it fails to accept the flag input argument.
7 个评论
It indeed conforms to the "initial" with 4 input variables (not 3)
feval(outputFcn,[t tfinal],y(outputs),'init',outputArgs{:});
And this is my function form:
myOutputFcn(t_tfinal, ysol_i, initTag, outputArgs)
I actually needed the forth input outputArgs, this "status = myOutputFcn(t,y,flag)" actually returns error "too man input arguments"
So, what is wrong with this anyways?
stop = feval(outputFcn,tout_new,yout_new(outputs,:),'',outputArgs{:});
Show your call to ode23 along with the code that defines your options structure containing your OutputFcn. My suspicion now is that you're passing additional parameters after the options structure which is supported for backwards compatibility with very old versions of MATLAB but is neither documented nor recommended any longer. The techniques described on this documentation page are the recommended ways to pass additional parameters into your ODE function, event functions, output functions, etc.
I am only passing parameters as following:
opts = odeset('RelTol',1e-6,'AbsTol',1e-10,'Stats','on','OutputFcn',@myOutputFcn);
[t_sol, y_sol] = ode23s(@BilMos10, [time_ext], [y0],opts, Params);
Params is a structure array.
I am not passing additionaly parameters. Do you see anything odd on the code above?
I am not passing additionaly parameters.
Yes you are, and you're using the undocumented and discouraged approach I mentioned. As written both BilMos10 and myOutputFcn must accept the Params struct array regardless of whether or not they use it. If only BilMos10 uses it, write your code like this instead so it doesn't get passed into myOutputFcn at all. Then myOutputFcn needs to accept only the three inputs t, y, and flag.
opts = odeset('RelTol',1e-6,'AbsTol',1e-10,'Stats','on','OutputFcn',@myOutputFcn);
[t_sol, y_sol] = ode23s(@(t, y) BilMos10(t, y, Params), time_ext, y0,opts);
If only myOutputFcn needs Params:
opts = odeset('RelTol',1e-6,'AbsTol',1e-10,'Stats','on','OutputFcn', ...
@(t, y, flag) myOutputFcn(t, y, flag, Params));
[t_sol, y_sol] = ode23s(@BilMos10, time_ext, y0,opts);
Ok, I tried this:
opts = odeset('RelTol',1e-6,'AbsTol',1e-10,'Stats','on','OutputFcn',@myOutputFcn);
[t_sol, y_sol] = ode23s(@(t, y) BilMos10(t, y, Params), time_ext, y0, opts);
myOutputFcn(t_tfinal, ysol_i, Tag)
It is still giving error at line 515 of ode23s (no error on line 288)
"Error using myOutputFcn
Too many output arguments."
This is a different problem than your original question. In the case where ode23s calls your output function with the '' flag, it must return an output telling ode23s whether or not to continue solving the ODE. From the documentation:
myOutputFcn must return a status of 0 or 1. If status = 1, then the solver halts integration. You can use this mechanism, for instance, to implement a Stop button.
Your myOutputFcn doesn't return anything, I assume?
Yes, my myOutputFcn function doesn't return anything.
Yes, status is needed.
Thanks for your help.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
产品
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
