error in using a function while using surf plot
Show older comments
I am trying to plot a 3D plot using surf function im MATLAB.
I am using a calc fucntion and it gives an error "Z must be a matrix and not a scalar value".
Please help me resolve this error.
function fx = calc(x, const)
A = const(1);
B = const(2);
r = sqrt(x(1)^2 + x(2)^2);
fx = A * cos(r)^2 * exp(-B * r);
end
% Constants
A = 2;
B = 0.3;
% Coordinates
x = [3; 2];
% Evaluate the function at the given point
fx = calc(x, [A, B]);
% Define the range of x and y values
x_range = linspace(-10, 10, 100);
y_range = linspace(-10, 10, 100);
% Create a grid of points
[X, Y] = meshgrid(x_range, y_range);
% Evaluate the function at each point of the grid
Z = calc([X(:) Y(:)]', [A, B]);
surf(X,Y,Z)
Accepted Answer
More Answers (0)
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!