vectors must be the same lengths error with subplot

조회 수: 1 (최근 30일)
Anna
Anna 2014년 9월 11일
댓글: Anna 2014년 9월 11일
I am new to MatLab and am unsure what is causing the error in my code. I get the vectors must be the same lengths error message. The code is below.
y1=0.5.*x.^2 x1=-5:0.25:5 y2=-x x2=0:2:100 figure subplot(2,1,1); plot(x1,y1) subplot(2,1,2); plot(x2,y2)

채택된 답변

Julia
Julia 2014년 9월 11일
Hi,
What size has the vector x? y1 and y2 have the same size as x. But the sizes of x1 and x2 are not the same:
length(x1) = 41
length(x2) = 51
But since y1 and y2 have the same size, x1 and x2 need to have the same size as them.
This could work if
length(x)=41 :
x1=-5:0.25:5
x2=0:2:80
length(x1) = 41
length(x2) = 41
  댓글 수: 1
Anna
Anna 2014년 9월 11일
The assignment required x2 go up to 100 but using x2=0:2.5:100 and keeping x1 the same accomplishes this as well. Thanks for the explanation!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mischa Kim
Mischa Kim 2014년 9월 11일
Anna, you need to define x1 and x2 first before you can use the variables to compute y1 and y2. Use something like
x1 = -5:0.25:5;
y1 = 0.5.*x1.^2;
x2 = 0:2:100;
y2 = -x2;
figure
subplot(2,1,1);
plot(x1,y1)
subplot(2,1,2);
plot(x2,y2)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by