フィルターのクリア

Plotting different datasets from looped ode45 using subplots.

2 ビュー (過去 30 日間)
Frederik Bjerregaard
Frederik Bjerregaard 2021 年 12 月 9 日
コメント済み: Star Strider 2021 年 12 月 9 日
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

採用された回答

Star Strider
Star Strider 2021 年 12 月 9 日
I assume that the intended result is to have different plots representing the function with different values of ‘f’ in each subplot. The code needs to be tweaked dlightly to get there —
% Defining parameters and initial conditions
f0 = 0.35;
fv = [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:numel(fv)
f = f0 + fv(i); % Create New 'f' For Each Iteration
[t,theta] = ode45(@(t,theta) odefcn(t,theta,f,r,zeta), tspan,Theta0);
eqn=@(A) f^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(3,2,i); % Iterate Through 'subplot' Array
plot(theta(:,1),theta(:,2),thetaHB,theta_HB);
pbaspect([1 1 1])
title(sprintf('f=0.35+%.1f',fv(i)));
xlabel ('\theta');
ylabel('d\theta/dt');
hl = legend('Numerical','Harmonic Balance');
hl.Visible = 'off';
% 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
subplot(3,2,[5 6])
ax = gca;
pos = ax.Position;
hl.Position = pos + [0.1 0.15 -0.2 -0.2];
hl.Visible = 'on';
ax.Visible = 'off';
sgtitle('Phase Planes r=0.3','FontSize',20)
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
I re-positioned the legend so that it would not cover the plots, since it was common to all of them.
.
  2 件のコメント
Frederik Bjerregaard
Frederik Bjerregaard 2021 年 12 月 9 日
Thank you very much!
Star Strider
Star Strider 2021 年 12 月 9 日
As always, my pleasure!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by