Starting Mathematica


This is a basic introduction to Mathematica. Unlike conventional computer languages Mathematica can do symbolic manipulation, has a huge number of built-in numerical functions, can do numerical calculations to arbitrary precision, and has powerful plotting routines which interface directly with the results of calculations. Mathematica can also be used for programming in styles which are quite different from those of C and fortran, but we will not discuss programming in Mathematica here.
There are two interfaces to Mathematica: (i) a command line interface, and (ii) the notebook interface. The same commands are given in both, but the notebook interface has some additional features.

If one starts Mathematica in Windows  by clicking on an icon one goes straight into the notebook interface in which a separate window appears into which Mathematica commands are typed.  To execute a command in Windows, you need to hold down the Shift key as well as pressing Enter (just pressing Enter allows you to continue entering your command on the next line but does not execute it).

A Simple Mathematica Session


Mathematica understands the usual operators + - * / and for exponentiation. It also understands basic constants pi and E.  It also knows about a large number of mathematical functions such as Exp[x], Sin[x], Cos[x], ArcSin[x], Log[x]

The following session illustrates some of these features:

x = 7

7

y = 19

19

x y

133

(x + 3) y

190

2^x

128

Exp[ Pi]

^π



Two-Dimensional Plots



The basic plotting command is Plot[f, {x, xmin, xmax}]
which plots the function f(x) from xmin to xmax
For example,

<br />p1 = Plot[ Sin[x], {x, 0, 2 Pi} ]

[Graphics:HTMLFiles/index_8.gif]

⁃Graphics⁃

<br /><br />p2 = Plot[ Cos[x], {x, 0, 2 Pi} ]

[Graphics:HTMLFiles/index_11.gif]

⁃Graphics⁃


The command Show can also be used to combine plots. Suppose we create plots of two functions using two separate calls to Plot, e.g.

<br />Show[ p1 , p2]

[Graphics:HTMLFiles/index_14.gif]

⁃Graphics⁃

  

Three-Dimensional Plots


We will learn how to plot cool surfaces like this one...

Plot3D[Exp[-(x^2 + y^2)] * (Cos[6 * x] + Cos[6 * y]), {x, -2, 2}, {y, -2, 2}, PlotPoints70, PlotRange {-2, 2}, Mesh->False]

[Graphics:HTMLFiles/index_17.gif]

⁃SurfaceGraphics⁃

We will also learn how to plot curves in three dimensions.

ParametricPlot3D[{Cos[10 * t], Sin[10 * t], t}, {t, 0, 2 * Pi}, PlotPoints200]

[Graphics:HTMLFiles/index_20.gif]

⁃Graphics3D⁃

  

Integration

Mathematica can also do indefinite and definite integrals . Integrate[f, x] gives the indefini ... nbsp;         ∫ x^4/(a^2 + x^2) x<br />

<br />Clear[x] <br />Integrate[x^4/(a^2 + x^2), x] <br />

-a^2 x + x^3/3 + a^3 ArcTan[x/a]

Here we find the derivative of the result  and then we simplify to get the original function .     

D[%, x]

-a^2 + x^2 + a^2/(1 + x^2/a^2)

Simplify[%]

x^4/(a^2 + x^2)

Here we compute <br /><br />            ... sp;         ∫_1^2 x^4/(1 + x^2) x<br />

NIntegrate[ x^4/(1 + x^2), {x, 1, 2} ]

1.65508


Defining Functions

There are many functions in Mathematica but it is often very useful to be able to define functions oneself. This is illustrated by the following example, which defines the function . f(x) = x + x^2

f[x_] := x + x^2

f[3]

12

f[t]   

t + t^2



Note two things about the first line:. First of all the underscore after the x, which is called a ``blank'', stands for ``any expression'', so when the function is called, the argument can be named anything, not necessarily x. Secondly, the use of :=, rather than =, which means that the assignment is delayed until the function is called. In the present example it wouldn't have made any difference if we had used =, the normal assignment operator, but it would if the function depended on a parameter.  







Series Expansions
.
Mathematica can work out series of complicated functions to very high order. The command is Series[f, {x, x0, n}] which expands f in powers of up to n-th order, e.g. to get the first 20 terms in the expansion of about x=0:

Series[Exp[Sin[x]], {x, 0, 20}] <br />

1 + x + x^2/2 - x^4/8 - x^5/15 - x^6/240 + x^7/90 + (31 x^8)/5760 + x^9/5670 - (2951 x^10)/362 ... 0929 x^18)/6402373705728000 + (3631 x^19)/34735100400 + (27069353 x^20)/3283268567040000 + O[x]^21

Series [ Sqrt[1 + x], {x, 0, 4} ] <br />

1 + x/2 - x^2/8 + x^3/16 - (5 x^4)/128 + O[x]^5

Series[Exp[Sin[x]], {x, 0, 20}] <br />

1 + x + x^2/2 - x^4/8 - x^5/15 - x^6/240 + x^7/90 + (31 x^8)/5760 + x^9/5670 - (2951 x^10)/362 ... 0929 x^18)/6402373705728000 + (3631 x^19)/34735100400 + (27069353 x^20)/3283268567040000 + O[x]^21

Series [ Sqrt[1 + x], {x, 0, 4} ] <br />

1 + x/2 - x^2/8 + x^3/16 - (5 x^4)/128 + O[x]^5

  

Solving Differential Equations

DSolve[eqn, y, x] solves a differential equation for the function y, with independent variable x.

Clear[y] DSolve[4  y''[x] + 3  y '[x] 0, y[x], x]

{{y[x]  -4/3 ^(-3 x/4) C[1] + C[2]}}

Here we solve Cauchy-Euler equation.

DSolve[x^2 y''[x] + x  y '[x] 0, y[x], x] <br />

{{y[x] C[2] + C[1] Log[x]}}

  

Eigenvalues & Matrices

Eigenvalues[m] gives a list of the eigenvalues of the square matrix m.

A = {{2, 3}, {0, 3}}  Eigenvalues[A] Null

{{2, 3}, {0, 3}}

{3, 2}

Here we find eigenvectors.

Eigenvectors[A] <br />

{{3, 1}, {1, 0}}

Here we find CharacteristicPolynomial.

CharacteristicPolynomial[A, x] <br />

6 - 5 x + x^2

Here we find the determinant.

Det[A] <br />

6

Here we find the inverse.

Inverse[A] //MatrixForm<br />

( 1     1 )           -    --           2     2                 1                -           0    3

Here we use row operations to reduce the matrix.

B = {{1, 2, 3}, {0, 3, 2}, {1, -1, 1}}  RowReduce[B] //MatrixForm Null

{{1, 2, 3}, {0, 3, 2}, {1, -1, 1}}

(         5 )                   -           1   0   3                    2                   -           0   1   3              0   0   0

Exp[B] //MatrixForm<br />

(                                   )                               2          ...                        1                       --------                   

 

 

 

Laplace Transform

 

 LaplaceTransform[Sin[t],t,s]

 

Series

 0.5+Sum[ (1-(-1)^n)*Sin[n x]/(n Pi),{n,1,100}]

To plot f(x)

 Plot[0.5+Sum[ (1-(-1)^n)*Sin[n x]/(n Pi),{n,1,1000}],{x,-Pi,Pi}];

 

 

 

 


Created by Mathematica  (May 16, 2006)