How to plot loop iteration in same figure

Hello all, I need to plot my gama for different instance loop iteration. This means I should save data for all loop and plot all in the same figure. Please see the following code. clear; gama=[1.20, 2.90 ,3.1] for gama ? end How to compute ?

7 Comments

madhan ravi
madhan ravi on 21 Oct 2018
Edited: madhan ravi on 21 Oct 2018
What have we tried so far?
clear;
% p0=2;Input power in (w)
p0=6
% p0=5
T1=5;% Pulse with
T0=T1/2/sqrt(log(2));%(FWHM)
h=1;%²½³¤
% gama=2.90
% for gama=[1.20, 2.90 ,3.1]
% gama = 2:0.1:10;gama=[1.20,2.90,3.1,4.4,5.3];
% gama=[1.20,2.90,3.1,4.4,5.3];
for gama=[1.20,2.90,3.1,4.4,5.3];
% do operations
% Plot();
hold on;
end
beta=0;%%second order dispersion
T=0.3;% total pulse with (TFWHM)
N=285;% Soliton order
n=-N/2:(N/2-1);
t=n.*T;% order
uszgz0=exp(-t.^2./(2*T0^2));
usz=exp(-t.^2./(2*T0^2));% Dispersion
A0=sqrt(p0)*uszgz0;% Amplitude
dfait=1/T0*h*gama*p0*(t/T0).*exp(-(t/T0).^2);
for k=1:10
u0=usz;
usz=abs(u0).*exp(-h*j*gama*p0*(abs(u0).^2));%first order with Chirp parameter
fusz=fftshift(fft(usz));% Spectrum
end
f=(0:(N-1))./(N*T)*1e3-1/(2*T)*1e3;
Az=usz*sqrt(p0);%
% figure (1)
% plot(t,A0.^2,'g');
hold on
figure (100)
plot(t,A0.^2,'b-o',t,A0.^2,'r.-','Linewidth',6)
title('Pulse Evolution');
xlabel('Time (ps) ');
ylabel('Power(W)');
set(gca,'FontSize',24);
legend ('Matlab IN','After SPM');
legend boxon
grid on;
xlim([-10,10])
% figure (2);
% plot(t,(abs(Az).^2),'b');%´«ÊäLºóÐźŹ¦Âʲ¨ÐÎ*2/N
% plot (3)
% plot(f,fftshift((T*abs(fft(uszgz0))).^2)/N,'g');%³õʼÐźŹ¦ÂÊÆ×
% figure (4)
% plot(f,(T*(abs(fusz))).^2/N,'b','Linewidth',4);%´«²¥100kmºóµÄÐźŹ¦ÂÊÆ×
% title('Pulse Evolution');
% xlabel('Wavelenght(u) ');
% ylabel('Power (W)');
% set(gca,'FontSize',24);
% legend ('Wavelenght');
% % legend boxon
% % grid on;
% % xlim([-800,800])% figure (5)
% plot(t/T0,dfait,'g');%ÀíÂÛßúà±
% figure (6)
% plot(t(2:N)/T0,diff(angle(usz))/T,'b');%ʵ¼Ê·ÂÕæßúà±
figure (7)
plot(f,fftshift((T*abs(fft(uszgz0))).^2)/N,'g','Linewidth',6)
hold on
plot(f,(T*(abs(fusz))).^2/N,'b','Linewidth',4)
title('Pulse Evolution');
xlabel(' Frequency (GHz)');
ylabel('Power (W)');
set(gca,'FontSize',24);
legend ('Matlab IN','After SPM');
% legend ('1m','2m','3m','4m','5m');
legend boxon
grid on;
xlim([-900,900])
grid on
end
end
Not clear. plot(x,y) or plot(y)
for gama=[1.20,2.90,3.1,4.4,5.3]; % i dont understand this line
gama is your x or y?
gama is the defined number ex: if gama= 1.20, or 2.90 or 3.1. or 4.4. 5.3 it is possible to make a loop with it? because every time I change manually it gives different values but the result is overwritten. So I need to make a loop which will save data to be plotted together. Please see the next figure attached.
Your gamma don't have any pattern
try this way.
gamma = [1.20 2.9 3.1 4.4 5.3]
%your for loop
for i=1:1:length(gamma)
end
inside the for loop, change all gamma to
gamma(i)
let me know if it is working for you. So, i post it in answer for your acceptance
Dear Kevin, I don't get the last part how to change all gama (i)inside the loop? could you please make an example here. thank you in advance. But the output figure is overwritten at the end. there not different information in the same plot.
clear;
p0=6
T1=5;
T0=T1/2/sqrt(log(2));
h=1;
gama=[1.20,2.90,3.1,4.4,5.3];
for i=1:1:length(gama)
beta=0;
T=0.3;% total pulse with (TFWHM)
N=285;% Soliton order
n=-N/2:(N/2-1);
t=n.*T;% order
uszgz0=exp(-t.^2./(2*T0^2));
usz=exp(-t.^2./(2*T0^2));% Dispersion
A0=sqrt(p0)*uszgz0;% Amplitude
dfait=1/T0*h*gama(i)*p0*(t/T0).*exp(-(t/T0).^2);
%for k=1:10
u0=usz;
usz=abs(u0).*exp(-h*j*gama(i)*p0*(abs(u0).^2));%first order with Chirp parameter
fusz=fftshift(fft(usz));% Spectrum
end
f=(0:(N-1))./(N*T)*1e3-1/(2*T)*1e3;
Az=usz*sqrt(p0);%
figure(1)
hold on
plot(t,A0.^2,'b-o',t,A0.^2,'r.-','Linewidth',6)
title('Pulse Evolution');
xlabel('Time (ps) ');
ylabel('Power(W)');
set(gca,'FontSize',24);
legend ('Matlab IN','After SPM');
legend boxon
grid on;
xlim([-10,10];
hold off
figure(2)
hold on
plot(f,fftshift((T*abs(fft(uszgz0))).^2)/N,'g','Linewidth',6)
hold on
plot(f,(T*(abs(fusz))).^2/N,'b','Linewidth',4)
title('Pulse Evolution');
xlabel(' Frequency (GHz)');
ylabel('Power (W)');
set(gca,'FontSize',24);
legend ('Matlab IN','After SPM');
legend boxon
grid on;
xlim([-900,900])
grid on
hold off
end
%end

Sign in to comment.

 Accepted Answer

Check the below pseudo code, which plots the same function for different values and the function is plotted in a loop. Understand the below code and apply it to your case.
x = linspace(0,2*pi) ;
A = 1:10 ;
figure
hold on
for i = 1:length(A)
y = A(i)*sin(x) ;
plot(x,y)
end

More Answers (0)

Asked:

on 21 Oct 2018

Commented:

on 25 Oct 2018

Community Treasure Hunt

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

Start Hunting!