Quiz #8 Matlab Solutions

Contents

Exercise #1

% close all open figure window
close all

% create the mesh
x=linspace(-1,0.5,40);
y=linspace(-1.5,0.5,40);
[x,y]=meshgrid(x,y);

% calculate z-values
z=8*x.^3+y.^3+6*x.*y;

% mesh
mesh(x,y,z)

% labels and title
xlabel('x-axis')
ylabel('y-axis')
title('z=8x^3 + y^3 + 6xy')

% change the view
view(130,30)

% annotations
annotation(...
    'textarrow',[0.2,0.45],[0.8,0.75],...
    'String','Local Max (-0.5,-1,1)',...
    'textedgecolor','k',...
    'textbackgroundcolor','w');

annotation(...
    'textarrow',[0.7,0.55],[0.85,0.55],...
    'String','Saddle Point (0,0,0)',...
    'textedgecolor','k',...
    'textbackgroundcolor','w');

% new figure window
figure

% contour map
[c,h]=contour(x,y,z,-2:.25:2);

% hand label
clabel(c,h,'manual');

% critical curves f_x and f_y
x=linspace(-1,0.5,200);
y=-4*x.^2;
line(x,y,...
    'LineWidth',1,...
    'Color',[1,0,0])

y=linspace(-1.5,0.5,200);
x=-1/2*y.^2;
line(x,y,...
    'LineWidth',1,...
    'Color',[0,0,1])

text(-0.45,-1,'  (-0.5, -1) Local Max')
text(0,0.05,'  (0,0) Saddle')

text(-0.55,-1.4,' f_x(x,y) = 0')
text(-0.05,0.4, '  f_y(x,y) = 0')

% grid
grid on

% axis labels and title
xlabel('x-axis')
ylabel('y-axis')
title('Level curves of z=8x^3 + y^3 + 6xy')

% second derivative test
x=[0;-1/2];
y=[0;-1];
f=8*x.^3+y.^3+6*x.*y;
fxx=48*x;
D=(48*x).*(6*y)-6^2*ones(size(x));

[x,y,f,fxx,D]
 
    Please wait a moment...
 
   Carefully select contours for labeling.
   When done, press RETURN while the Graph window is the active window.
ans =
         0         0         0         0  -36.0000
   -0.5000   -1.0000    1.0000  -24.0000  108.0000