Numerical Computing
COL 100 - Introduction to Computer Science
Department of Computer Science and Engineering
Indian Institute of Technology Delhi
Numerical Computing
• Solving mathematical problems numerically
– in terms of NUMBERS, on a computer
• Alternative solution method to difficult
engineering problems
– closed form solution may not be straightforward
• Example problems:
– Integration and differentiation
– Solution to equations
– Interpolation
Courtesy Prof PR Panda CSE Department IIT Dehi
Numerical Integration
• Computing a definite
integral y = f(x)
• Integral of f(x) between
x=a and x=b is the
area under the curve
y=f(x) between x=a
and b
x=a x=b
• This area can be
approximated
numerically
Courtesy Prof PR Panda CSE Department IIT Dehi
Rectangular Rule
y = f(x)
• Divide interval [a,b] into n
equal sub-intervals
– length of sub-interval
h=(b-a)/n
• In each interval,
approximate f(x) by f(x*)
– f(x*) is the value of f(x) at the x=a x=b
mid-point x*of the interval x=a
• Areas of the rectangles are
h.f(x1*), h.f(x2*),...,h.f(xn*) x1* x2* xn*
Integral = h.f(x1*) + h.f(x2*) + ... + h.f(xn*) = h. ∑i f(xi*)
Courtesy Prof PR Panda CSE Department IIT Dehi
Program for Numerical Integration
• f is approximated by a
step-function def f(x): ….
• This will work no matter
def integrate_midpoint(f,a,b,n):
how complex f is h = (b-a)/n
• Accuracy depends on n x = a + h/2
– trade-off against sum = 0.0
computation time for i in range(n):
sum += f(x)
• Only definite integral, no x += h
indefinite integral using return sum*h
this method
Courtesy Prof PR Panda CSE Department IIT Dehi
Trapezoidal Rule
• Example
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Trapezoidal Rule
• Simple rule
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Trapezoidal Rule
• Example
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Trapezoidal Rule
• Composite Rule
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Trapezoidal Rule
• Divide interval [a,b] into n
equal sub-intervals
y = f(x)
– length of sub-interval
h=(b-a)/n
• In each interval,
approximate f(x) by line
segments with end-points:
– [a,f(a)], [x1,f(x1)],..., [b,f(b)]
• Areas of the trapeziums
are x=a x=b
– 1/2 h (f(a) + f(x1)),
– 1/2 h (f(x1) + f(x2)),...,
– 1/2 h (f(xn-1) + f(b)) x1 x2 xn-1
Integral = h (f(a)/2 + f(x1) + f(x2) + ... + f(xn-1) + f(b)/2)
Courtesy Prof PR Panda CSE Department IIT Dehi
Trapezoidal Rule
• Divide interval [a,b] into n
equal sub-intervals
– length of sub-interval
h=(b-a)/n def f(x): ….
• In each interval,
def integrate_trapezoidal(f,a,b,n):
approximate f(x) by line
h = (b-a)/n
segments with end-points: x=a+h
– [a,f(a)], [x1,f(x1)],..., [b,f(b)] sum = (f(a)+f(b))/2
• Areas of the trapeziums for i in range(1,n):
are sum += f(x)
– 1/2 h (f(a) + f(x1)), x += h
– 1/2 h (f(x1) + f(x2)),..., return sum*h
– 1/2 h (f(xn-1) + f(b))
Integral = h (f(a)/2 + f(x1) + f(x2) + ... + f(xn-1) + f(b)/2)
Courtesy Prof PR Panda CSE Department IIT Dehi
Solving the Equation f(x)=0
• Start with a guess x = x0
• Iteratively refine, attempting to get
closer to the root
• Stop if we detect that we are
– converging: differences between
successive x’s get very small
– diverging: differences between successive
x’s get larger
Courtesy Prof PR Panda CSE Department IIT Dehi
Fixed Point Iteration
• Transform f(x) = 0
algebraically to get y = g(x)
x = g(x)
e.g., x2 - 2x +7 = 0 becomes
x = 1/2 (x2 + 7)
• Start with arbitrary x = x0
• Compute
– x1 = g(x0),
– x2 = g(x1),
– ..., y=x
– xn = g(xn-1)
• Solution is called fixed point x = g(x)
of g [solution to f(x)=0]
• This is a solution to f(x)=0
Courtesy Prof PR Panda CSE Department IIT Dehi
Fixed Point Iteration Example
• Let f(x) = x2 - 3x + 1 = 0
• x = 1/3 (x2 + 1)
• Solutions: x = 2.618, 0.382
• For numerical solution
– let x0 = 1 y = g(x)
– x1 = 0.667
y=x
– x2 = 0.481
– x3 = 0.411
– x4 = 0.390
– ...converging. Solution!
• Try different starting point
– let x0 = 3
– x1 = 3.333
– x2 = 4.037
– x3 = 5.766
– x4 = 11.415
– ...diverging. Try different start. 0.382 2.618
Courtesy Prof PR Panda CSE Department IIT Dehi
Bisection Method
• Intermediate value theorem
– if f(a) < 0 and f(b) > 0, then f(x) = 0
somewhere in the interval (a,b)
• Find some a and b for which sign
of f(x) are different y=f(x)
• Consider c=(a+b)/2
• If f(c) and f(a) are of same sign
– new interval is (c, b) [or (b,c)]
• Else
– new interval is (a,c) [or (c,a)] a
c
• Iterate until interval is small b
enough d =(a+b)/2
=(a+c)/2
Courtesy Prof PR Panda CSE Department IIT Dehi
Newton-Raphson Method
• Another method for solving
f(x)=0 y = f(x)
• Assume f ’(x) is continuous
• f ’(x0) = f(x0)/(x0-x1)
• x1 = x0 - f (x0)/f ’(x0)
• x2 = x1 - f (x1)/f ’(x1)
• ...
xn+1 = xn - f (xn)/f ’(xn)
x3 x2 x1 x0
Courtesy Prof PR Panda CSE Department IIT Dehi
Program for Newton-Raphson
Method
• Convergence def f(x):
detected if def fp(x):
new_guess is less
than err away from def newton(x0, f, fp, error, n):
guess = x0
guess for i in range(n):
d = fp(guess)
• Fails if tangent slope if d == 0: print('There is an error')
is zero else:
new_guess = guess-f(guess)/d
• Signal failure if no if abs(new_guess - guess) < error:
convergence after N print('successful')
iterations return new_guess
guess = new_guess
– if failure, try again print('failed')
with different x0 return 0
Courtesy Prof PR Panda CSE Department IIT Dehi