Error using surf X, Y, Z, and C cannot be complex error

8 visualizaciones (últimos 30 días)
Dikra dikra
Dikra dikra el 14 de En. de 2018
Editada: Carlos Guerrero García el 23 de Nov. de 2022
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)

Respuesta aceptada

John BG
John BG el 15 de En. de 2018
Hi
The problem is not in solving
x = 0:0.8:pi;
A = sin(x).^(1/3)
B = cos(x).^(1/3)
the operator
.^
solves for either real or complex.
The problem lays in the fact that surf doesn't take in complex values.
To solve the long expression of H, since you are dealing with complex numbers, you have to plot 2 surfs, not one: Real and Imaginary, or Modulus and Phase
clc
theta5=[0:0.1:pi];
theta4=[0:0.1:pi];
[X,Y]=meshgrid(theta4,theta5);
H=(((((cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).*(cos(X).^2.*cos(Y).^2-cos(X).^2*sin(X).^2+cos(Y).^2.*sin(X).^2+cos(X).^2.*cos(Y).^4.*sin(X).^2+2*cos(X).^2.*sin(X).^2.*sin(Y).^2-cos(Y).^2.*sin(X).^2.*sin(Y).^2-cos(X).^2.*sin(X).^2.*sin(Y).^4))./6+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^3./27+(cos(X).^2.*cos(Y).^2.*sin(X).^2)./2-(cos(X).^2.*cos(Y).^6.*sin(X).^2)./2-(cos(X).^2.*cos(Y).^2.*sin(X).^2.*sin(Y).^2)./2-cos(X).^2.*cos(Y).^4.*sin(X).^2.*sin(Y).^2).^2-((cos(X).^2.*cos(Y).^2)./3-(cos(X).^2.*sin(X).^2)./3+(cos(Y).^2.*sin(X).^2)./3+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(X).^2+sin(X).^2.*sin(Y).^2).^2./9+(cos(X).^2*cos(Y).^4.*sin(X).^2)./3+(2.*cos(X).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(Y).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(X).^2.*sin(X).^2.*sin(Y).^4)./3).^3).^(1/2)-((cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).*(cos(X).^2.*cos(Y).^2-cos(X).^2.*sin(X).^2+cos(Y).^2.*sin(X).^2+cos(X).^2.*cos(Y).^4.*sin(X).^2+2.*cos(X).^2.*sin(X).^2.*sin(Y).^2-cos(Y).^2.*sin(X).^2.*sin(Y).^2-cos(X).^2.*sin(X).^2.*sin(Y).^4))./6-(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^3./27-(cos(X).^2.*cos(Y).^2.*sin(X).^2)./2+(cos(X).^2.*cos(Y).^6.*sin(X).^2)./2+(cos(X).^2.*cos(Y).^2.*sin(X).^2.*sin(Y).^2)./2+cos(X).^2.*cos(Y).^4.*sin(X).^2.*sin(Y).^2).^(1/3)+cos(X).^2./3-cos(Y).^2./3+sin(X).^2./3+((cos(X).^2.*cos(Y).^2)./3-(cos(X).^2.*sin(X).^2)./3+(cos(Y).^2.*sin(X).^2)./3+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^2./9+(cos(X).^2.*cos(Y).^4.*sin(X).^2)./3+(2*cos(X).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(Y).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(X).^2.*sin(X).^2.*sin(Y).^4)./3)./(((((cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).*(cos(X).^2.*cos(Y).^2-cos(X).^2.*sin(X).^2+cos(Y).^2.*sin(X).^2+cos(X).^2.*cos(Y).^4.*sin(X).^2+2.*cos(X).^2.*sin(X).^2.*sin(Y).^2-cos(Y).^2.*sin(X).^2.*sin(X).^2-cos(X).^2.*sin(X).^2.*sin(Y).^4))./6+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^3./27+(cos(X).^2.*cos(Y).^2.*sin(X).^2)./2-(cos(X).^2.*cos(Y).^6.*sin(X).^2)./2-(cos(X).^2.*cos(Y).^2.*sin(X).^2.*sin(Y).^2)./2-cos(X).^2.*cos(Y).^4.*sin(X).^2.*sin(Y).^2).^2-((cos(X).^2.*cos(Y).^2)./3-(cos(X).^2.*sin(X).^2)./3+(cos(Y).^2.*sin(X).^2)./3+(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^2./9+(cos(X).^2.*cos(Y).^4.*sin(X).^2)./3+(2.*cos(X).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(Y).^2.*sin(X).^2.*sin(Y).^2)./3-(cos(X).^2.*sin(X).^2.*sin(Y).^4)./3).^3).^(1/2)-((cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).*(cos(X).^2.*cos(Y).^2-cos(X).^2.*sin(X).^2+cos(Y).^2.*sin(X)^2+cos(X).^2.*cos(Y).^4.*sin(X).^2+2.*cos(X).^2.*sin(X).^2.*sin(Y).^2-cos(Y).^2.*sin(X).^2.*sin(Y).^2-cos(X).^2.*sin(X).^2.*sin(Y).^4))./6-(cos(Y).^2-cos(X).^2-sin(X).^2+cos(X).^2.*sin(Y).^2+sin(X).^2.*sin(Y).^2).^3./27-(cos(X).^2.*cos(Y).^2.*sin(X).^2)./2+(cos(X).^2.*cos(Y).^6.*sin(X).^2)./2+(cos(X).^2.*cos(Y).^2.*sin(X).^2.*sin(Y).^2)./2+cos(X).^2.*cos(Y).^4.*sin(X).^2.*sin(Y).^2).^(1/3)-(cos(X).^2.*sin(Y).^2)./3-(sin(X).^2.*sin(Y).^2)./3;
figure(1);surf(X,Y,abs(H))
figure(2);surf(X,Y,angle(H))
.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
  2 comentarios
John BG
John BG el 15 de En. de 2018
Editada: John BG el 15 de En. de 2018
Thanks Dikra
if you capture the handles returned by function surf, for instance with:
figure(1);h_abs=surf(X,Y,abs(H));
figure(2);h_ang=surf(X,Y,angle(H));
then you can read the resulting surface point directly
Habs_x=h_abs.XData
Habs_y=h_abs.YData
Habas_z=h_abs.ZData
Hang_x=h_ang.XData
Hang_y=h_ang.YData
Hang_z=h_ang.ZData
also it is possible to change surface and edges transparency. If the surface is too dark, sometimes it's because of the high density of edges really close each other. Then the common practice is:
h_abs.EdgeColor='none' % horse seat
h_ang.EdgeColor='none' % pickeman helmet
Dikra dikra
Dikra dikra el 16 de En. de 2018
Editada: Dikra dikra el 16 de En. de 2018
Thank you. You are right. This helped me

Iniciar sesión para comentar.

Más respuestas (1)

Star Strider
Star Strider el 14 de En. de 2018
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 comentarios
Star Strider
Star Strider el 15 de En. de 2018
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
Carlos Guerrero García el 23 de Nov. de 2022
Editada: Carlos Guerrero García el 23 de Nov. de 2022
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)
A = 1×4
0 0.8952 0.9999 0.8774
B = cos(x).^(1/3)
B =
1.0000 + 0.0000i 0.8865 + 0.0000i 0.1540 + 0.2667i 0.4517 + 0.7824i
is
x = 0:0.8:pi;
A = nthroot(sin(x),3)
A = 1×4
0 0.8952 0.9999 0.8774
B = nthroot(cos(x),3)
B = 1×4
1.0000 0.8865 -0.3079 -0.9034
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

Iniciar sesión para comentar.

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by