필터 지우기
필터 지우기

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.

채택된 답변

Jan
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
Thayumanavan
Thayumanavan 2013년 8월 3일
Sir ,I got answer for the problem that i asked help.But for simplicity i gave that differential equation.But my code is little bit different with many variables.I am attaching the main program and my function.Please help me to plot Te and Vpcc intermediate variable.
Main Code: clear all clc;
% Time Tspan =[0 10]; X0=[0.7; 1.0151; 157 ];
options=odeset('RelTol',1e-3,'AbsTol',1e-6);
%solving the differential Equation [t,x]=ode45(@dvdt,Tspan,X0,options);
%Plotting the figures figure subplot(5,1,1) plot(t(:,1),x(:,1)) xlabel('t'); ylabel('Eprime'); title('Dynamic Wind Time Response ') grid on
subplot(5,1,2) plot(t(:,1),x(:,2)) xlabel('t'); ylabel('Delta') grid on
wradsec=x(:,3); wmech=(60*wradsec)/(2*pi); wpu=wmech/1500;
subplot(5,1,3) plot(t(:,1),wradsec) xlabel('t'); ylabel('Omega') grid on
% Plotting of Intermediate Variables as per Your Idea subplot(5,1,4) op= dvdt(t, x, 'flag'); plot(t(:,1),op(1,:)) xlabel('t'); ylabel('Vterminal') grid on
subplot(5,1,5) plot(t(:,1),op(2,:)) xlabel('t'); ylabel('ElectromagneticTorque') grid on
Function code: function dv=dvdt(t,x,flag)
Xs=0.0754;Xr=0.0326;H=0.20; Rl=0.0083;Rcon=0.0089;Xl=0.033;Xcon=0.01789; Vdinf=1.0;Vqinf=0.0;wspu=157.0; T=0.7855;Xdash=0.1076;D=6.4165; Rt=Rl+Rcon;Xt=Xl+Xcon; xtrans=Xt+(D/Xr); detm=(Rt*Rt)+(xtrans*xtrans); J1=-xtrans;J2=-Rt;J3=-Rt;J4=xtrans; iq=(1/detm)*((J3*((x(1)*cos(x(2)))-Vqinf))+(J4*((-x(1)*sin(x(2)))-Vdinf))); id=(1/detm)*((J2*((-x(1)*sin(x(2)))-Vdinf))+(J1*((x(1)*cos(x(2)))-Vqinf))); Vq=(1/Xr)*((D*id)+(Xr*x(1)*cos(x(2)))); Vd=(-1/Xr)*((D*iq)+(Xr*x(1)*sin(x(2)))); Vdpc=(-Rcon*id)+(Xcon*iq)+Vdinf; Vqpc=(-Rcon*iq)-(Xcon*id)+Vqinf; VPCC=sqrt((Vdpc*Vdpc)+(Vqpc*Vqpc));% Intermediate Variable 1 t1=(Vq*cos(x(2)))-(Vd*sin(x(2))); t2=(Vq*sin(x(2)))+(Vd*cos(x(2))); t3=((Vd*cos(x(2)))+(Vq*sin(x(2)))); Te=(x(1)*t3/Xdash); % Intermediate Variable 2
%The Diffenrential Equation
dv=[ (1/T)*((-Xs*x(1)/Xdash)+(((Xs-Xdash)/Xdash)*t1)); % My 1st Differential Equation
(x(3)-wspu)-(((Xs-Xdash)*t2) /(Xdash*T*x(1))); %My 2nd Differential Equation
(wspu/(2*H))*(Tm+Te)]; % My 3rd Differential Equation
if nargin == 3
dv = [VPCC;
Te];
end
You copy paste the code execute and suggest me to plot Te,Vpcc.Thanks
Thayumanavan
Thayumanavan 2013년 8월 3일
clear all clc; % Time Tspan =[0 10]; X0=[0.7; 1.0151; 157 ];
options=odeset('RelTol',1e-3,'AbsTol',1e-6);
%solving the differential Equation [t,x]=ode45(@dvdt,Tspan,X0,options);
%Plotting the figures figure subplot(5,1,1) plot(t(:,1),x(:,1)) xlabel('t'); ylabel('Eprime'); title('Dynamic Wind Time Response ') grid on
subplot(5,1,2) plot(t(:,1),x(:,2)) xlabel('t'); ylabel('Delta') grid on
wradsec=x(:,3); wmech=(60*wradsec)/(2*pi); wpu=wmech/1500;
subplot(5,1,3) plot(t(:,1),wradsec) xlabel('t'); ylabel('Omega') grid on
% Plotting of Intermediate Variables as per Your Idea subplot(5,1,4) op= dvdt(t, x, 'flag'); plot(t(:,1),op(1,:)) xlabel('t'); ylabel('Vterminal') grid on
subplot(5,1,5) plot(t(:,1),op(2,:)) xlabel('t'); ylabel('ElectromagneticTorque') grid on
Function Code: function dv=dvdt(t,x,flag)
Xs=0.0754;Xr=0.0326;H=0.20; Rl=0.0083;Rcon=0.0089;Xl=0.033;Xcon=0.01789; Vdinf=1.0;Vqinf=0.0;wspu=157.0; T=0.7855;Xdash=0.1076;D=6.4165; Rt=Rl+Rcon;Xt=Xl+Xcon; xtrans=Xt+(D/Xr); detm=(Rt*Rt)+(xtrans*xtrans); J1=-xtrans;J2=-Rt;J3=-Rt;J4=xtrans; iq=(1/detm)*((J3*((x(1)*cos(x(2)))-Vqinf))+(J4*((-x(1)*sin(x(2)))-Vdinf))); id=(1/detm)*((J2*((-x(1)*sin(x(2)))-Vdinf))+(J1*((x(1)*cos(x(2)))-Vqinf))); Vq=(1/Xr)*((D*id)+(Xr*x(1)*cos(x(2)))); Vd=(-1/Xr)*((D*iq)+(Xr*x(1)*sin(x(2)))); Vdpc=(-Rcon*id)+(Xcon*iq)+Vdinf; Vqpc=(-Rcon*iq)-(Xcon*id)+Vqinf; VPCC=sqrt((Vdpc*Vdpc)+(Vqpc*Vqpc));% Intermediate Variable 1 t1=(Vq*cos(x(2)))-(Vd*sin(x(2))); t2=(Vq*sin(x(2)))+(Vd*cos(x(2))); t3=((Vd*cos(x(2)))+(Vq*sin(x(2)))); Te=(x(1)*t3/Xdash)% Intermediate Variable 2 Tm=1.0;
%The Diffenrential Equation
dv=[ (1/T)*((-Xs*x(1)/Xdash)+(((Xs-Xdash)/Xdash)*t1)); % My 1st Differential Equation
(x(3)-wspu)-(((Xs-Xdash)*t2) /(Xdash*T*x(1))); %My 2nd Differential Equation
(wspu/(2*H))*(Tm+Te)]; % My 3rd Differential Equation
if nargin == 3
dv = [VPCC;
Te];
end
Little change in code .i forgot to give 1 parameter .You pls simulate and tell me what change i need to do in the code to get plot of Te,Vpcc

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Shashank Prasanna
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
Jan
Jan 2013년 7월 29일
Even the output function has no access to the internally used variables.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by