MATLAB: What is wrong I have been getting errors that it is wrong

MATLABmatlab functionsurface plot

function output = impact(x,y)
output = sin(sqrt((x^2)+(y^2)))/sqrt((x^2)+(y^2));
x = -10:1:10;
y = -10:1:10;
z = output;
surfc(x,y,z) %Surface Plot
contour(x,y,z) %Contour lines
colormap(hsv)
Grid off
title('Impact Surface Plot')
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
end

Best Answer

Try something like this:
outputfcn = @(x,y) sin(sqrt((x.^2)+(y.^2)))./sqrt((x.^2)+(y.^2));
x = linspace(-10, 10, 25);
y = linspace(-10, 10, 25);
[X,Y] = ndgrid(x,y);
z = outputfcn(X,Y);
figure
surfc(x,y,z) %Surface Plot
hold on
contour3(x,y,z) %Contour lines
hold off
colormap(hsv)
grid off
title('Impact Surface Plot')
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
It is necessary to use element-wise opearations in the function, and the ndgrid or meshgrid functions to create the correct matrices, and the hold function to plot the surf and countour3 plots on the same axes. See Array vs. Matrix Operations for more information.