# [Beginner] Plotting 3D graphs, but "Error using plot3 Vectors must be the same length."

조회 수: 3 (최근 30일)
ABCDEFG HIJKLMN 2021년 10월 21일
편집: Cris LaPierre 2021년 10월 25일
i want to plot two 3D functions (Z_1 and Z_2) in one graph, where
X = linspace(x_1_min,x_1_max);
Y = linspace(x_2_min,x_2_max);
and I am calculating Z_1 and Z_2 with
for i=1:1:100
for j=1:1:100
Z_sigma_1 = ((1 + X(i)) * sqrt(1 + X(i)^2)) / (2 * sqrt(2) * X(i) * Y(j));
Z_sigma_2 = ((X(i) - 1) * sqrt(1 + X(i)^2)) / (2 * sqrt(2) * X(i) * Y(j));
Z_1 = [Z_1 Z_sigma_1];
Z_2 = [Z_2 Z_sigma_2];
end
end
I get the error that the vectors must be the same size, but how could I ever not have more elements in Z_1 and Z_2 than in X and Y? The number of elements in Z_1 and Z_2 is the same and would always be the product of the number of elements in X and Y! What I am doing wrong?
Thanks in advance for the help!
The figure is plotted as follows
figure (1)
grid on
hold on
plot3(X,Y,Z_1, '-r','DisplayName','sigma1');
plot3(X,Y,Z_2, '-m','DisplayName','sigma2');
xlabel('x_1')
ylabel('x_2')
zlabel('sigma')
legend show

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

### 답변 (1개)

Cris LaPierre 2021년 10월 21일
편집: Cris LaPierre 2021년 10월 21일
You appear to have left out the code that initializes Z_1 and Z_2. Depending how you initialize them, Z_1 and Z_2 could have more elements than X and Y.
To ensure Z_1 and Z_2 are the size you expect, try the following.
Z_1 = [];
Z_2 = [];
for i=1:1:100
for j=1:1:100
Z_sigma_1 = ((1 + X(i)) * sqrt(1 + X(i)^2)) / (2 * sqrt(2) * X(i) * Y(j));
Z_sigma_2 = ((X(i) - 1) * sqrt(1 + X(i)^2)) / (2 * sqrt(2) * X(i) * Y(j));
Z_1 = [Z_1 Z_sigma_1];
Z_2 = [Z_2 Z_sigma_2];
end
end
##### 댓글 수: 2없음 표시없음 숨기기
ABCDEFG HIJKLMN 2021년 10월 25일
But this doesn't stop the vectors Z_1 and Z_2 to have more elements than X and Y. That's the problem I have that I don't know how to fix.
Cris LaPierre 2021년 10월 25일
편집: Cris LaPierre 2021년 10월 25일
It starts them out as empty. The only reason I see that they would now not be the correct length is if your equations for Z_sigma_1 or Z_sigma_2 are returning more than one value each time. Please check there.

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

### 카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by