MATLAB: I am getting this error- “Vectors must be the same lengths.”. Also please explain why its showing this error as I am plotting a funtion with ‘x’ as variable with defined vector.

errorMATLABvectors must be the same lengths

clear all
rr=0.99997622; % reflectivity of the cavity
r=sqrt(rr);
finesse=pi.*sqrt(rr)./(1-rr); % finesse of the cavity
fsr=10^9;
x=-9e4:10:9e4; % frequnecy detuning
z=x./fsr;
FWHM=fsr./finesse;
mod=40e6; % modulation frequency to the laser
m=mod./fsr;
g=r.*(exp(((x./fsr).*(2.*pi)).*1i)-1)./(1-(r.^2).*exp(((x./fsr).*(2.*pi)).*1i));
abg=abs(g);
sg=power(abg,2);
dfg=diff(sg);
y=m.*dfg;
plot(x,y)

Best Answer

Jayash, with the diff command you compute the difference between elements in a vector thereby reducing the number of elements by one. Use instead
plot(x(2:end),y)