Introduction

We will spend the first part of the course by going through some simple Mathematica programming basics. 

Most Important advice: CONSTANTLY CONSULT THE HELP FUNCTION IN MATHEMATICA.

As a starter, we shall  go through a series of simple exercises. During the course of the exercises, you shall pick up some simple knowledge of how to ‘speak’ in Mathematica language. After these exercises, students should have gained sufficient ‘feel’ of how to use Mathematica for more complicated situations that will in the subsequent chapters.

Print “Hello world”

Print["Hello world"]

String and Number

str="This is a string";
number=4;
Print[str];
Print[number];

var1=1;var2="1";
Print[{var1,var2}];
NumberQ[var1]
StringQ[var2]

A simple arithmatic calculation

a=1.0;
b=2.0;
c=a+b;
Print[c];

Arithmatic operations in Mathematica: Sqrt[],^,Ln[],Exp[],Sin[], etc.

Sqrt[2]
Exp[10]
Log[1]
Log[E]
Cos[Pi/2]
Sin[Pi/2]
10^2
10!
10^(1/3.0)

N[Exp[2]]

N[10+1]

Log[2.718]

N[Pi]

N[Cos[Pi/4]]

Cos[45Degree]

More complicated Arithmatic operations

1.2^(1/2)Exp[-1/2]
N[Tan[45Degree]]
N[Tan[Pi/4]]

1. Graph Plotting

Define a simple function f = (x - a)^2 + c.
Define a function of single variable - A Gaussian function, f(x)=(1/(σ(Sqrt[2π])))(e^(-((((x-μ))^2)/(2(σ^2))))).
Plot the function in terms of x.
Plot two functions (for two values of sigma) in a single graph

f[x_]:=(x-a)^2+c;a=1;c=1;
g[x_]:=x;
Plot[{f[dummy],g[dummy]},{dummy,-10,10},
AxesLabel->{"x","y"},
PlotLabel->"y vs x",PlotRange->{{0,10},{0,10}},
AspectRatio->1,
PlotStyle->{Red,Blue}
]

sigma=2;mu=-2;
f[x_]:=(1/(sigma*Sqrt[2 Pi]))*Exp[-0.5(x-mu)^2/sigma^2];
Plot[f[x],{x,-10,10},PlotRange->All]

ListPlot.

Creat a set of paired data points. 
Plot a set of data point in a x-y plane.
Creat another set of paired data points. 
Plot two sets of data point in a x-y plane.
Cosmetics - labelling of axes, title of graph, aspectratio, color, plotrange, framming.
Merge the function plot and data point plot in a single graph using Show.

dat1={{1,2},{3,4},{4,6},{5,8}};
ListPlot[dat1,PlotRange->{{0,10},{0,10}},
Joined->True,Mesh->All
]

data1={{1,0.2},{2,0.4},{3,0.9},{4,0.16}};
ListPlot[data1,PlotJoined->True];
data2={{5,0.2},{6,0.4},{7,0.9},{8,0.16}};
ListPlot[{data1,data2},PlotJoined->True,
PlotStyle->{Red,Blue},
AspectRatio->1,
(*Frame->True,*)
(*PlotRange->{{0,4},{0,10}},*)
PlotRange->All,
AxesLabel->{x,y},
Mesh->Full
]

g1=ListPlot[data1,PlotJoined->True,Mesh->Full,PlotStyle->Red];
(*f[x_]:=(1/(sigma*Sqrt[2 Pi]))*Exp[-0.5(x-mu)^2/sigma^2];*)
g2=Plot[f[x],{x,0,4},PlotStyle->Blue];
Show[{g1,g2}]

Directory hoping: Directory[];NotebookDirectory[];SetDirectory[];

(1) What is your current directory? (2) Can you creat a new directory named “newdirectory” in you current directory? (3) Can you then go into “newdirectory” and create another directory “newnewdirectory” there? Now, can you go back to your original directory as in (1)? (4) What are the files that are in your currently directory?

Directory[]

NotebookDirectory[]

SetDirectory[NotebookDirectory[]]

CreateDirectory["newdir"]

FileNames[]

Directory[]

SetDirectory["newdir"]

Directory[]

FileNames[]

Import["2.dat","List"]

Export["1.dat",0]

FileNames[]

input0=Import["1.dat","List"]

input0[[1]]

list={1,2,3,5}

list[[4]]

input0[[1]]

NumberQ[input0]

Export["1.dat",Random[Integer]];
Export["2.dat",Random[Integer]];
Export["3.dat",Random[Integer]];

For[
i=1,i≤3,i++,
fn=ToString[i]<>".dat";
Print[fn,StringQ[fn]];
Export[fn,Random[Integer]]
];

FileNames[]

Import["1.dat"]

2==1

count0=0;
For[i=1,i≤10,i++,
ran=Random[Integer];
(*Print["ran = ",ran];*)
If[ran==0,
count0=count0+1;
If[count0==9,Print["the value of i corresponse to the 9th zero is ",i]];
];(*end if*)
](* end for *)
Print["count0 = ",count0]

I/O: exporting, importing; for loop;

Generate random values in a for loop.
Generate pairs of data points into a .dat file
Create a for loop to generate two set of .dat files. Use Append for this purpose.
Read in one data set from one of the .dat files generated. List plot this data.
Use for loop to read in two data set and listplot both on the same graph.
Command to use: Random[];Append; Export;Import;StringQ;NumberQ;ToString;<>;indexed list, data[i];

appended={};
For[i=1,i≤10,i++,
ran=Random[];
dat={i,ran};
(*Print[appended];*)
appended=Append[appended,dat];
](*end for*)
Export["data1.dat",appended];

For[i=1,i≤10,i++,
ran=Random[];
dat={i,ran};
(*Print[appended];*)
appended=Append[appended,dat];
](*end for*)
Export["data2.dat",appended];

datain1=Import["data1.dat"];
datain2=Import["data2.dat"];
(*Print[datain1];
Print[datain2];*)

For[
i=1,i≤2,i++,
istr=ToString[i];
filename="data"<>istr<>".dat";
(*Print[istr," ",StringQ[istr], " ",NumberQ[istr]];*)
Print[filename, " ",StringQ[filename]];
data[i]=Import[filename];
];(*end for i*)
Print[data[1]]
Print[data[2]]

An exercise

f[x_]:=x^2;
data={};
For[i=1,i≤10,i++,
(*Print[{i,f[i]}];*)
data=Append[data,{i,f[i]}]
];
(*Print[data];*)
lpdata=ListPlot[data];
pf=Plot[f[x],{x,1,10}];
Show[lpdata,pf]

Abstract descrete data points from the gaussian by appending the data points into a list. ListPlot the list in the same graph with the funciton. Take the range of x from [-1,1]. Number of descrete points = 5;

(*x[1]=-1.0;x[2]=-0.5;x[3]=0;x[4]=0.5;x[5]=1.0;
Table[x[i],{i,0,5}];*)
sigma=1;mu=0;
f[x_]:=(1/(sigma*Sqrt[2 Pi]))*Exp[-0.5(x-mu)^2/sigma^2];

xini=-1.0;xlast=1.0;NP=20;
deltax=(xlast-xini)/NP;
x[i_]:=xini+i*deltax;
tx=Table[x[i],{i,0,NP}];
txfx=Table[{x[i],f[x[i]]},{i,0,NP}];
g1=ListPlot[txfx(*,PlotJoined->True*),Mesh->Full,PlotStyle->{Red,PointSize->0.02}];
g2=Plot[f[x],{x,-1,1},PlotStyle->Blue];
Show[g1,g2]

i=0;
While[i<5,
i=i+1;
](*end while *)
Print[i]

Root finding: Bisection method

Find the root of a simple function, xtan x = x. Purpose - to familiarlise student with logical structure of a program. 
Command to use: While loop;

h[x_]:=(*x*Tan[x]-x;*)(x-1)^3;
Plot[h[x],{x,0,Pi/2}]

h[x_]:=(*x*Tan[x]-x;*)(x-1)^3;
xinil=0.5;xinir=20.0;
xl=xinil;xr=xinir;
xmid=(xl+xr)/2.0;
While[
Abs[h[xmid]]>0.00001,
If[h[xmid]*h[xl]<0,
xr=xmid;
,(*else*)
xl=xmid;
];(*end if*)
xmid=(xl+xr)/2.0;
Print[Abs[h[xmid]]];
];(*end while*)
Print["the solution is: xsol = ",xmid];
Print["h[xsol] = ",h[xmid]];

Root finding: Newton-Rapson method

Root finding: “Methematica” method

Integrate with stochasitc method

Use stochastic method to integrate the area under the curve of a all positive function, say, f = x^2 for x in [-1,1] as example.

Integrate with Trapizoid rule (see Numerical integration.ppt)

Integrate with Simpson rule (see Numerical integration.ppt)

Integrate with stochasitc method

Use stochastic method to integrate the area under the curve of a all positive function, say, f = x^2 for x in [-1,1] as example.

Integrate using “Methematica”method.

Use stochastic method to integrate the area under the curve of a all positive function, say, f = x^2 for x in [-1,1] as example.

f1[x_]:=10x*Sin[x];f2[x_]:=x^2-10;
F[x_]:=f1[x]-f2[x];
Plot[{f1[x],f2[x]},{x,-10,10}];
Plot[{F[x]},{x,-10,10}]

f1[x_]:=10x*Sin[x];f2[x_]:=x^2-10;
F[x_]:=f1[x]-f2[x];
g[x_]:=x^2+3x-2;
NSolve[g[x]==0,x]

FindRoot[F[x]==0,{x,1.9}]

{x->3.145048026228391`}

xini=0;xlast=10;yini=0;ylast=10;
Deltay=ylast-yini;Deltax=xlast-xini;A=Deltax*Deltay;
f[x_]=x;ilast=10;
For[i=1,i≤ilast,i++,
x=Random[Real,{xini,xlast}];
y=Random[Real,{yini,ylast}];
P={x,y};
Print[P]
];

x=Random[Real,{xini,xlast}]

|