Lecture 1
Lecture 1
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.
Now, our task is to write a numerical code to find the roots of the equation for known values
of 𝑎, 𝑏, 𝑐.
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:'))
else:
Roots(a, b, c)
𝑎 𝑏
𝐴=/ 1
𝑐 𝑑
To find the inverse of the matrix A first, we need to check the determinant of the
matrix which is 𝑎𝑑 − 𝑏𝑐.
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.
/ !"#$
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
𝑑 ! 𝑥(𝑡) 𝑑𝑥(𝑡)
𝑚 = −𝑚 𝑔 − 𝛾
𝑑𝑡 ! 𝑑𝑡
𝑔 𝑡!
𝑥(𝑡) = − + 𝑐* 𝑡 + 𝑐!
2
Where 𝑐* and 𝑐! are the constants which can be determined using the initial conditions
7/(8-.)
𝑥(𝑡 = 0) = 𝑥. and 78 = 𝑣.
𝑚 "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
𝑑 𝑓(𝑥)
= 𝐺(𝑥)
𝑑𝑥
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 𝑥.
0th step - Choose a proper initial condition, i.e., for ℎ = 0 → 𝑓(𝑥 = 𝑥=+=8=(> ) = 𝑓.
nth step - 𝑓(𝑥 + 𝑛ℎ) = 𝑓(𝑥 + (𝑛 − 1)ℎ) + ℎ 𝐺(𝑥 + (𝑛 − 1)ℎ) → 𝒇𝒏 = 𝒇𝒏"𝟏 + 𝒉 𝑮(𝒙𝒏"𝟏 )
/" "/("(
Note that step ℎ = +
. It implies, 𝑛 ∝ 1/ℎ
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 !