Computational Physics: - The Programming Language I'll Use Is

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Computational Physics

• In this course we will learn many of the


important numerical techniques used in
computational physics and demonstrate them
with the help of computer programs.

• The programming language I’ll use is


MATLAB
• You may use any other programming language
you are more familiar with for assignments etc.
The major numerical techniques we will learn
and use are the following.

1. Finding roots of a function of one and more


variables.
kL
Example : k tan   k
2 2

2
Solve for k
Newton–Raphson method in one dimension
and extension to higher dimensions.
2 . Interpolation and Curve fitting

Example : Interpolating/curve fitting experimental


data.

Polynomial and Spline interpolation, Best fit


by minimizing RMS difference
3. Numerical integration

Example : Cornu’s spiral


t
 u 2  t
 u 2 
x ( t )   cos   du y( t )   cos   du
0  2  0  2 

Euler method, Simpson Method, Gaussian


quadrature etc.
4. Numerical solutions of ordinary differential
equations with initial as well as boundary
conditions.

Example : Solving the Airy’s equation

y  xy  0 y(0)  0 ; y(L)  0

Euler Method, Runge-Kutta methods of


different order, Shooting method.
5. Numerical solutions of PDE with boundary
conditions

Example : The wave, the heat and Laplace


equations.
Finite Difference and Finite Element methods.
6. Monte Carlo techniques

Examples : Brownian motion, Diffusion, Ising


Model, Monte-Carlo integration

The Metropolis Algorithm,


7. Numerical Quantum Mechanics

Example : Finding energy eigenvalues


and eigenfunctions in different setting,
time evolution of wavefunctions etc.
Beginning With MATLAB

1. MATLAB : An Introduction with Applications


by
Amos Gilat
Basic Operations With Matrices

MATLAB is the short form for


Matrix Laboratory
Not
Mathematics Laboratory

Every variable is a matrix. A single number


is a 1 x 1 matrix. An 1-d array of length N is
either a 1 x N matrix (row vector) or N x 1
matrix (column vector).
In MATLAB one does not need to declare the
data/variable type. Any alphanumeric string,
beginning with a alphabet can be used as a
real, integer or complex variable.

The arithmetic operations : + ; - ; * ; / ; ^

X^3 is x raised to the power 3


Built-in MATLAB Functions

sqrt(x) nthroot(x,n) exp(x) log(x)

Log10(x) factorial(x) abs(x) sin(x)

sind(x) cos(x) cosd(x) tan(x) tand(x)

Cot(x) cotd(x) asin(x) acos(x) tanh(x)


round(x) : round(17/5) = 3 round(18/5)=4

fix(x) : fix(13/5)=2

ceil(x) : ceil(13/5)=3

rem(x,y) : rem(13,5)=3

rand : generates a random number


between 0 and 1
randi(n) : generates a random integer b/w 1
and n
rand(m,n) : generates a random 2 x 3 matrix
with each element a random number b/w 0
and 1

randi(m,n,p) : generates a m x n matrix


with each element a random number b/w 1
and p
Creating a Matrix

A = [2.5 -3.2 1.8]

A = [1 2 3;4 5 6;7 8 9]

A=2:3:17 A=linspace(x,y,n)

Matrix Operations

A*B A multiplied by B. Dimensions must match.


inv(A) : Inverse of A

det(A) : determinant of A

trace(A) : Trace of A

A= ones(m,n) : m x n matrix with all ones

A = zeros(m,n) : m x n matrix with all zeros

A = eye(n) : n x n identity matrix


sin(A) cos(A) exp(A) atan(A) etc. create
matrices where the operation (sin, cos, exp
etc. are applied to each element of the
matrix)

A^n : matrix A multiplied with itself n times.

A.^n : each element of the matrix is raised


to the nth power.
If A and B are of the same dimension, then
A./B is element by element division of A with
those of B
The “for” loop

for k=1:n
--------
--------
--------
end
Let us numerically prove the following.

If A is a square matrix, then


det(exp(A)) = exp(trace(A))

You might also like