Three-Dimensional Mathematica Graphics (Section 6.2)

Extract from Mathematica Graphics: An Intensive Tutorial

by Tom Wickham-Jones

This HTML document is based on Mathematica Graphics: An Intensive Tutorial by Tom Wickham-Jones. It was adapted by Martin Kraus for non-commercial use.

Mathematica and MathLink are registered trademarks, and MathReader, MathSource and 3-Script are trademarks of Wolfram Research, Inc.

All other product names mentioned are trademarks of their producers.

Copyright 1992 by Wolfram Research, Inc.

All rights reserved. No part of this document may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the copyright holder.

Function Plotting

Constructing a new function is very simple.

In[36]:= weirdf[x_, y_] := If[x^2 + y^2 < 1, x^2 + y^2, Exp[1 - x^2 - y^2]]
The function which Plot3D requires must evaluate to a number. Thus our weird function is quite adequate. In[37]:= Plot3D[weirdf[s, t], {s, -2, 2}, {t, -2, 2}]
Out[37]= - SurfaceGraphics -

A further possibility is to use Mathematica numerical capabilities. For this the NDSolve function, which numerically solves differential equations, is very suitable.

NDSolve returns an InterpolatingFunction. Of course we could have solved this problem analytically. In[38]:= NDSolve[{x''[t] == -x[t], x[0] == 2, x'[0] == 0}, x, {t, 0, 20}]

Out[38]= {{x -> InterpolatingFunction[{0., 20.}, <>]}}
This plots the result. Evaluate is necessary since Plot is HoldAll. In[39]:= ParametricPlot3D[Evaluate[{t, x[t], D[x[t], t]} /. %], {t, 0, 20}]
Out[39]= - Graphics3D -
This example is more complex. It comprises of two coupled oscillators. In[40]:= NDSolve[{x''[t] == -x[t] - x[t] y[t]^2, y''[t] == -y[t] - x[t] y[t]^2, x[0] == 2, x'[0] == 0, y[0] == 1, y'[0] == 0}, {x, y}, {t, 0, 20}]

Out[40]= {{x -> InterpolatingFunction[{{0., 20.}}, <>], y -> InterpolatingFunction[{{0., 20.}}, <>]}}
This shows the solution. In[41]:= ParametricPlot3D[Evaluate[{t, x[t], y[t]} /. First[%]], {t, 0, 20}]
Out[41]= - Graphics3D -

next page: 6.3 Animation back to table of contents