Error using surf X, Y, Z, and C cannot be complex error
이전 댓글 표시
i run this code, but get this message Error using surf X, Y, Z, and C cannot be complex error, any idea what is wrong?
clc
theta5=[0:0.2:pi];
theta4=[0:0.2:pi];
for M=1:length(theta4)
for N=1:length(theta5)
H(M,N)=cos(theta4(M))*sin(theta5(N));
end
end
[X,Y]=meshgrid(theta4,theta5);
surf(Y,X,H)
채택된 답변
추가 답변 (1개)
Star Strider
2018년 1월 14일
Your code runs for me without error.
A more efficient approach would be:
theta5=[0:0.2:pi];
theta4=[0:0.2:pi];
[X,Y]=meshgrid(theta4,theta5);
H = cos(X).*sin(Y);
surf(Y,X,H)
댓글 수: 10
Dikra dikra
2018년 1월 15일
편집: Walter Roberson
2018년 1월 15일
Star Strider
2018년 1월 15일
편집: Star Strider
2018년 1월 15일
You have at least one term where some combination of your trigonometric functions is raised to the (1/3) power.
To illustrate the effect, run this:
x = 0:0.8:pi;
A = sin(x).^(1/3)
B = cos(x).^(1/3)
Any negative value raised to a non-integer power will be complex.
You must resolve this, since I do not know what you are doing.
Hi
there's nothing wrong with the .^ operator applied to negative values, it will work ok and return complex results.
The one that doesn't take complex values is SURF thus returning error when attempting so.
Please check my answer, thanks in advance.
Regards
John BG
Star Strider
2018년 1월 15일
... with a completely irrelevant Comment!
Jan
2018년 1월 15일
The contentual repeating an irrelevant comment does not increase its relevance.
Star Strider
2018년 1월 15일
@Jan — Thank you!
Dikra dikra
2018년 1월 15일
Star Strider
2018년 1월 15일
Noted.
The accepted Answer does not solve your original problem. You need to address the reason your function is taking non-integer powers of negative results, and describe what you actually want to do.
Carlos Guerrero García
2022년 11월 23일
편집: Carlos Guerrero García
2022년 11월 23일
The line defining H is too long for me, but the error detected by Star Strider (my +1 for StarStrider) can be solved using the "nthroot" command, and so, my suggestion for
x = 0:0.8:pi;
A = sin(x).^(1/3)
B = cos(x).^(1/3)
is
x = 0:0.8:pi;
A = nthroot(sin(x),3)
B = nthroot(cos(x),3)
Perhaps the same idea will be useful, but perhaps another way to plot that surface must be consider (perhaps as an implicit surface, but I don't know about how the parametrization appears). Also note that real(cos(1.6)^(1/3)), imag(cos(1.6)^(1/3)) and nthroot(cos(1.6),3) are not the same
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

