[sci.math.symbolic] Fitting a surface to 3D data and plotting with Mathematica

roskar@jhunix.HCF.JHU.EDU (Veljko Roskar) (10/05/90)

Since only one person replied to my question about fitting a surface
to 3-dimensinal data, I am asking again for help.


This time I will be more general:

Using Mathematica, is there an easy procedure to plot both 3-dimensional
points and a surface that gives a best fit to these points?

The data is linear in one dimension and has a polynomial dependency
in the other, but a cubic-spline fit would give the best fit in both
dimensions.

Because of my time constraint, I cannot code the procedures from
Numerical Recipes in Fortran. Besides it's the 3D plot I'm after, not
an equation to model the data.

Below is a summary (long) of the correspondence I've exchanged with Jerry
who was my only help so far:

This is what my data looks like, the third variable is a function 
of the first two:

^L
...
My data:

co1=List[{.807,1.0,22.84},{.575,1.0,12.36},{.417,1.0,7.43},{.267,1
.0,3.44},{0,1.0,1.23}]

co2=List[{.807,1.1,24.41},{.575,1.1,13.82},{.417,1.1,8.40},{.267,1
.1,3.85},{0,1.1,1.27}]

co3=List[{.807,1.2,25.97},{.575,1.2,15.29},{.417,1.2,9.37},{.267,1
.2,4.26},{0,1.2,1.31}]

co4=List[{.807,1.3,27.54},{.575,1.3,16.75},{.417,1.3,10.3},{.267,1
.3,4.67},{0,1.3,1.35}]

Note that the first variable repeats itself in every Rco#S set, 
and that the second one is constant in each set.
...

+++++++++++++++
From:   IN%"trantow%csd4.csd.uwm.edu%rutgers@APLCEN.BITNET"  "Jerry J Trantow"  2-OCT-1990 10:06:11.71
To:	roskar%jhunix%aplcen@mimsy
Subj:	RE: Fitting surfaces (planes) to (x,y,f(x,y)) data:Mathematica

I don't think Fit[] will do what you want.  What you really want is simple
multiple regression.   I just had to do this for a HW assignment and I spent
a few extra minutes doing the graphics you also requested.
     
I'm not sure how this will cut/paste, but here goes
Problem 1.4.8 Multiple Regression
Multiple Regression fits two-dimensional data by a plane
y=C+Dt+Ez, instead of one-dimensional data by a line.  If we are given
(t,z,b)
Write down the 4 equations in the 3 unknowns C,D,E.  What is the least
squares solution from the normal equations?
givendata={{0,0,2},{0,1,2},{1,0,1},{1,1,5}}
{{0, 0, 2}, {0, 1, 2}, {1, 0, 1}, {1, 1, 5}}
A=Table[{1,givendata[[i,1]],givendata[[i,2]]},{i,1,4}];MatrixForm[A]
1  0  0
     
1  0  1
     
1  1  0
     
1  1  1
b=Table[{givendata[[i,3]]},{i,1,4}]
{{2}, {2}, {1}, {5}}
Aprime=Transpose[A].A;bprime=Transpose[A].b
{{10}, {6}, {7}}
solution=LinearSolve[Aprime,bprime]
{{1}, {1}, {2}}
yplane[w_]=Transpose[solution].w
{{1, 1, 2}} . w
ypoly=Polygon[{{0,0,yplane[{{1},{0},{0}}][[1,1]]},
               {0,1,yplane[{{1},{0},{1}}][[1,1]]},
               {1,0,yplane[{{1},{1},{0}}][[1,1]]},
               {1,1,yplane[{{1},{1},{1}}][[1,1]]}}]
Polygon[{{0, 0, 1}, {0, 1, 3}, {1, 0, 2}, {1, 1, 4}}]
abc=Table[Graphics3D[Point[givendata[[i]]]],{i,1,4}]
Show[Graphics3D[ypoly],ViewPoint->{2,1,2}]
Show[Graphics3D[PointSize[.02]],abc,ViewPoint->{2,1,2}]
Show[Graphics3D[PointSize[.02]],abc,Graphics3D[ypoly],ViewPoint->{2,1,2}]

I will also pop a copy of this into snail mail for you.  Good luck.
....


************
^L
My reply:
You mailed me:
...
Problem 1.4.8 Multiple Regression
Multiple Regression fits two-dimensional data by a plane
y=C+Dt+Ez, instead of one-dimensional data by a line.  If we are given
(t,z,b)
Write down the 4 equations in the 3 unknowns C,D,E.  What is the least
squares solution from the normal equations?
...
 
Did you mean y=Cb+Dt+Ez?  There is also the problem that in one dimension
my data is linear (the third variable w/respect to the second--from
co1 to co4), and in the second dimension it best fits a polynomial of the
2nd order (the third variable with respect to the first--for each co# set).
 
So my surface will look like a bent sheet of paper that's angled about an
axis perpendicular to the "axis" of the bend.
 
Any ideas?
...
***************
^L
From:   IN%"trantow@csd4.csd.uwm.EDU"  "Jerry J Trantow"  2-OCT-1990 22:19:44.39
To:	ROSKAR@JHUVMS.BITNET, trantow@csd4.csd.uwm.EDU
Subj:	RE: Fitting a plane to 3D data

The example I gave was correct as sent.  In one dimension

y=-mx+b with x being the variable

In two dimensions, you are fitting 

y=C+Dt+Ez  with t and z being the variables.  I didn't pick the variable
           names they were given in the book.  If your data requires a 
           second order  polynomial, I suspect you could set up the equation
           to be 

y=C+Dt+Ez+Fx^2

           the same technique as I gave should work.  I mailed it off to you
           today.  Find a linear algebra or statistic book that shows how
           to do multiple regression.  Your problem does not seem very bad.

****************
End of included text.


-- 
Veljko Roskar                            | roskar@jhuvms.bitnet
Department of Chemical Engineering       | roskar@jhuvms.hcf.jhu.edu  
The Johns Hopkins University, Baltimore  | uunet!mimsy!aplcen!jhunix!roskar