Lecture 1

You might also like

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

Computational Physics: Lecture – I

Reference books: Computational Methods for Physics by Joel Franklin

In this course, we will discuss the numerical methods which will can be used to solve the
problems that we encounter in various disciplines, for example, Physics, Engineering, etc.

Before we discuss the numerical methods, let us work on some simple problems as given
below.

1) Finding the roots of a quadratic equation, 𝑎 𝑥 ! + 𝑏 𝑥 + 𝑐 = 0

"# ±√# ! " ' ( )


As we all know, the roots of this equation are !(
and the nature of the roots
depends the determinant 𝑏 ! − 4 𝑎 𝑐.

• If 𝑏 ! − 4 𝑎 𝑐 > 0 then the equation has two different real roots

• If 𝑏 ! − 4 𝑎 𝑐 < 0 then the equation has two different complex roots

• If 𝑏 ! − 4 𝑎 𝑐 = 0 then the equation has two same real roots

Now, our task is to write a numerical code to find the roots of the equation for known values
of 𝑎, 𝑏, 𝑐.

Program in Python language:

# Python program to find roots of quadratic equation


import math

# function for finding roots


def Roots(a, b, c):

det = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(det))

if det > 0:
print(" Two different real roots ")
print((-b + sqrt_val) / (2 * a))
print((-b - sqrt_val) / (2 * a))

elif det == 0:
print(" Real and same roots")
print(-b / (2 * a))

else:
print("Different complex roots")
print(- b / (2 * a), " + i", sqrt_val)
print(- b / (2 * a), " - i", sqrt_val)
a = int(input('Enter a:'))
b = int(input('Enter b:'))
c = int(input('Enter c:'))

# If a is 0, then incorrect equation


if a == 0:
print("Input correct quadratic equation")

else:
Roots(a, b, c)

2) Finding inverse of a 2 x 2 matrix.

𝑎 𝑏
𝐴=/ 1
𝑐 𝑑

To find the inverse of the matrix A first, we need to check the determinant of the
matrix which is 𝑎𝑑 − 𝑏𝑐.

If 𝑎𝑑 − 𝑏𝑐 = 0 then the matrix A is singular (inverse does not exist).

If 𝑎𝑑 − 𝑏𝑐 ≠ 0 then

1 𝑑 −𝑏
𝐴"* = / 1
𝑎𝑑 − 𝑏𝑐 −𝑐 𝑎

Now, we can write a code (similar to the previous problem) to find the inverse of a matrix.
This we can do during the lab session.

Note that at the moment we will not use any inbuilt functions in Python. We will write the
simple codes to solve the problems numerically.

3) Let us consider expansion of sin (𝑥)

/ !"#$
sin(𝑥) = ∑,
+-.(−1)
+
(!+1*)!
/% /& /'
sin(𝑥) = 𝑥 − 4!
+ 5!
− 6!
+⋯

We will write a simple code in the lab session to plot the sin(𝑥) function within the interval
[0, 2𝜋]. Vary the number of points between the interval [0, 2𝜋] and see how the smoothness
of the sin (𝑥) curve varies (see the above figure).
Solving ordinary differential equations (ODE)

Physical motivation

Let us consider the situation where a tiny spherical body of mass 𝑚 and radius 𝑅 falling in a
fluid medium of viscosity 𝜂. The corresponding equation of motion reads

𝑑 ! 𝑥(𝑡) 𝑑𝑥(𝑡)
𝑚 = −𝑚 𝑔 − 𝛾
𝑑𝑡 ! 𝑑𝑡

Where 𝛾 is the damping constant which is 𝛾 = 6 𝜋 𝜂 𝑅.

If we ignore the damping, the solution of the above equation is

𝑔 𝑡!
𝑥(𝑡) = − + 𝑐* 𝑡 + 𝑐!
2

Where 𝑐* and 𝑐! are the constants which can be determined using the initial conditions
7/(8-.)
𝑥(𝑡 = 0) = 𝑥. and 78 = 𝑣.

If we ignore the gravitational force, the solution of the above equation is

𝑚 "9 8
𝑥(𝑡) = 𝑑* − 𝑑! 𝑒 :
𝛾

Where 𝑑* and 𝑑! are the constants which can be determined using the initial conditions
7/(8-.)
𝑥(𝑡 = 0) = 𝑥. and 78 = 𝑣. .

The above problem is easy to solve analytically for the known initial conditions. However,
consider a physical situation where the equations are complicated and may not be solved
analytically even if we know the initial conditions. However, those systems can be solved
numerically.

Euler’s method

Consider a simple first order ordinary differential equation

𝑑 𝑓(𝑥)
= 𝐺(𝑥)
𝑑𝑥

With the initial condition 𝑓(𝑥 = 𝑎) = 𝑓. . Here 𝑥 is a generic independent variable such as
position, time, etc.

We assume that 𝑓(𝑥) is a well-behaved function so that it can be expanded around the known
point 𝑥.
<!
𝑓(𝑥 + ℎ) = 𝑓(𝑥) + ℎ 𝑓 ; (𝑥) + !!
𝑓 ;; (𝑥) + 𝑜(ℎ4 )

By truncating this Taylor’s series after the 1st order term, we get

𝑓(𝑥 + ℎ) = 𝑓(𝑥) + ℎ 𝑓 ; (𝑥) + 𝑜(ℎ! )

𝑓(𝑥 + ℎ) ≈ 𝑓(𝑥) + ℎ 𝐺(𝑥)

This is the simplest method to compute the value of the function 𝑓(𝑥) at a different position
𝑓(𝑥 + ℎ) by knowing the value at 𝑓(𝑥) at 𝑥.

For example, for the considered first order differential equation

0th step - Choose a proper initial condition, i.e., for ℎ = 0 → 𝑓(𝑥 = 𝑥=+=8=(> ) = 𝑓.

1st step - 𝑓(𝑥 + ℎ) = 𝑓(𝑥) + ℎ 𝐺(𝑥) → 𝑓* = 𝑓. + ℎ 𝐺(𝑥=+=8=(> )

2nd step - 𝑓(𝑥 + 2ℎ) = 𝑓(𝑥 + ℎ) + ℎ 𝐺(𝑥 + ℎ) → 𝑓! = 𝑓* + ℎ 𝐺(𝑥* )

3rd step - 𝑓(𝑥 + 3ℎ) = 𝑓(𝑥 + 2ℎ) + ℎ 𝐺(𝑥 + 2ℎ) → 𝑓4 = 𝑓! + ℎ 𝐺(𝑥! )

nth step - 𝑓(𝑥 + 𝑛ℎ) = 𝑓(𝑥 + (𝑛 − 1)ℎ) + ℎ 𝐺(𝑥 + (𝑛 − 1)ℎ) → 𝒇𝒏 = 𝒇𝒏"𝟏 + 𝒉 𝑮(𝒙𝒏"𝟏 )

Note that we need to choose the values of 𝑥 as


shown below.

The local error (at each step) in the Euler’s


method is 𝑜(ℎ! ) → ℎ! .

/" "/("(
Note that step ℎ = +
. It implies, 𝑛 ∝ 1/ℎ

The corresponding global error:


<!
𝑛 (𝑠𝑡𝑒𝑝𝑠) 𝑡𝑖𝑚𝑒𝑠 ℎ! → <
=ℎ

As the step size ℎ is small we can reduce the error in the simulations. However, note that
smaller ℎ increases computational time. Thus, one has to choose the optimal step size !

You might also like