简体   繁体   English

如何实时显示Matlab的ode45结果?

[英]How to display Matlab's ode45 results in real time?

Let's suppose that we have the following solver for a system of first-order ODEs: 假设对于一阶ODE系统,我们具有以下求解器:

   % func.m
   function dydt = func(t,y)
   dydt = [y(2); (1-y(1)^2)*y(2)-y(1)];

and the main code: 和主要代码:

% solver.m
tspan=0:1:10;
[t,y] = ode45(@func,tspan,[2; 0]);

How to display in real-time the results, y1(t) and y2(t), for each time step t that the ode45 makes (t=0,1,2,...,10), without waiting for the whole code to finish? 如何实时显示ode45的每个时间步t(t = 0,1,2,...,10)的结果y1(t)和y2(t),而无需等待整个时间代码完成?

The OutputFcn ode solver option should be used. 应该使用OutputFcn ode求解器选项。 For example, to plot the solution vs time, the built-in output function odeplot can be used: 例如,要绘制解决方案随时间变化的曲线,可以使用内置的输出函数odeplot

options= odeset('OutputFcn',@odeplot);
[t,y] = ode45(@func,[0 200],[2; 0],options);

You can use your own output function. 您可以使用自己的输出功能。 Here is an example: 这是一个例子:

myOutputFcn= @(t,y,flag)fprintf('t= %s y=  %s\n',mat2str(t),mat2str(y))*0;
options= odeset('OutputFcn',myOutputFcn);
[t,y] = ode45(@f,0:1:10,[2; 0],options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM