Quiz #7 Matlab Solutions
Contents
Exercise #2
close all
[x,y]=meshgrid(linspace(-4,4,50));
z=3*x./(x.^2+y.^2+1);
[c,h]=contour(x,y,z,10);
clabel(c,h,'manual')
hold on
xlabel('x-axis')
ylabel('y-axis')
title('Level curves and gradient field of f(x,y) = 3x/(x^2 + y^2 + 1).')
[x,y]=meshgrid(-4:.5:4);
Fx=(3*y.^2-3*x.^2+3)./(x.^2+y.^2+1).^2;
Fy=-6*x.*y./(x.^2+y.^2+1).^2;
quiver(x,y,Fx,Fy)
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
x=linspace(0,2,40);
y=linspace(0,2,40);
[x,y]=meshgrid(x,y);
z=4-x.^2-2*x.*y-3*y.^2;
shndl=surf(x,y,z);
set(shndl,...
'FaceColor',[0.7,0.7,0.7]);
lambda=linspace(-1,1);
x=1+lambda/sqrt(2);
y=1+lambda/sqrt(2);
z=-2-6*sqrt(2)*lambda;
lhndl=line(x,y,z);
set(lhndl,...
'Color','blue',...
'Linewidth',2)
hold on
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')
x=1; y=1; z=-2;
phndl=line(x,y,z);
set(phndl,...
'Linestyle','none',...
'Marker','*',...
'Markersize',12)
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
x=linspace(-3,3,40);
y=linspace(-3,3,40);
[x,y]=meshgrid(x,y);
z=x.^2-2*x.*y+3*y.^2;
contour(x,y,z,[2,2])
lambda=linspace(-1,1);
x=ones(size(lambda));
y=1+4*lambda;
lhndl1=line(x,y);
set(lhndl1,...
'Color','blue',...
'Linewidth',1)
lambda=linspace(-1,1);
y=ones(size(lambda));
x=1-4*lambda;
lhndl2=line(x,y);
set(lhndl2,...
'Color','red',...
'Linewidth',1)
grid on
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
title('Tangent line and normal line to x^2 - 2xy + 3y^2 = 2.')