home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / sci / math / symbolic / 2113 < prev    next >
Encoding:
Text File  |  1992-07-29  |  2.7 KB  |  90 lines

  1. Newsgroups: sci.math.symbolic
  2. Path: sparky!uunet!wupost!psuvax1!atlantis.psu.edu!apix.psu.edu!thor
  3. From: thor@vivaldi.psu.edu (Thomas Moertel)
  4. Subject: Re: 3D scatter plots using Mathematica
  5. In-Reply-To: andrew@uaneuro.uah.ualberta.ca's message of Wed, 29 Jul 1992 23:31:13 GMT
  6. Message-ID: <THOR.92Jul30010933@haydn.vivaldi.psu.edu>
  7. Sender: news@atlantis.psu.edu (Usenet)
  8. Organization: Pennylvania State University
  9. References: <1992Jul29.233113.24299@kakwa.ucs.ualberta.ca>
  10. Date: 30 Jul 92 01:09:33
  11. Lines: 77
  12.  
  13. >         I need to produce 3D surfaces as the solution to various
  14. > regression analysis and plot those with the original points in 3D, ie the
  15. > 3D equivalent of a linear regression with both regression line and data
  16. > points visible. I am using Mathematica 2.0 on the NeXT. Be merciful I'm
  17. > not a Mathematician.
  18. >         Andrew Penn
  19.  
  20. What you need to do is create 2 plots, one of the sample points and one 
  21. of the regression surface.  Then you can combine them with the Plot[] 
  22. command.  Using 2D for example, you could combine plots of sine and cosine
  23. like this:
  24.  
  25.   plot1 = Plot[Sin[x], {x, 0, Pi}];  (* first plot *)
  26.   plot2 = Plot[Cos[x], {x, 0, Pi}];  (* second plot *)
  27.   Show[plot1, plot2]                 (* combo plot *)
  28.  
  29. Here's a more realistic example in 3D.  Let's say your regression surface is 
  30. given by
  31.  
  32.   surfaceEq = x^2 + y^2 + z^2 == 16; (* sphere w/radius 4 *)
  33.  
  34. We can make a plot of this surface and some (fake) data like so:
  35.  
  36.   (* this function calculates the z-value of a point on the suface *)
  37.   (* at the given (x, y) coordinates *)
  38.   
  39.   f[x_, y_] := Evaluate[z /. Flatten[Solve[surfaceEq, z]]]
  40.   
  41.   (* this function randomly generates a "pretend" data point *) 
  42.   
  43.   samplePoint[] :=
  44.     Module[{r, phi, x, y},
  45.       r = Random[Real, 4.0];
  46.       phi = Random[Real, N[Pi/2]];
  47.       x = r Cos[phi];
  48.       y = r Sin[phi];
  49.       {x, y, f[x, y] + Random[Real, 0.25]}
  50.     ]
  51.   
  52.   (* let's make a fake set of data *)
  53.   
  54.   data = Table[samplePoint[], {50}];
  55.   
  56.   (* now let's plot the data *)
  57.   
  58.   Needs["Graphics`Graphics3D`"]
  59.   
  60.   pointPlot = ScatterPlot3D[data]
  61.   
  62.   (* let's plot the regression surface *)
  63.   
  64.   surface =
  65.     ParametricPlot3D[
  66.       {
  67.         r Cos[phi],
  68.         r Sin[phi],
  69.         Re[f[r Cos[phi], r Sin[phi]]]
  70.       },
  71.       {r, 0, 4}, {phi, 0, Pi/2.0}
  72.     ]
  73.   
  74.   (* finally, we can combine the plots! *)
  75.   
  76.   Show[surface, pointPlot]
  77.  
  78. Under less ideal conditions some sample points may be hidden by the
  79. surface.  Therefore, you may want to make several plots from different
  80. viewpoints so that all the sample points can be seen. 
  81.  
  82. I hope this helps.
  83.  
  84. --Tom
  85. --
  86.  
  87. Tom Moertel                 Pennsylvania State University
  88. thor@vivaldi.psu.edu        "Them bats is smart. They use radar!"  ;-)
  89.