How to plot intermediate variables of a function used by ode45 solver
조회 수: 10 (최근 30일)
이전 댓글 표시
Sir, I am calling ode45 for integrating a diff equation say x'=(1/(H))*(2x)
Main Program:
x0=0.1; final_time=1; [t,x]=ode45(@myfunc,[0,final_time],x0);% ODE Call plot(t(:,1),x(:,1))% plotting the state variable main ends
Function Program:
function dv=myfunc(t,x) H=1.0; Te=2*x(1);% intermediate calculations/variables for my differential equations %The Differential Equation dv=[(1/(H))*(Te)];
But i want to plot the intermediate variables say Te in this case.But ode returns t,x alone.How to plot the intermediate variables that was used in the integration process.
댓글 수: 0
채택된 답변
Jan
2013년 7월 29일
You can prepare your function such, that it replies the wanted value according to a special trigger method and accepts vectors as input also:
function dv = myfunc(t, x, flag)
H = 1.0;
Te = 2 * x(:, 1)
dv = Te ./ H; % Btw, No addition square brackets
if nargin == 3
dv = Te;
end
Now run the integration at first:
[t,x] = ode45(@myfunc,[0,final_time],x0);
and obtain the internal variable afterwards:
Te = myFunc(t, x, 'flag');
댓글 수: 5
추가 답변 (1개)
Shashank Prasanna
2013년 7월 29일
You can define an output function (outputfcn) that will be called after each iteration.
you can specify that in the options structure. In your custom output function you can use a plot command with a hold on in order to plot all you intermediate values.
댓글 수: 2
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!