Error using surf X, Y, Z, and C cannot be complex error
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Dikra dikra
il 14 Gen 2018
Modificato: Carlos Guerrero García
il 23 Nov 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)
0 Commenti
Risposta accettata
John BG
il 15 Gen 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 Commenti
John BG
il 15 Gen 2018
Modificato: John BG
il 15 Gen 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
Più risposte (1)
Star Strider
il 14 Gen 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 Commenti
Star Strider
il 15 Gen 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
il 23 Nov 2022
Modificato: Carlos Guerrero García
il 23 Nov 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)
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
Vedere anche
Categorie
Scopri di più su Surface and Mesh Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!