lm having a problem with code for dispersion when ever l try to run, it say the variable 'dispersion' in line 20 is changing the size of every loop so how do l solve this

3 visualizzazioni (ultimi 30 giorni)
clc;
clear all;
close all;
format long;
wl=1:0.05:2;
wl1=wl*1e-6;
vc=299792458;
w=2*pi*(vc./wl1);
Wavelength=wl*1e-6;
Fresult={'ofc_y.txt', 'ofc_x.txt'};
for i=1:2
[neff,beta,alpha,alphadB]=textscan(Fresult{i},....
'%f %f %f %f');
beta(i,:)=beta;
alpha(i,:)=alpha;
alphadB(i,:)=alphadB;
b1=diff(beta(i,:))./diff(w);
Dispersion(i,:)=((diff(b1)./diff(wl1(1:end-1))));
end
figure(1)
plot(wl1*1e6,neffr(1,:),'-r',wl1*1e6,neffr(2,:),'-b');
legend('y-mode','x-mode','Location','best');
xlabel('Wavelenght(\mu m)');
ylabel('Effective Index');
title('Effective Index vs wavelength');
figure(2)
plot(wl1*1e6,betar(1,:),'-r',wl1*1e6,betar(2,:),'k');
legend('y-mode','x-mode','Location','best');
xlabel('Wavelenght(\mu m)');
ylabel('beta');
title('beta vs wavelength');
figure(3)
plot(wl*1e6,alphadBr(1,:),'-r',wl*1e6,alphadBr(2,:),'k');
legend('y-mode','x-mode','Location','best');
xlabel('Wavelenght(\mu m)');
ylabel('alphadB');
title('alphadB vs wavelength');
figure(4)
plot(wl1(1:end-2)*1e6,Dispersion(1,:),'-r',wl1(1:end-2)*1e6,Dispersion(2,:),'k');
legend('y-mode','x-mode','Location','best');
xlabel('Wavelenght(\mu m)');
ylabel('Dispersion');
title('Dispersion vs wavelength');

Risposta accettata

Abderrahim. B
Abderrahim. B il 23 Lug 2022
Hi!
I beleive that s only a warning, to avoid it preallocate for Disperssion variable.
Demo:
Without preallocation :
A = randi(10,20,20) ;
for ii = 1:20
B(ii, :) = A(ii, :) ;
end
B
B = 20×20
10 1 9 9 5 1 7 4 1 4 1 2 7 5 7 8 3 4 5 10 5 2 3 5 4 9 9 8 10 9 8 4 3 2 9 8 2 1 7 4 2 5 6 3 3 5 2 9 7 2 10 9 6 8 2 9 1 4 6 7 8 3 10 8 7 8 5 2 3 9 6 7 7 1 2 9 8 6 3 10 3 1 7 3 4 3 1 5 9 10 6 7 6 1 5 6 1 9 10 5 10 8 7 10 1 8 9 7 4 8 8 3 1 8 1 9 8 1 10 2 10 3 8 8 6 4 2 7 2 9 3 8 1 9 7 4 5 7 6 2 8 7 5 3 10 2 1 4 9 8 1 3 4 4 3 7 9 9 3 6 8 1 4 3 3 3 1 7 5 2 3 1 8 8 2 10 2 4 2 5 1 4 2 4 1 9 4 10 8 3 7 9 1 7 7 10 1 2 6 7
With preallocation:
B = zeros(size(A)) ; % preallocation for B using function zeros
for ii = 1:20
B(ii, :) = A(ii, :) ;
end
B
B = 20×20
10 1 9 9 5 1 7 4 1 4 1 2 7 5 7 8 3 4 5 10 5 2 3 5 4 9 9 8 10 9 8 4 3 2 9 8 2 1 7 4 2 5 6 3 3 5 2 9 7 2 10 9 6 8 2 9 1 4 6 7 8 3 10 8 7 8 5 2 3 9 6 7 7 1 2 9 8 6 3 10 3 1 7 3 4 3 1 5 9 10 6 7 6 1 5 6 1 9 10 5 10 8 7 10 1 8 9 7 4 8 8 3 1 8 1 9 8 1 10 2 10 3 8 8 6 4 2 7 2 9 3 8 1 9 7 4 5 7 6 2 8 7 5 3 10 2 1 4 9 8 1 3 4 4 3 7 9 9 3 6 8 1 4 3 3 3 1 7 5 2 3 1 8 8 2 10 2 4 2 5 1 4 2 4 1 9 4 10 8 3 7 9 1 7 7 10 1 2 6 7
Hope this helps

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by