MATLAB: How to find the equation of the line that intersects a 3D surface and a plane passing through 3 points of interest on the surface

Curve Fitting Toolboxdimensionaldimentionsftool

Suppose I have a surface that I know the equation for, either analytically or from the form provided by the Surface Fitting Tool (sftool) in the Curve Fitting Toolbox. I would like to find the equation of the intersecting line between the plane and the surface.
An illustration of what I mean follows:
[x,y] = meshgrid(-2:0.1:2);
zplane = 3*x + 4*y;
zsurf = x.^2 + 3 + y.^2;
figure
hold on
surfl(x,y,zplane)
surfl(x,y,zsurf)
hold off

Best Answer

While there is no way of directly getting the intersecting line's equation from the Surface Fitting Tool. Achieving this will require a combination of symbolic analysis (by hand) and regression analysis in MATLAB.
To start with, you will need at least 3 points in order to project a 3-dimensional surface onto 2 dimensions. With only two points, there are an infinite number of curves that can simultaneously intersect the two points along the surface of interest.
First, we create an example dataset to work with. Here, we will try to fit a 2-dimensional second degree polynomial (a surface).
[x,y] = meshgrid(-2:0.1:2);
z = x.^2 + 3 + y.^2;
[xi,yi,zi] = deal(x(:),y(:),z(:))
Open the Surface Fitting Tool using the 'sftool' command and perform the fit using the vectors xi, yi, zi that we just created. This should yield an equation for the surface as follows, where x and y are the variables.
Linear model Poly22:
f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2
We know the 3 points of interest that define the intersecting plane, defined in the form 'z = ax + by + c', where a, b, and c are unknowns that we need to find next. Let the coordinates for these 3 known points be defined as:
Point1 : [x1, y1, z1]
Point2 : [x2, y2, z2]
Point3 : [x3, y3, z3]
To get the equation of the plane, we solve for the coefficients of the plane by regressing using the MRDIVIDE ("\") operator:
W = [x1 y1 1;
x2 y2 1;
x3 y3 1];
z = [z1;
z2;
z3];
plane = W\z;
a = plane(1);
b = plane(2);
c = plane(3);
Now, we know the equation of the intersecting plane:
fplane(x,y) = z = a*x + b*y + c
To find the equation of the line that intersects between the sphere and the plane, we can equate the two expressions, f(x,y) for the surface and fplane(x,y) for the plane that we found, and simplify by hand or simplify by using the Symbolic Math Toolbox.
fplane(x,y) = z = a*x + b*y + c
f(x,y) = z = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2
Equating the two expressions for 'z', we have the equation of the line. Of course, this could be simplified algebraically by hand, but that is purely a matter of aesthetics:
p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 = a*x + b*y + c