Error: Vectors must be the same length.
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mick
am 3 Dez. 2017
Bearbeitet: Walter Roberson
am 3 Dez. 2017
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 Kommentare
Stephen23
am 3 Dez. 2017
@Mihnea Capat: I asked how you defined t. What is this:
t = ... ?
Akzeptierte Antwort
Star Strider
am 3 Dez. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Subplots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!