Quiz #7 Matlab Solutions

Contents

Exercise #2

% close all open figure window
close all

% create the mesh
[x,y]=meshgrid(linspace(-4,4,50));

% calculate z-values
z=3*x./(x.^2+y.^2+1);

% draw and label the contours
[c,h]=contour(x,y,z,10);
clabel(c,h,'manual')

% hold the plot
hold on

% labels and title
xlabel('x-axis')
ylabel('y-axis')
title('Level curves and gradient field of f(x,y) = 3x/(x^2 + y^2 + 1).')

% create the mesh locations for the gradient vectors
[x,y]=meshgrid(-4:.5:4);

% compute the gradient of F
Fx=(3*y.^2-3*x.^2+3)./(x.^2+y.^2+1).^2;
Fy=-6*x.*y./(x.^2+y.^2+1).^2;

% sketch the gradient field
quiver(x,y,Fx,Fy)

% turn on the grid
grid on

axis equal
 
    Please wait a moment...
 
   Carefully select contours for labeling.
   When done, press RETURN while the Graph window is the active window.

Exercise #3

% close all open figure window
close all

% domain for x and y
x=linspace(0,2,40);
y=linspace(0,2,40);

% create the mesh
[x,y]=meshgrid(x,y);

% calculate z-values
z=4-x.^2-2*x.*y-3*y.^2;

% draw the surface
shndl=surf(x,y,z);
set(shndl,...
    'FaceColor',[0.7,0.7,0.7]);

% calculate tangent line
lambda=linspace(-1,1);
x=1+lambda/sqrt(2);
y=1+lambda/sqrt(2);
z=-2-6*sqrt(2)*lambda;

% draw the line
lhndl=line(x,y,z);
set(lhndl,...
    'Color','blue',...
    'Linewidth',2)

% hold the plot
hold on

% draw plane in direction of u
z=linspace(-20,10,40);
r=linspace(0,2*sqrt(2),40);
[z,r]=meshgrid(z,r);
theta=pi/4*ones(size(z));
x=r.*cos(theta);
y=r.*sin(theta);
shndl2=surf(x,y,z);
set(shndl2,...
    'FaceColor','magenta',...
    'Edgecolor','none')


% plot point of contact
x=1; y=1; z=-2;
phndl=line(x,y,z);
set(phndl,...
    'Linestyle','none',...
    'Marker','*',...
    'Markersize',12)

% labels and title
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
title('Tangent line to f(x,y) = 4 - x^2 -2xy -3y^2 at (1,1) in the direction of (1,1)/sqrt(2).')

view(112,12)

Exercise #4

% close all open figure window
close all

% domain for x and y
x=linspace(-3,3,40);
y=linspace(-3,3,40);

% create the mesh
[x,y]=meshgrid(x,y);

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

% draw a single contour
contour(x,y,z,[2,2])

% calculate normal line
lambda=linspace(-1,1);
x=ones(size(lambda));
y=1+4*lambda;

% draw the line
lhndl1=line(x,y);
set(lhndl1,...
    'Color','blue',...
    'Linewidth',1)

% calculate tangent line
lambda=linspace(-1,1);
y=ones(size(lambda));
x=1-4*lambda;

% draw the line
lhndl2=line(x,y);
set(lhndl2,...
    'Color','red',...
    'Linewidth',1)

% turn the grid on
grid on

% labels and title
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
title('Tangent line and normal line to x^2 - 2xy + 3y^2 = 2.')