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

59 visualizaciones (últimos 30 días)
vetri veeran
vetri veeran el 25 de Mzo. de 2017
Comentada: Jan el 25 de Mzo. de 2017
Hi all,
I want to draw 3D plot using surf command for vt = 0, -0.1...-0.5
I am getting an error as,
Error using surf X,Y, Z and C cannot be complex
My code is given below,
B = 1e-4; sigma_on = 0.45; x_on = 0.06; sigma_p = 4e-5; A = 1e-10; sigma_off = 0.013; x_off = 0.4; G_m = 0.025; rho = 1e-3; v_m = -0.5;
vt= 0:0.1:0.5;
t = 0.01:0.01:1;
for v = 1:length(vt)
v_m = -vt(v);
for x = 1:length(t)
G(x) = G_m*t(x)+ exp(sqrt(v_m));
f1(x) = A*sinh(v_m/sigma_off)*exp(-(x_off^2/t(x)^2));
f2(x) = B*sinh(v_m/sigma_on)*exp(-(t(x)^2/x_on^2));
f(x) = f1(x) + f2(x);
final(v,x)=log(f(x));
end
end;
surf(t,vt,final);
Can anyone help me.

Respuesta aceptada

Jan
Jan el 25 de Mzo. de 2017
Editada: Steven Lord el 25 de Mzo. de 2017
The error message is clear: "X,Y, Z and C cannot be complex". Obviously there are complex values. All we see is the failing code, but we cannot guess reliably, what its intention is. Therefore it is hard (or random) to suggest an improvement.
Maybe you want:
surf(t, vt, real(final));
or
surf(t, vt, abs(final));
or your calculations are wrong.
[SL: fixed typo in last code segment.]

Más respuestas (2)

Star Strider
Star Strider el 25 de Mzo. de 2017
The logarithm of a negative number will be complex. There are several ways to avoid that, the easiest being:
final(v,x)=log(abs(f(x)));
Note that the logarithm of 0 is -Inf, so you may want to trap ‘f(x)’ to prevent it from being zero as well. The easiest way to do that would be to have it equal NaN if it is zero, because NaN values will not plot. Consider setting the negative values of ‘f(x)’ to NaN as well, as one option.

vetri veeran
vetri veeran el 25 de Mzo. de 2017
Thank you very much Jan Simon and Star Strider for your answer

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by