假设我们对一阶ODE系统有以下求解器:
% func.m
function dydt = func(t,y)
dydt = [y(2); (1-y(1)^2)*y(2)-y(1)];
和主要代码:
% solver.m
tspan=0:1:10;
[t,y] = ode45(@func,tspan,[2; 0]);
如何实时显示结果y1(t)和y2(t),对于ode45所做的每个时间步t(t = 0,1,2,...,10),无需等待整个代码完成?
答案 0 :(得分:4)
The OutputFcn
ode solver option should be used. For example, to plot the solution vs time, the built-in output function odeplot
can be used:
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);