How to create a surface plot of a function that uses colon operators?
Show older comments
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 (2)
Harshavardhan
on 29 Apr 2025
The current “Error” function looks to be designed for scalar inputs. When used with “meshgrid” for surface plots, MATLAB passes arrays, leading to issues with expressions like “narray = 1:floor(y);”.
To resolve this, use “arrayfun” to evaluate the function for each (x,y) pair separately as shown below.
%Example x,y arrays
x = linspace(0, 1, 30);
y = linspace(1, 20, 30);
[X, Y] = meshgrid(x, y); %Make the X,Y grid
Z = arrayfun(@(xi, yi) Error(xi, yi), X, Y);% Using “arrayfun” to calculate Z(error)
figure
surf(X, Y, Z);
xlabel('x'); ylabel('y'); zlabel('Error');
title('Surface plot of Error(x, y)');
colorbar;
For more information on “arrayfun” and “meshgrid”, type the following commands in a MATLAB command window
doc meshgrid
doc arrayfun
Hope this helps.
@Rahul Bhadani It seems you have not defined the y variable. However, If its an external input value to the function Error then you need to ensure that its a scalar. In your case its doesnt seem to be scalar but rather a vector or a matrix. Since you have a for loop inside the function, the colon operator does not parse the vector inputs to a for loop index The solution would be to reshape the matrix and use numel instead of floor as below to get rid of the warning/error message
x = linspace(0, 1, 30);
y = linspace(1, 20, 30);
[X, Y] = meshgrid(x, y); %Make the X,Y grid
PE = Error(X,Y);
figure
surf(X,Y,PE)
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:numel(y)
y = reshape(y,1,[]) %
PX0LessthanTh = 0;
for na = narray
pp = dist(y(na), 0, M, Ns, Nb, G, kappa);
% ^^
PX0LessthanTh = PX0LessthanTh + pp;
end
PXpiLessthanTh = 0;
for na = narray
pp = dist(y(na), pi, M, Ns, Nb, G, kappa);
% ^^
PXpiLessthanTh = PXpiLessthanTh + pp;
end
PE = p0.*PX0LessthanTh + p1.*(1 - PXpiLessthanTh);
end
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
Categories
Find more on Surface and Mesh Plots 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!