Error: Z must be a matrix, not a scalar or vector and error in solution (line 12): surf(x,y,z);

6 views (last 30 days)
1 %Set the Domain of both x and y by setting the lower limit
2 a = 0 %Enter the lower limit of both x and y in the domain of the sketch
3 b = 0 %Enter the upper limit of both x and y in the domain of the sketch
4 h = 0.05 %Identify the increment
5
6 %Setup the graphing properties for both x and y and z.
7 [x,y] = meshgrid(a:h:b); %Create the domain of the graph
8 z = -x^4+x^2-y^2 %Encode the function in terms of x and y (Note that all operations between x's and y's are done with dotted operation
9
10 %First Plotting Space. Sketch the surface with the title "Surface Plot"
11 subplot(2,1,1); %Divide the plotting space into Four parts and use the first plotting area
12 surf(x,y,z); %Plot the given surface
13 title("Surface Plot"); %Set the title as "Surface Plot"
14
15 %Second Plotting Space. Sketch the Mesh with the title "Mesh Plot"
16 subplot(2,2,2); %First PLotting Space. Sketch the surface with the title "Surface Plot"
17 mesh(x,y,z); %Use the second plotting space
18 title("Mesh Plot"); %Mesh Plot the given surface
19 %Set the title as "Mesh Plot"
20
21 %Third Plotting Space. Sketch the Level Curves at various heights with the title "Level Curves"
22 subplot(2,2,3); %Use the third plotting space
23 contour3(x,y,z); %Plot the Level Curves of the given surface
24 title("Level Curves"); %Set the title as "Level Curves"
25 grid on %Set Grid On
26
27 %Fourth Plotting Space. Sketch the Contour Map with the title "Contour Plot"
28 subplot(2,2,4); %Use the fourth plotting space
29 contour(x,y,z); %Plot the Level Curvesof the given surface
30 title("Contour Plot"); %Set the title as "Contour Plot"
31 axis equal %Set Grid On
32 grid on %Set the Axes to have the same scale
33
34 %Start Using Rotate 3D Tools to rotate the solids
Error:
Error using surf
Z must be a matrix, not a scalar or vector.
Error in solution (line 12)
surf(x,y,z); %Plot the given surface

Answers (1)

Chunru
Chunru on 4 May 2023
Change the value of b:
%Set the Domain of both x and y by setting the lower limit
a = 0 %Enter the lower limit of both x and y in the domain of the sketch
a = 0
% VVV use a bigger b so that x, y have more points
b = 10 %Enter the upper limit of both x and y in the domain of the sketch
b = 10
h = 0.05 %Identify the increment
h = 0.0500
%Setup the graphing properties for both x and y and z.
[x,y] = meshgrid(a:h:b); %Create the domain of the graph
z = -x^4+x^2-y^2; %Encode the function in terms of x and y (Note that all operations between x's and y's are done with dotted operation
%whos
%First Plotting Space. Sketch the surface with the title "Surface Plot"
subplot(2,2,1); %Divide the plotting space into Four parts and use the first plotting area
surf(x,y,z); %Plot the given surface
title("Surface Plot"); %Set the title as "Surface Plot"
%Second Plotting Space. Sketch the Mesh with the title "Mesh Plot"
subplot(2,2,2); %First PLotting Space. Sketch the surface with the title "Surface Plot"
mesh(x,y,z); %Use the second plotting space
title("Mesh Plot"); %Mesh Plot the given surface
%Set the title as "Mesh Plot"
%Third Plotting Space. Sketch the Level Curves at various heights with the title "Level Curves"
subplot(2,2,3); %Use the third plotting space
contour3(x,y,z); %Plot the Level Curves of the given surface
title("Level Curves"); %Set the title as "Level Curves"
grid on %Set Grid On
%Fourth Plotting Space. Sketch the Contour Map with the title "Contour Plot"
subplot(2,2,4); %Use the fourth plotting space
contour(x,y,z); %Plot the Level Curvesof the given surface
title("Contour Plot"); %Set the title as "Contour Plot"
axis equal %Set Grid On
grid on %Set the Axes to have the same scale
%Start Using Rotate 3D Tools to rotate the solids

Community Treasure Hunt

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

Start Hunting!