MATLAB Answers

How to create a surface plot of a function that uses colon operators?

1 view (last 30 days)
Rahul Bhadani
Rahul Bhadani on 7 Apr 2021
Commented: Rahul Bhadani on 7 Apr 2021
I have a following function:
function p = dist(n, theta, M, Ns, Nb, G, kappa)
Ntheta = 55;
logterm1 = sum( log(1: n + M - 1) ) - sum( log (1: M - 1)) - sum( log (1:n));
logterm2 = n.*(log (Ntheta) - log(1 + Ntheta));
logterm3 = M*log(1 + Ntheta);
total = logterm1 + logterm2 - logterm3;
p = exp(total);
end
function PE = Error(x, y)
p0 = x;
p1 = 1-x;
Ns = 1e-2;
Nb =10;
kappa = 1e-2;
G = 1.01;
M = 1e3;
Ntheta0 = 150;
Ntheta_pi = 33;
sigma0 = sqrt( Ntheta0.*( Ntheta0 + 1));
sigma_pi = sqrt( Ntheta_pi.*( Ntheta_pi + 1));
narray = 1:floor(y);
PX0LessthanTh = 0;
for na = narray
pp = dist(na, 0, M, Ns, Nb, G, kappa);
PX0LessthanTh = PX0LessthanTh + pp;
end
PXpiLessthanTh = 0;
for na = narray
pp = dist(na, pi, M, Ns, Nb, G, kappa);
PXpiLessthanTh = PXpiLessthanTh + pp;
end
PE = p0.*PX0LessthanTh + p1.*(1 - PXpiLessthanTh);
end
I am looking to create a surface plot of Error with respect to x and y. However, I because I am using a statement narray = 1:floor(y); I am getting an error:
The following error was reported evaluating the function in FunctionLine
update: Colon operands must be real scalars.
How can I modify my Error function above so that I can use 3D functions like surf.

Answers (1)

VBBV
VBBV on 7 Apr 2021
%if true
log(1: n + M - 1)
Check if n+M-1 is positive or negative values?
  1 Comment
Rahul Bhadani
Rahul Bhadani on 7 Apr 2021
I will cross check the point you raised here.
However, for my case, n is always less than M.
I think, 1: n or any other operator requires n to be scalar but surf function sends vector. So if y is vector,
then in Error function,
the statement
narray = 1:floor(y);
won't work.
I think my error spawns from narray = 1:floor(y) that statement. But I might be wrong. I am going to recheck my values.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!