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

41 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 25 Mar 2017
Edited: Steven Lord on 25 Mar 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.]

More Answers (2)

Star Strider
Star Strider on 25 Mar 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 on 25 Mar 2017
Thank you very much Jan Simon and Star Strider for your answer

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!