CS229 Project #1: Mathematica
Due: Wednesday, September 22, 1999, 11:59 pm
Goals
You should have two goals for this project:
- Become sufficiently familiar with Mathematica to use it in the
next few projects.
- Explore some of the curve representations used for animation.
Mathematica
To get started in Mathematica from any Sun in the CS department:
- Run "xstartmath" from a shell to start up Mathematica
- Go to the "Help" menu and select "Help." This brings up the Help
Browser.
- Click the "Getting Started / Demos" radio button in the top
portion of the Help Browser.
- In the left hand list box, select "Tour of Mathematica"
Read through the "Tour of Mathematica" to try out some of the features
available. Good things to learn about are:
- How to define and multiply matrices. (Make sure you are doing
real matrix multiplication and not piecewise multiplication of
elements!)
- How to define and plot functions (e.g. plot 1 + 2t + 3t2 + t3
over some range).
- How to define and plot parametric functions. For example, suppose
x(t)= sin(t) and y(t) = cos(t). Plot x vs. y for some range
of t.
Hints:
- At the bottom of the page labelled "Mathematica as a Calculator",
there is a link labelled "this hyperlink" that has a variety of useful
information that can help you get started with matrices.
- You can get to subjects such as "Plot" or
"ParametricPlot" by typing the function names into the "GoTo"
window in the Help Browser or by searching for these keywords in the
Master Index (select the Radio Button).
- A simple example of defining a function:
foo[t_] := Sin[t]
A nonintuitive step: to use this function with
ParametricPlot, you will likely need
to wrap it in a Evaluate, like so:
ParametricPlot[{Evaluate[foo[t]], Cos[3t]}, {t, 0, 2*Pi}];
The
Assignment
Now for the actual assignment...
Mathematica has a spline fitting program that creates C1 continuity
spines with the second derivative zero at the two extreme endpoints.
Find the discussion and example by searching in the Master Index for
"SplineFit". Work through the example, creating a spline from just
the first three points given: {0,0},{1,2},{-1,3}.
Plot the spline as shown in the example. (Note that with only three
points it is valid only over the range 0-2.)
Recall that a natural cubic spline is a C2 continuous spline
with second derivative zero at the endpoints. Set up and solve the
equations for a natural cubic spline that interpolates the same three
points: {0,0},{1,2},{-1,3}. Don't worry about efficiency, just set up a
huge matrix equation and solve it using
Mathematica. (LinearSolve will do the dirty work here; if you
don't remember how this works from linear algebra, you should read the
documentation on LUSolve).
Hint: You will need to compute two natural cubic splines: one
for x and one for y. Each will have two cubic segments, say u1 and u2
for x; v1 and v2 for y.
Now plot your spline along with that created by Mathematica.
Hint:Assuming you have solved the equations to
define u1, u2, v1, and v2, you can then plot the spline in x-y space
using a call such as the following:
ParametricPlot[ {
{Evaluate[u1[t]], Evaluate[v1[t]] },
{ Evaluate[u2[t]], Evaluate[v2[t]] }
},
{t, 0, 2}]
All segments (u1, u2, v1, and v2) must be defined over
the correct range (0 - 1 for u1 and v1; 1 - 2 for u2 and v2) to make
this work properly; otherwise, the two curves will overlap
unattractively (unlike SplineFit's plot). This is unfortunate, but not hard to deal with.
(If you know a better way to make this plot, please post to the
newsgroup). Questions:
- Does your spline match the spline computed by Mathematica for the
three points given?
- Work out the Catmull-Rom spline through these same three points
and compare it to the other two. Recall that the Catmull-Rom spline
is a Hermite spline with tangents computed automatically. Use the
following formulae for the tangents:
- Interior tangent: p'[i] = (1/2)*(p[i+1] - p[i-1])
- First tangent: p'[0] = (1/2)*(2*p[1]-p[2]-p[0])
- Last tangent: p'[n] = -(1/2)*(2*p[n-1]-p[n-2]-p[n])
Handing it in..
Things to hand in:
Nancy Pollard