Basic Mathematica Tools (Intro)

Execute each command with  Shift + Enter
Help: Use pull-down menu, OR, if you know command name, e.g. Plot, just type ?Plot, and
navigate to the examples.
Mathematica notebooks are organized in cells (which can contain more than one command).
A semicolon at the end of a command suppresses the output of the command.
Cells can be grouped into units like Sections using the Format menu.

Arithmetic

2+3

2*3

Now type a 2, followed by a space, followed by a 3.

7!

1/3

1./3 (*Notice the difference *)

N[1/3]

1/3//N

Pi

Pi//N

π//N  (* You can get the π symbol using   Esc pi Esc , or with Palettes *)

N[π,1000]

Sqrt[27]

Sqrt[27.]

Sqrt[27]//N

(Sqrt[27.])  (* Entered using a Palette *)

Algebra

Expand[(a+b)^3]

Factor[x^2-x-6]

Solve[x^2-x-6==0,x]

Solve[x^5-x^2+6==0,x]

NSolve[x^5-x^2+6==0,x]

Lists, Vectors, and Matrices

The following are two "lists" of 3 numbers.The lists has been given the names "v" and "w"

v={5,10,11}
w={-1,4,2}

MatrixForm[v]

Extracting individual elements of a list:

v[[1]]

w[[3]]

v[[4]]

The dot product is done with a period between the vectors :

v.w

The following is a "list of lists"
The individual lists can be interpreted as rows of a matrix.

m={{1,2},{3,4}}

MatrixForm[m]

MatrixForm[Inverse[m]]

MatrixForm[m.Inverse[m]]

MatrixForm[Transpose[m]]

Defining Functions

f[x_]:= x Sin[x]

The colon in the “:=” above should be used by default; it delays evaluation of the function definition until the function is actually called.

f[1.1]

Simple Plots

Plotting functions

Plot[x^3-x^2,{x,-1.0,1.5}]

Plot[f[x],{x,-0.5,1.5}]

Plotting Data

a = {{-0.5,0.28},{-0.25,0.03},{0.,-0.1},{0.25,0.08},{0.5,0.3},{0.75,0.4},{1.,0.85}};

MatrixForm[a]

ListPlot[a]

Combining Plots

Give "names" to output  (with semicolons to suppress output) :

graph1 = Plot[f[x],{x,-0.5,1.5}];
graph2 = ListPlot[a];
Show[graph1,graph2]

Other Kinds of Plots

ParametricPlot[{Cos[t],Sin[t]},{t,0,(3 π)/2}]

Plot3D[Sin[x]Cos[y],{x,0,π},{y,0,π}]

Calculus

Simple Differentiation

f[x]

f'[x]

Integration

Clear[x,a,b]  (* Necessary because of previous definitions *)
Integrate[x^3,{x,a,b}]

You can make thigs look pretty using Palettes :

(∫_a^b)x^3\[DifferentialD]x

Differential Equations

Clear[y,x]

Analytical solution with undetermined constants (NOTE: ‘equals’ sign entered as ‘==’)

DSolve[y''[x]+ 4y[x]==0,y[x],x]

Analytical solution with boundary conditions given (NOTE: ‘equals’ signs entered as ‘==’)

DSolve[{y''[x]+4 y[x] ==0,y[0]==0,y'[0]==3},y[x],x]

Generation of Lists with Table Command

b=Table[i^2,{i,0,8}]

c=Table[i^3,{i,0,8}]

{b,c}

MatrixForm[{b,c}]

MatrixForm[Transpose[{b,c}]]

“For” Loops

?For

total1=0;
For[i=1,i≤10,i=i+1,total1=total1+i]

total1

Data Analysis

Reading/Writing of File :

Export["out.dat",{{5.7,4.3},{-1.2,7.8}}];

data=Import["sample.dat"];(* the file needs to be in directory where you started mathematica, or you need a complete path to the file *)

data

Mean and Standard Deviation :

a={3.2,1.6,9.8,10.3,2.1};
b={3,2,1,9,7,10};
Mean[a]
Mean[b]//N
StandardDeviation[a]