Error using plot vectors must be the same length

33 views (last 30 days)
that my code i get a erro using plot
f=(0:10);
f0=5*10^9;
k1=(100*pi/3);
k2=(sqrt(3)*k1);
c=(3*10^8);
t1=(1/f0);
t2=(1/5*10^9);
w1=(2*pi*f0);
w2=(2*pi*f);
z1=(0.75:0.015);
z2=(0.0075:0.03);
rin1=-0.71;
rin2=0.094;
%x1=rin1*cos*(w1*t1+k1*z1);
%x2=rin1*cos*(w2*t1+k2*z2);
%x3=rin2*cos*(w1*t1+k1*z1);
%x4=rin2*cos*(w2*t2+k2*z2);
y1=-0.71*cos(w1*t1+k1*z1);
y2=-0.71*cos(w2*t2+k2*z2);
y3=0.094*cos(w1*t1+k1*z1);
y4=0.094*cos(w2*t2+k2*z2);
%rin=abs(y);
plot(f,y1);
plot(f,y2);
plot(f,y3);
plot(f,y4);
%T-Γ12=1;
m=1*cos(w1*t1-k1*z1);
m1=1*cos(w2*t2-k2*z2);
%rin=abs(m);
plot(f,m);
xlabel("Frequency",'fontsize',5);
ylabel("|Γin|",'FontSize', 5);
title("Input reflection coefficient as a function of frequency","fontsize",7.5);
grid on;
since i run this code i get the erro:Error using plot
Vectors must be the same length.
Error in untitled3 (line 23)
plot(f,y1);

Answers (3)

Jan
Jan on 12 Jan 2023
The error message is clear. Use the debugger to examine the reasons:
dbstop if error
Type this in the command window an run the code again. When Matlab stops, check the sizes of f and y1.

Torsten
Torsten on 12 Jan 2023
You can easily test what the error is.
A plot command
plot(x,y)
can only be successful if:
x is a column vector and y is a matrix that has the same number of rows as x has
x is a row vector and y is a matrix that has the same number of columns as x has.
Thus insert
size(x)
size(y)
in your code before plotting and check if one of the conditions for successful plotting is satisfied.

Star Strider
Star Strider on 12 Jan 2023
Note that ‘z1’ is defined with a +1 default increnent and with a starting value that is greater than its ending value. This means that it is already satisfied at the outset, and produces an empty value, not a vector.
The ‘z2’ assignment has a different problem, that being that it has the same +1 default increment, so it is satisfied at the first value (since the end value is less than +1 from the start value) and produces a scalar result, not a vector.
Consider using the linspace function to create all the vectors with the same desired lengths.
.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!