Lecture 1 - Introduction to Mathematica

by Leonid Pryadko

Sin[x]

{x,y}

z=Sin[x y]

FullForm[z]

Head[z]

z[[1]]

Length[z]

lis={1,2,3}

Length[lis]

FullForm[lis]

lis[[0]]

TreeForm[lis]

TreeForm[z]

z[[1]]//FullForm

FullForm[z[[1]]]

ans=100!; ans//N

Plotting functions

Basic functions for plotting are Plot and Plot3D

Plot[Sin[x]/x,{x,0,Pi}]

These functions take many options

Plot[{Sin[x]/x,Cos[x]},{x,0,Pi},AxesLabel->{x,Sin[x]/x},PlotStyle->{{ Thickness[.02],Hue[0.5]},{Hue[0.6]},{Hue[0.7]}}]

Plot3D[Sin[x^2+y^2],{x,-3,3},{y,-3,3}]

Lists and Matrices

z1={1,2,3};MatrixForm[z1]

y1={(Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3])};A=Outer[Times,y1,z1];A//MatrixForm

Inverse[A]

IdentityMatrix[3]//MatrixForm

A=IdentityMatrix[3]+Outer[Times,z1,z1]+Outer[Times,y1,y1];A//MatrixForm

Inverse[A]

You can easily plot an array of data

data={{0,1},{1,2},{2,3},{4,5},{5,7}};

ListPlot[data]

ListPlot[data,PlotStyle->PointSize[0.02]]

Much bigger array can be constructed with the function Table

data=Table[{i,Cos[0.05i]},{i,1,200}];data//Short

gr=ListPlot[data,PlotStyle->PointSize[0.01]]

Internal structure of an object

FullForm[0.3]

FullForm[3]

FullForm[z1]

FullForm[y1]

FullForm[A]

y1

y1/.Subscript->Superscript

FullForm[%]

gr//FullForm

Show[gr/.PointSize[0.01`]->PointSize[0.005]]

Solving Algebraic Equations

eq1=(x+3==0);

sol=Solve[eq1,x]

The solution is a list of list of substitution rules for all variables (here just one):

FullForm[sol]

The result of the substitution is a set of values

x/.sol

If you know there is only one answer, you just pick the 1st solution

x/.sol[[1]]

Try quadratic equation

eq2=(x^2-3 x+2==0);

sol=Solve[eq2,x]

x/.sol

You can solve rather complicated equations:

eq3={x+y^2-1==0,x^2-3x+2==y};

sol=Solve[eq3,{x,y}]

These are the corresponding numbers

({x,y}/.sol)//N

Calculus: derivatives and integrals

f=x^3 y+Sin[x];

Single derivative with respect to x is the partial derivative

D[f,x]

To get the full derivative you need to specify the x-dependence for y

D[(f/.y->y[x]),x]

Second derivative and partial derivatives

{D[f,{x,2}],D[f,x,y],D[f,{x,2},y]}

Integrate[f,x]

Integrate[f,{x,0,z}]//Expand

y=.;z=.

f

DSolve[{z'[x]==(x^3) y+Sin[x],z[0]==0},z[x],x]

Solving differential equations

DSolve[y''[t]+y'[t]+5 y[t]==Cos[t],y[t],t]

sol=DSolve[{y''[t]+y'[t]+5 y[t]==Cos[t],y[0]==0,y'[0]==0},y[t],t]

Plot[y[t]/.sol[[1]],{t,0,100}]

Plot[BesselJ[0,x],{x,0,15}]

Integrate[1/(a+b Cos[x]),{x,0, 2 Pi}]

Integrate[1/(a+b Cos[x]),{x,0, 2 Pi},Assumptions->a>b && b>0]

n=5;yy=Table[i,{i,1,n}]

y:=Table[i,{i,1,n}]

y

n=3; y

n=3;yy

y=.

z=Sin[x]+Sin[y]+Sin[2 y]

z/.Sin[2 s_]->Cos[2s^2]

y[x_]:=Sin[x]+Cos[x]

y[x]

y[2x]

Simple program: Block and Module

Block[{variables}, commands]

Block[{i},
For[i=1,i<10,i=i+1,Print[i]]]

i

Fun[A_]:=Block[{i},
For[i=1,i<10,i++,Print[A,"   ",i]]]

Fun["abc"]

Fun["Message"]

Define your own function

generate factorial

Fact1[n_]:=Block[{ans=1,i=n},
While[i>0,ans=ans*i;i=i-1];ans]

Fact1[5]

Fact1[n_]:=Block[{ans=1,i=n},
While[i>0,ans=ans*(i--)];ans]

Fact1[3]

The problem with this definition is that it does not work for fractions or negative numbers

{Fact1[3],Fact1[1.3],Fact1[x],Fact1[-5],5!}

%134

We can limit the domain to integers

Fact2[n_Integer]:=Block[{ans=1,i=n},
While[i>0,ans=ans*(i--)];ans]

Remove[Fact1]

Fact1[n_Integer]:=Block[{ans=1,i=n},
While[i>0,ans=ans*(i--)];ans]

Now it works correctly most of the time

{Fact2[5],Fact2[0.3],Fact2[x],Fact2[-2]}

You can finally fix the definition by writing a test function for non-negative integers

NonNegativeIntegerQ[x_]:=NonNegative[x]&&IntegerQ[x]

Fact[n_?NonNegativeIntegerQ]:=Block[{ans=1,i=n},
While[i>0,ans=ans*(i--)];ans]

This now does what we want it to:

{Fact[5],Fact[0.3],Fact[x],Fact[-2]}

There is a built-in function for factorial which is implemented in terms of Euler Γ function

{3!,x!,1.3!,(-0.2)!, (-3)!}

Head[4]

FullForm[4]

Example: a short program to plot a list of functions with labels

This defines the list of functions

lis={{x,"x"},{x^2,"(x^2)"},{x^3,"(x^3)"}};

Block[{fun,nam},
For[i=1,i≤Length[lis],i++,
fun=lis[[i]][[1]];
nam=lis[[i]][[2]];
Plot[fun,{x,0,3},AxesLabel->{"x",nam}]]]