How to plot intermediate variables of a function used by ode45 solver:

Asked by Thayumanavan about 8 hours ago
Latest activity Answered by Jan Simon about 6 hours ago

Sir, I want to plot the intermediate variables say Te from a function that was used by ode45 solver .But i am not getting the response exactly. The time response of x is not constant(from the plot of (t,x(:,1))). But the The time response of Te seems to be constant with respect to time. The problem is ,in workspace i am not getting the array of Te values for all time instants.I am getting a single last value of Te say 4.5 (1x 1 Element) Workspace is having the last value of Te alone ie. at t=10 if tspan=[0 10]. But the vector of state variable x is coming in workspace.(say 1057x1) entries.(If x is time varying means Te should also vary with respect to time) . I tried giving Te as global variable also.But in vein.Please help me .

Main Pragram:
x0=0.1;
final_time=1;
[t,x]=ode45(@myfunc,[0,final_time],x0);% ODE Call
plot(t(:,1),x(:,1))% plotting the state varaible 
Intervar=myfunc(t,x,'flag');
plot(t(:,1),Intervar)
main ends
Function :
function dv=myfunc(t,x)
H=1.0;
Te=2*x(1);% intermediate calculations/variables for my differential equations 
%The Diffenrential Equation 
dv=[(1/(H))*(Te)];
if nargin == 3
dv=Te; //Returns the intermediate variable Te
end

0 Comments

Thayumanavan

Products

No products are associated with this question.

2 Answers

Answer by Azzi Abdelmalek about 7 hours ago
Edited by Azzi Abdelmalek about 7 hours ago

In your code Te represent 2*x, then why you don't just type

Te=2*x(:,2)

0 Comments

Azzi Abdelmalek
Answer by Jan Simon about 6 hours ago
Edited by Jan Simon about 6 hours ago

Please copy the code I have posted exactly:

function dv = myfunc(t, x, flag)
H  = 1.0;
Te = 2 * x(:, 1);   % <-- Not "Te=2*x(1);"
dv = Te ./ H;       % Btw, No additional square brackets
if nargin == 3
  dv = Te;
end

0 Comments

Jan Simon

Contact us