"Mathematica" Cheat Sheet Page 2

ADVERTISEMENT

Linear Algebra...
Page 2
Create a 2x2 matrix (we’re using symbols, but
Work with (x,y) data points from an external file.
In[1]:= w={{a,b},{c,d}}
In[3]:=!!mydata.dat
you could equally easily use numeric values)
!!mydata.dat shows us the contents of the file.
Out[1]= {{a, b}, {c, d}}
4.1 10.7
Read in pairs of numbers from that file, storing
[etc]
Use a dot product to apply that matrix of
In[2]:= w.{x,y}=={k1,k2}
the list of values by the name newvals. Plot the
In[4]:= newvals=ReadList[ \
coefficients to two variables to form a system
Out[2]= {a x + b y, c x + d y} ==
dataset. Fit a line to the points & plot that.
"mydata.dat",{Number,Number}]
of two equations with constants {k1, k2}
{k1, k2}
Finally, overlay both and save as a gif
Out[4]= {{4.1,10.7},[etc]}
In[5]:= plot1=ListPlot[newvals]
Mathematica can easily do most standard
In[3]:= Transpose[w]//MatrixForm
Out[5]= -Graphics- [not shown]
linear algebra operations, for example, we
Out[3]//MatrixForm= a
c
In[6]:= Fit[newvals,{1,x},{x}]
can easily transpose matrix w...
b
d
Out[6]= 5.14286 + 9.96429 x
Or compute the inverse of a 2x2 numeric
In[7]:= plot2=Plot[%,{x,1,8}]
In[4]:=Inverse[{{1,-1},{2,2}}]
Out[7]= -Graphics- [not shown]
matrix...
1
1
1
1
In[8]:= Show[plot1,plot2]
Out[4]={{-, -},{-(-), -}}
Out[8]= -Graphics-
2
4
2
4
In[9]:= Display["b.gif",%,"GIF"]
Or compute the determinant of a 3x3
In[5]:= Det[{{a,b,c},{d,e,f},\
Out[9]= -Graphics-
symbolic matrix...
{g,h,i}}]
Out[5]= -(c e g) + b f g + c d h
Mathematica As A Programming Language...
- a f h - b d i + a e i
If Mathematica doesn’t have precisely what you
(* Approach No. 1 *)
In addition to entering matrices on an element
In[6]:= Table[If[EvenQ[i]||EvenQ[j]\
need (or what it has is overkill), you can always
w=Join[Table[0,{7}],Table[5,{7}],\
by element basis, Mathematica will also let us
,1,0],{i,3},{j,3}]//MatrixForm
use Mathematica as a programming language
Table[10,{4}],{25}];
construct matrices using rules, such as this
Out[6]= 0
1
0
and write your own code. For example, assume
<<DiscreteMath`Combinatorica`
1
1
1
example that sets elements of a 3x3 matrix to
you have a pile of 5, 10 and 25 pound weights.
x=Union[KSubsets[w,7]];
0
1
0
be 1 if the column or row is an even number.
Select[x,(Plus@@##)<=45&]\
Using no more than 7 of them in any instance,
//TableForm
how many combinations can you form that will
Plotting in Mathematica...
Print["\n ",Length[%]," soln’s"]
total no more than 45 pounds?
Plot a function over an interval. If connecting
In[1]:= Plot[x^2,{x,-5,5}]
(* Approach No. 2 *)
from a Unix workstation or an X terminal, your
We can solve that problem using Mathematica’s
Out[1]= -Graphics-
solns=0;
graph will be shown in a new window; we also
Combinatorica package, or we can just write a
In[2]:= Display["a.gif",%,"GIF"]
Do[If[((25i+10j+5k<=45)&&\
show saving graphic output in gif format.
little program to solve that problem directly by
Out[2]= -Graphics-
(i+j+k<=7)),\
looping through a three way nested do loop,
solns++, Null],\
Note: besides GIF format, you can also use
using an if statement to tally only solutions
{i,0,1},{j,0,5},{k,0,7}];
the Display function to save Mathematica
that meet the specified restriction.
Print["\n",solns," soln’s"]
graphics in PDF, EPS, PCL, PBM and
other formats.
Mathematica on other platforms...
UO has a site license for Mathematica covering
For more information, please see
its installation on University owned PC’s, Macs,
and Unix systems.
More Information About Mathematica...
The Mathematica Book, 4th Ed., by Stephen
See also and
Wolfram [ISBN 0-521-64314-7, 1470 pages] is
for online
the definitive reference.
copies of many Mathematica documents.

ADVERTISEMENT

41 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2