Error: Vectors must be the same length.

Hi guys ! I'm getting this error when trying to plot y3: Vectors must be the same length. I am very new to matlab and I understand this is a common problem but couldn't figure out how to change my code so it works just by looking at other people's more complicated situations of the same error.
t1=0:2.5/251:2.5;
t2=0:0.25/251:0.25;
t3=0:10/1001:10;
y1=4*cos(4.8*pi*t1+3*pi/2)+1.5*cos(12*pi*t1+pi/4);
y2=-5*cos(8*pi*t2+0);
y3=y1+y2;
subplot(311);
plot(t1,y1);
subplot(312);
plot(t2,y2);
subplot(313);
plot(t3,y3);
Error using plot
Vectors must be the same length.
Error in lab3ex2 (line 15)
plot(t3,y3);
Anyone can tell me what to do exactly so it works? Thank you in advance !

4 commentaires

Stephen23
Stephen23 le 3 Déc 2017
Modifié(e) : Stephen23 le 3 Déc 2017
Whatever t is (you don't show us) it must have a different length to t1, t2, and/or t3.
How is t defined?
Mick
Mick le 3 Déc 2017
t symbolizes time for the function y(t) where the function describes a signal. t1,t2,t3 are time spans for each y1(t),y2(t),y3(t).
@Mihnea Capat: I asked how you defined t. What is this:
t = ... ?
Mick
Mick le 3 Déc 2017
I think that inside y1 there should be t1 instead of t and inside y2 there should be t2 instead of t. My apologies. Forget about t

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 3 Déc 2017
The easy solution is to use the linspace function to make them all the same lengths:
t1 = linspace(0, 2.5, 250);
t2 = linspace(0, 0.25, 250);
t3 = linspace(0, 10, 250);
y1 = @(t) 4*cos(4.8*pi*t+3*pi/2)+1.5*cos(12*pi*t+pi/4);
y2 = @(t) -5*cos(8*pi*t+0);
y3 = y1(t1) + y2(t2);
subplot(311); plot(t1,y1(t1))
subplot(312); plot(t2,y2(t2))
subplot(313); plot(t3,y3)
I created anonymous functions from your equations to make this even easier.

6 commentaires

Mick
Mick le 3 Déc 2017
Now it works and I have all functions graphed. The only thing I'm not sure about is if my code will pass the tests of the assignment because the assignment says "Both The time spans t1 and t2 should contain 251 samples, while the time span t3 should contain 1001 samples" (which is why I divide by 251 and 1001).
Star Strider
Star Strider le 3 Déc 2017
That is new information.
What precisely does the assignment require?
Note that for known homework problems, we give hints, not complete code.
Mick
Mick le 3 Déc 2017
Modifié(e) : Mick le 3 Déc 2017
The assignment gives me 2 graphs and asks me to create y1(t) and y2(t) from the plot/spectral lines but also y3(t) which is = y1+y2. After that: Create a script that creates a subplot of three plots underneath each eachother. The first plot should contain y1 on the specified time domain [0, 2.5]. The second plot should contain y2 on the time domain as in the figure ( t2 ). The last plot should contain y3 on the time domain( t3 ) [0, ?] where exactly 2 full periods of the function y3 are visible (and I figured this must be 10). Use the solution template provided. t1 should contain 251 samples. t2 should also contain 251 samples. t3 should contain 1001 samples.
Sorry. I am lost. I have no idea what it wants.
The only way I can parse that is to do this:
t1 = linspace(0, 2.5, 251);
t2 = linspace(0, 0.25, 251);
t3 = linspace(0, 10, 1001);
y1 = @(t) 4*cos(4.8*pi*t+3*pi/2)+1.5*cos(12*pi*t+pi/4);
y2 = @(t) -5*cos(8*pi*t+0);
y3 = y1(t3) + y2(t3);
subplot(311); plot(t1,y1(t1))
subplot(312); plot(t2,y2(t2))
subplot(313); plot(t3,y3)
Mick
Mick le 3 Déc 2017
I managed to solve it by writing y3=4*cos(4.8*pi*t3+pi*3/2)+1.5*cos(12*pi*t3+pi/4)+5*-cos(t3*8*pi); instead of y3=y2+y1 and now it works. Your code gives exactly the same output as the one I have now. Thank you a lot though because now I can see different ways of doing it and learning about linspace and anonymous functions. Have a good day !
Star Strider
Star Strider le 3 Déc 2017
My pleasure.
You, too!

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by