How to plot z = x^2 * y
Show older comments
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)
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
Find more on Creating and Concatenating Matrices 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!