How to plot z = x^2 * y

x = linspace(-10, 10, 50);
y = linspace(-10, 10, 50);
[X,Y] = meshgrid(x,y);
Z = X.^2. * Y;
figure(1)
surf(X, Y, Z)
Don't know why the output just shows a flat surface

Answers (1)

You have an extra space between "." and "*"
Z = X.^2. * Y;
% ^ space breaks up the .* operator
That causes Z to be the matrix product (*) of X.^2. and Y, which happens to result in a matrix containing one unique value very close to 0:
x = linspace(-10, 10, 50);
y = linspace(-10, 10, 50);
[X,Y] = meshgrid(x,y);
Z = X.^2. * Y;
unique(Z)
ans = -5.6843e-13
Remove the extra space, and you'll get Z to be the element-wise product (.*) of X.^2 and Y, as intended:
Z = X.^2.* Y;
surf(X, Y, Z)

Categories

Tags

Asked:

on 17 Feb 2023

Answered:

on 17 Feb 2023

Community Treasure Hunt

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

Start Hunting!