ode 45 plot of intermediate vraibles of differential equations

1 view (last 30 days)
clear all
clc;
Tspan =[0 1];
X0=[1.0; 1]; % Initial condition
%solving the differential Equation
[t,x]=ode45(@myfunc,Tspan,X0);
%Plotting the figures figure(1)
subplot(2,1,1)
plot(t(:,1),x(:,1))
xlabel('t');
ylabel('x1');
grid on
subplot(2,1,2)
plot(t(:,1),x(:,2))
xlabel('t');
ylabel('x2')
grid on
% subplot(3,1,3) % plot(t(:,1),P) % xlabel('t'); % ylabel('P') % grid on
The ode function is:
function dv=myfunc(t,x)
P=sqrt(x(1)+x(2));
%The Diffenrential Equation
dv=[ (x(1))+(x(2)*x(2))+P;%x1dot Equation
x(1)-x(2); ]; % x2dot Equation
I want to plot P also wrt time like x1 and x2.
Since the ode fucntion returns only the time and solution ie t and x (x1,x2) . How to get the P for plotting?.I tried declaring it global but in vein .Please help

Accepted Answer

Mischa Kim
Mischa Kim on 16 Feb 2014
Edited: Mischa Kim on 16 Feb 2014
Thayumanavan, after the ode45 call, compute P:
[t,x]=ode45(@myfunc,Tspan,X0);
P = sqrt(x(:,1) + x(:,2));
plot(t,P)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!