function []=code6;
%
% Draw 2 surfaces over the region where the surfaces intersect.
% Draw z1=x^2+3y^2 and z2=8-x^2-y^2 over curve of intersection.
% This routine uses Delaunay triangles to creat a triangular mesh.
% ******************************************************************

% First build a rectangular mesh that encloses the triangle, or whatever
% boundary you may have.
minx=-2; maxx=2; dx=0.05;
miny=-2; maxy=2; dy=0.05;
[x,y]=meshgrid(minx:dx:maxx,miny:dy:maxy);

% Find the 2-D curve of intersection of the 2 surfaces, x^2+2y^2=4
% Find all the points from the above mesh that are inside
% the curve of intersection.
k=find((x.^2+2*y.^2)>=4);
x1=x(k); y1=y(k);
% Evaluate the surface functions only at points inside the intersection curve.
z1=x1.^2+3*y1.^2;
z2=8-x1.^2-y1.^2;
 tri = delaunay(x1,y1);
       trisurf(tri,x1,y1,z1);
       hold on;
       trisurf(tri,x1,y1,z2);
 colorbar 'v';
 shading interp;
 hold on;

% Plot the curve of intersection
% the first 2 lines create the curve of points (x2,y2) where surfaces intersect.
x2=-2:0.1:2;
y2=sqrt((4-x2.^2)/2);
y3=-sqrt((4-x2.^2)/2);

% Evaluate one of the surfaces (either one) along this path at (x2,y2).
z3=x2.^2+3*y2.^2;

% Plot the 3-D curve of intersection.
plot3(x2,y2,z3,x2,y3,z3,'color','k','LineWidth',2);

% project the 3-D curve of intersection onto the xy plane
plot3(x2,y2,0*z3,x2,y3,0*z3,'color','k','LineWidth',2);
%==========================================

view(120,20);
% Draw a set of axes
xmax=max(max(x)); ymax=max(max(y)); zmax=max(max(z1))+.2;  
%zmin=min(min(z)); zmax=max(max(z));
line([0,xmax],[0,0],[0,0],'LineWidth',2,'Color','r');
line([0,0],[0,ymax],[0,0],'LineWidth',2,'Color','r');
line([0,0],[0,0],[0,zmax],'LineWidth',2,'Color','r');
text(xmax+0.1,0,0,'X','Color','r','FontSize',15);
text(0,ymax+0.1,0,'Y','Color','r','FontSize',15);
text(0,0,zmax+0.1,'Z','Color','r','FontSize',15);
xlabel('x'); ylabel('y'); zlabel('z');
title('Surface');
% --------------------------------------------------------

