Plotting different datasets from looped ode45 using subplots.
Show older comments
Hi. I have a script where I'm solving and ode for different values of a parameter. I want to plot each of these cases on a seperate subplot as plotting them all on the same plot is messy. So far i have managed to get the result for the first iteration onto every subplot using:
clc; clear;close all;
% Defining parameters and initial conditions
f = 0.35+[0.1:0.1:0.4];
r = 0.3;
zeta = 0.25;
eps = -1/6;
Theta0 = [0.30, 0];
tspan = [0 100];
% Solver options
opts = odeset('RelTol',1e-6,'AbsTol',[1e-9 1e-9]);
% Solver
for i = 1:length(f)
[t,theta] = ode45(@(t,theta) odefcn(t,theta,f(i),r,zeta), tspan,Theta0);
eqn=@(A) f(i)^2-(-r^2*A+A+(3*eps*A^3)/4)^2-(2*zeta*r*A)^2;
Amp(:,i)=fzero(eqn,0.1);
thetaHB=Amp(i).*sin(r.*t);
theta_HB=Amp(i).*r.*cos(r.*t);
subplot(2,2,1);
plot(theta(:,1),theta(:,2),thetaHB,theta_HB);
pbaspect([1 1 1])
title('f=0.35+0.1');
xlabel ('\theta');
ylabel('d\theta/dt');
legend('Numerical','Harmonic Balance');
subplot(2,2,2);
plot(theta(:,1),theta(:,2),thetaHB,theta_HB);
pbaspect([2 2 1])
title('f=0.35+0.2');
xlabel ('\theta');
ylabel('d\theta/dt');
legend('Numerical','Harmonic Balance');
subplot(2,2,3);
plot(theta(:,1),theta(:,2),thetaHB,theta_HB);
pbaspect([1 1 1])
title('f=0.35+0.3');
xlabel ('\theta');
ylabel('d\theta/dt');
legend('Numerical','Harmonic Balance');
subplot(2,2,4);
plot(theta(:,1),theta(:,2),thetaHB,theta_HB);
pbaspect([1 1 1])
title('f=0.35+0.4');
xlabel ('\theta');
ylabel('d\theta/dt');
legend('Numerical','Harmonic Balance');
sgtitle('Phase Planes r=0.3','FontSize',20)
end
The underlying function for odefcn is
function dthetadt = odefcn(t,theta,f,r,zeta)
dthetadt = zeros(2,1);
dthetadt(1)=theta(2);
dthetadt(2)=f.*sin(r.*t)-2.*zeta.*theta(2)-sin(theta(1));
end
Accepted Answer
More Answers (0)
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
