Effacer les filtres
Effacer les filtres

How to define legend (or assign arrows to each plot)?

11 vues (au cours des 30 derniers jours)
Mehrdad Nasirshoaibi
Mehrdad Nasirshoaibi le 2 Avr 2023
In the following code, I have three plots for gamma 60, 80 and -80. But as you can see it is not obvious that which plot related to which gamma. Do you guys have ant solution? (one of the solutions is to plot them seperately, but I want them in a same plot).
%Analytical solution of Duffing equation
%% Matlab code to find the frequency response of Single Duffing oscillator
clear all;
clc;
% Given values
gamma_vec = [60 80 -80]; % Nonlinear Parameter; %1; % Nonlinear Parameter
q = 0.02; % Forcing amplitude
beta = 0.05; % linear damping
w0= 1; % Primary resonance
a=linspace(0.01,2.5,1000); % Range of a
%Finding the values of excitation frequency Omega/w0= F and G.
% And corresponding eigen values
for jj=1:length(gamma_vec)
gamma = gamma_vec(jj);
for ii=1:1:length(a)
F(ii)=(1+3*gamma/(8*w0^2)*a(ii)^2+sqrt((q/(2*w0^2*a(ii)))^2-beta^2));
lamF1=sqrt(-(F(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(F(ii)-w0-9*gamma/(8*w0)*a(ii)^2))-(beta*w0);
lamF2=-sqrt(-(F(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(F(ii)-w0-9*gamma/(8*w0)*a(ii)^2))-(beta*w0);
G(ii)=(1+3*gamma/(8*w0^2)*a(ii)^2-sqrt((q/(2*w0^2*a(ii)))^2-beta^2));
lamG1=sqrt(-(G(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(G(ii)-w0-9*gamma/(8*w0)*a(ii)^2))-(beta*w0);
lamG2=-sqrt(-(G(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(G(ii)-w0-9*gamma/(8*w0)*a(ii)^2))-(beta*w0);
if lamF1==conj(lamG1)% % To terminate the loop
break
else
if gamma>0 % For spring hardening effect
plot(G(ii),a(ii),'b.');
hold on
FG=(F(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(F(ii)-w0-9*gamma/(8*w0)*a(ii)^2)+(beta*w0)^2;
if FG<0
plot(F(ii),a(ii),'r.')
else
plot(F(ii),a(ii),'b.')
end
else
plot(F(ii),a(ii),'b.');
hold on
GF=(G(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(G(ii)-w0-9*gamma/(8*w0)*a(ii)^2)+(beta*w0)^2;
if GF<0
plot(G(ii),a(ii),'r.')
else
plot(G(ii),a(ii),'b.')
end
end
end
end
end
xlim([-1,3]); % Range of x-axis
ylim([0,0.3]); %Range of y-axis
xlabel('Normalised frequency (\Omega/\omega_{0})') % Label of x-axis
ylabel('Normalised amplitude (a)') % Label of y-axis
title_str = sprintf('Frequency response of a nonlinear system', gamma);% Create a formatted string with the values of your variables
% Update the title of your figure
title(title_str);
  2 commentaires
Dyuman Joshi
Dyuman Joshi le 2 Avr 2023
Modifié(e) : Dyuman Joshi le 2 Avr 2023
Firstly, format your code properly and use proper indentation.
Secondly, you are plotting each point individually and not a line, and thus it is not possible to make legends as you wish to make. Instead of plotting each point, store the points in an array and plot after the for loop.
Remove LamF2 and LamG2 if there is no use of them in the code, or define them as -LamF1 and -LamG1.
Mehrdad Nasirshoaibi
Mehrdad Nasirshoaibi le 2 Avr 2023
Thanks

Connectez-vous pour commenter.

Réponses (1)

Bjorn Gustavsson
Bjorn Gustavsson le 2 Avr 2023
Use the output from plot. Something like this:
%Analytical solution of Duffing equation
%% Matlab code to find the frequency response of Single Duffing oscillator
clf
% Given values
gamma_vec = [60 80 -80]; % Nonlinear Parameter; %1; % Nonlinear Parameter
q = 0.02; % Forcing amplitude
beta = 0.05; % linear damping
w0= 1; % Primary resonance
a=linspace(0.01,2.5,1000); % Range of a
psz = 18;
%Finding the values of excitation frequency Omega/w0= F and G.
% And corresponding eigen values
for jj = 1:length(gamma_vec)
clr1 = rgb2hsv(rand(1,3));
clr1(2) = clr1(2)^.5;
clr2 = clr1;
clr2(1) = rem(clr2(1)+0.5,1);
clr1 = hsv2rgb(clr1);
clr2 = hsv2rgb(clr2);
gamma = gamma_vec(jj);
for ii = 1:1:length(a)
F(ii) = (1+3*gamma/(8*w0^2)*a(ii)^2+sqrt((q/(2*w0^2*a(ii)))^2-beta^2));
lamF1 = sqrt(-(F(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(F(ii)-w0-9*gamma/(8*w0)*a(ii)^2))-(beta*w0);
lamF2 = -sqrt(-(F(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(F(ii)-w0-9*gamma/(8*w0)*a(ii)^2))-(beta*w0);
G(ii) = (1+3*gamma/(8*w0^2)*a(ii)^2-sqrt((q/(2*w0^2*a(ii)))^2-beta^2));
lamG1 = sqrt(-(G(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(G(ii)-w0-9*gamma/(8*w0)*a(ii)^2))-(beta*w0);
lamG2 = -sqrt(-(G(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(G(ii)-w0-9*gamma/(8*w0)*a(ii)^2))-(beta*w0);
if lamF1==conj(lamG1)% % To terminate the loop
break
else
if gamma > 0 % For spring hardening effect
phG1(jj) = plot(G(ii),a(ii),'.','markersize',psz,'color',clr1);
hold on
FG = (F(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(F(ii)-w0-9*gamma/(8*w0)*a(ii)^2)+(beta*w0)^2;
if FG < 0
phF2(jj) = plot(F(ii),a(ii),'.','markersize',psz,'color',clr2);
else
phF1(jj) = plot(F(ii),a(ii),'.','markersize',psz,'color',clr1);
end
else
phF1(jj) = plot(F(ii),a(ii),'.','markersize',psz,'color',clr1);
hold on
GF = (G(ii)-w0-3*gamma/(8*w0)*a(ii)^2)*(G(ii)-w0-9*gamma/(8*w0)*a(ii)^2)+(beta*w0)^2;
if GF < 0
phG2(jj) = plot(G(ii),a(ii),'.','markersize',psz,'color',clr2);
else
phG1(jj) = plot(G(ii),a(ii),'.','markersize',psz,'color',clr1);
end
end
end
end
end
xlim([-1,3]); % Range of x-axis
ylim([0,0.3]); %Range of y-axis
xlabel('Normalised frequency (\Omega/\omega_{0})') % Label of x-axis
ylabel('Normalised amplitude (a)') % Label of y-axis
title_str = sprintf('Frequency response of a nonlinear system', gamma);% Create a formatted string with the values of your variables
% Update the title of your figure
title(title_str);
legend([phF1,phF2,phG1],'F1-1','F1-2','F1-3','F2-1','F2-2','G1-1','G1-2','G1-3')
You might want to use some more selective set of colours instead of the random ones generated here. And you'll have to adjust the text in the legend too.
HTH

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by