How do I make the matrix match in size

5 visualizzazioni (ultimi 30 giorni)
Gabrielle Mesquita
Gabrielle Mesquita il 9 Mar 2016
Risposto: John BG il 10 Mar 2016
Hi, I'm trying to do my homework assignment, but I have gotten multiple errors so far. I'm not sure how to make the matrix size agree, either how to ensure that the grid is comprised of 61 x 81. This is my code so far:
x = -4:0.1:4;
y= -3:0.1:3;
z = x.^2 - 2*x.*y + 3*y + 2;
plot3(x,y,z)
contour(x,y,z)
ASSIGNMENT INSTRUCTIONS >> Construct a 3-D surface plot of z = x.^2 - 2*x.*y + 3*y + 2. You must provide the following:
  1. - Plot from -3 to 3 for x and -4 to 4 for y. Ensure that the grid is comprised of 61 x 81 data points.
  2. - Display contour map in the same figure.
  3. - Make sure that the shading for the surface is interpolated.
  4. - Make the sure the overlaid mesh is visible, black and has line width of 1.5.
If someone would help me with the other bullet point too I would really appreciate. Please, show me the syntax.

Risposte (2)

John BG
John BG il 10 Mar 2016
Hi
answer 1:
x=linspace(-4,4,61)
y=linspace(-3,3,81)
[X,Y]=meshgrid(x,y)
Z=X.^2-2*X.*Y+3*Y+2
surf(Z)
answer 2
surfc(X,Y,Z)
or
figure(3);meshc(X,Y,Z)
answer 3
h=meshc(X,Y,Z)
the handle to the surface properties is
h(1)
=
Surface with properties:
EdgeColor: 'flat'
LineStyle: '-'
FaceColor: [1 1 1]
FaceLighting: 'none'
FaceAlpha: 1
XData: [81x61 double]
YData: [81x61 double]
ZData: [81x61 double]
CData: [81x61 double]
Show all properties
AlignVertexCenters: 'off'
AlphaData: 1
AlphaDataMapping: 'scaled'
AmbientStrength: [1x1 double]
Annotation: [1x1 matlab.graphics.eventdata.Annotation]
BackFaceLighting: 'reverselit'
BeingDeleted: 'off'
BusyAction: 'queue'
ButtonDownFcn: ''
CData: [81x61 double]
CDataMapping: 'scaled'
CDataMode: 'auto'
CDataSource: ''
Children: []
Clipping: 'on'
CreateFcn: ''
DeleteFcn: ''
DiffuseStrength: [1x1 double]
DisplayName: ''
EdgeAlpha: 1
EdgeColor: 'flat'
EdgeLighting: 'flat'
FaceAlpha: 1
FaceColor: [1 1 1]
FaceLighting: 'none'
FaceNormals: [80x60x3 double]
FaceNormalsMode: 'auto'
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
LineStyle: '-'
LineWidth: [1x1 double]
Marker: 'none'
MarkerEdgeColor: 'auto'
MarkerFaceColor: 'none'
MarkerSize: 6
MeshStyle: 'both'
Parent: [1x1 Axes]
PickableParts: 'visible'
Selected: 'off'
SelectionHighlight: 'on'
SpecularColorReflectance: 1
SpecularExponent: 10
SpecularStrength: [1x1 double]
Tag: ''
Type: 'surface'
UIContextMenu: []
UserData: []
VertexNormals: [81x61x3 double]
VertexNormalsMode: 'auto'
Visible: 'on'
XData: [81x61 double]
XDataMode: 'manual'
XDataSource: ''
YData: [81x61 double]
YDataMode: 'manual'
YDataSource: ''
ZData: [81x61 double]
ZDataSource: ''
by 'shading of the surface' I understand
h(1).EdgeColor='interp'
answer 4
the handle to the contour is
h(2)
=
Contour with properties:
LineColor: 'flat'
LineStyle: '-'
LineWidth: 1.500000000000000
Fill: 'off'
LevelList: [-10 0 10 20 30 40 50]
XData: [81x61 double]
YData: [81x61 double]
ZData: [81x61 double]
Show all properties
Annotation: [1x1 matlab.graphics.eventdata.Annotation]
BeingDeleted: 'off'
BusyAction: 'queue'
ButtonDownFcn: ''
Children: []
Clipping: 'on'
ContourMatrix: [2x469 double]
CreateFcn: ''
DeleteFcn: ''
DisplayName: ''
Fill: 'off'
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
LabelSpacing: 144
LevelList: [-10 0 10 20 30 40 50]
LevelListMode: 'auto'
LevelStep: 10
LevelStepMode: 'auto'
LineColor: 'flat'
LineStyle: '-'
LineWidth: 1.500000000000000
Parent: [1x1 Axes]
PickableParts: 'visible'
Selected: 'off'
SelectionHighlight: 'on'
ShowText: 'off'
Tag: ''
TextList: [-10 0 10 20 30 40 50]
TextListMode: 'auto'
TextStep: 10
TextStepMode: 'auto'
Type: 'contour'
UIContextMenu: []
UserData: []
Visible: 'on'
XData: [81x61 double]
XDataMode: 'manual'
XDataSource: ''
YData: [81x61 double]
YDataMode: 'manual'
YDataSource: ''
ZData: [81x61 double]
ZDataSource: ''
the contour line width can be changed with
h(2).LineWidth=1.5
Regarding the color request, I am not sure whether the 'overlaid mesh' comment means the color of the actual figure, that you can change color altering
h(1).FaceAlpha % default 1
h(1).FaceColor: % default [1 1 1]
h(1).FaceLighting % default 'none'
or the color of the contours, that you can change with
h(2).ContourMatrix
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John

James Tursa
James Tursa il 10 Mar 2016
Instead of using x and y in your z equation directly, first make a grid of x and y values. E.g.,
[X Y] = ndgrid(x,y);
Then you can use these X and Y grids to generate the Z values:
Z = X.^2 - 2*X.*Y + 3*Y + 2;
Now you have what you need for the downstream stuff.

Categorie

Scopri di più su Graphics Object Properties in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by