Professional Documents
Culture Documents
Module 1 (1,2,3)
Module 1 (1,2,3)
Introduction
The major steps involved to solve a given problem using a computer are:
3
2. Truncation errors are those errors corresponding to the fact that a
finite (or infinite) sequence of computational steps necessary to
produce an exact result is “truncated” prematurely after a certain
number of steps.
3. Round of errors are errors arising from the process of rounding off
during computation. These are also called chopping, i.e. discarding all
decimals from some decimals on.
Due to errors that we have just discussed, it can be seen that our
numerical result is an approximate value of the (sometimes unknown)
exact result, except for the rare case where the exact answer is
sufficiently simple rational number.
If ~a is an approximate value of a quantity whose exact value is a ,
then the difference ε =a−~a is called the absolute error of ~a or, briefly, the
error of ~a . Hence, ~a=a+ ε , i.e.
Approximate value = True value + Error.
Part 1
(f(a) and f(b) have opposite signs) must have at least one root in the
interval [a, b] (a zero in the interval [a, b]). This root is the only one if the
first derivative of f(x) exists and retains its sign inside the interval [a, b].
sign(f’(x)) = const.
Here are basic tabular and graphical methods which can be used to
isolate the roots of the equation.
Example. Isolate all real roots of the equation x 3−2,5 x 2−1,2 x +3=0.
So, there are three segments at the ends of which f (x) has different
signs: [ −2 ,−1 ] , [ 1,2 ], [ 2,3 ] .
Algebraic equation of the third degree has three roots. Consequently,
each of the three segments contains one root of the equation.
2. Graphical method.
Its essence lies in building the graph of the function y=f (x ) and using
it to find the intervals which contain the intersection points of the graph with
the X-axis (that is zeros of the function f ( x)). If the equation has no close-
by-value roots, its roots can be easily separated by this method.
Sometimes f ( x)=0 can be easily represented as ϕ (x )=ψ ( x) where ϕ ( x ), ψ ( x )
are simpler, and then intervals containing the intersection points with these
functions can be found by plotting the functions y=ϕ ( x) and y=ψ ( x ).
So, at each step, the method divides the interval in two by computing
the midpoint ci = (ai+bi) / 2 (i-th iteration) of the interval and the value of the
function f(ci) at that point.
Error Analysis. The maximum error after the i-th iteration using this process
is
log ( b−a )−log ε i
ε i=¿ b−a∨ ¿i ⟹ i≥ ¿
2 log 2
As at each iteration, the interval is divided into halves, we get
ε i+ 1 1
= . Thus, this method converges linearly.
εi 2
The following formula can be used to determine the number of
iterations that the Bisection Method would need to converge a root with a
certain tolerance ε.
9
b-a
n> ln( )/ ln(¿ 2)¿ .
2ε
If we want to calculate the roots of the equation as accurate as ε , we
divide the segment [ a 0 , b 0 ] for as long as the condition |b n−a n|<2 ε is executed.
The approximate root value is chosen as an average value on the segment
a n+ bn
[ a n , b n ]: ξ ≈ .
2
Some other termination criteria are as follows:
Termination after N steps (N given, fixed)
Bisection Scheme
Given a function f (x) continuous on an interval [a, b] and f (a) * f (b) < 0
Do
c = (a+b)/2
If f(c)=0 then (c-root)
if f (a) * f (c) > 0 then a = c else b = c
while |b-a|<2ε (the convergence criteria is satisfied)
10
there is a discontinuity.
Bisection method cannot be applied over an interval where the
function takes always values of the same sign.
The method fails to determine complex roots.
If one of the initial guesses a 0 or b0 is closer to the exact solution, it
will take larger number of iterations to reach the root.
In the laboratory work, you will study the Bisection method and learn
how to use this method for calculating all real roots of an algebraic equation
f ( x)=0 at the given accuracyε =1 0−5.
Procedure
1. Procedure.
2. Theoretical background.
3. Hand calculations.
4. Calculations performed with MathCAD, Python.
5. Conclusions.
11
Variants
Variant
Variant
Equation Number Equation
Number
1 3 2
x −2.8 x −6.2 x+3.7=0 16 3 2
x −0.1 x −4.6 x +2.2=0
2 x 3−9.9 x 2+ 3.5 x −31.9=0 17 x 3−5.9 x 2+11.1 x−6.7=0
3 3 2
x + 0.3 x −5.7 x +2.2=0 18 3 2
x −7.2 x +16.5 x−11.9=0
4 x 3−0.8 x 2−6.8 x+ 0.7=0 19 x 3+ 4.1 x2 +3.6 x−0.4=0
5 3 2
x −0.9 x −22 x+30.8=0 20 3
x −7.3 x +13.5 x−5=0
2
6 3 2
x −5.4 x +2.5 x+12.5=0 21 3 2
x −6.7 x + 11.6 x−2.1=0
7 3 2
x −4.7 x + 4.1 x +0.5=0 22 3 2
x −3.3 x +1.6 x +1.7=0
8 x 3−7.5 x 2+15.9 x−7.8=0 23 x 3−2.1 x2 −2.6 x+1.7=0
9 3 2
x −4.4 x + 4.7 x−1.1=0 24 3 2
x −3.6 x + 3.3 x−0.5=0
10 x 3−4.8 x 2 +3.3 x+5=0 25 x 3+ 0.5 x2 −2.2 x−1.1=0
11 3 2
x −2.7 x −3.5 x+ 0.8=0 26 3 2
x −2.5 x −0.49 x+1.2=0
12 3 2
x −3.9 x + 4.4 x−1.4=0 27 3
x −2.2 x −0.25 x+1=0
2
13 3 2
x −4.2 x + 3.1 x +2.7=0 28 x −3.2 x +0.51 x+ 1.6=0-
3 2
14 3 2
x + 8.5 x +2.8 x +15.6=0 29 3 2
x −2.3 x −0.1 x+ 0.3=0
15 3 2
x −1.6 x −2.4 x +0.3=0 30 3 2
x −1.9 x −2.6 x+ 2.8=0
Hand calculation
Table 1.1
12
a n( f (a n)<0) b n(f (b n)>0) b n−a n an +b n an +b n
f( )
2 2
0 1 2 1 1.5 0.37
1 1 1.5 0.5 1.25 -1.58
2 1.25 1.5 0.25 1.375 -0.65
3 1.375 1.5 0.125 1.4375 -0.15
4 1.4375 1.5 0.0625 1.46875
MathCAD example
2
Let us consider f ( x) x 3 x 12. Its roots must be found at the
accuracy level of =10-5.
a0 7
b0 4
13
ai ai bi
ai 1 ai bi
if f ai f 0 ai bi 2
bi 1 2 2 bi
a20 b20
5.274916
2
a20 b20
f 7.894165 10 6
2
xxx1 F 7 5 10
5 xxx1 5.27491
xxx2 F 1 3 10
5 xxx2 2.27491
Programming blocks which include input validation and counting the
number of iterations might look like
14
3. Solve the equation using integrated functions.
Below you can see the solution for the equation by means of the
integrated function root:
TOL 10 – Internal solution accuracy is redefined here
5
as well as the solution for the equation by means of the integrated function
polyroots:
2
f ( x) x 3 x 12
12
v 3
1
5.27492
xxx
xxx polyroots( v) 2.27492
15
Python program.
MathCAD Pyton
5
f ( x) x 2x 10 lab_1.py
from dihotomia import
dihotomia
a = float(input('a: '))
b = float(input('b: '))
eps = float(input('eps: '))
print(dihotomia(a, b, eps))
function.py
def f(x):
return 2*x**5+4*x-3
16
Part 2
−f (b) −f (b)(a−b)
h1 = (a−b)= .
f (a)−f (b) f ( a)−f (b)
17
Then, applying this technique to the interval ([a; x 0] or [x0; b]), at the
ends of which the function f(x) has opposite signs, we obtain the second
approximation of the root x2, etc.
Geometrically, method of proportional parts is equivalent to replacing
the curve y = f (x) by the chord passing through the points A (a; f(a)) and
B(b; f(b)) (Fig. 1.2). That is why, the chord method has got its name.
If the interval [a; b] is sufficiently small, the error of the method is defined
as |ξ−x n|<| x n−x n−1| where ξ is root of the equation f(x)=0.
Thus, |ξ−x n|< ε is guaranteed if |x n−x n−1|< ε, where ε is the given limiting
absolute error.
18
2.2 Newton-Raphson Method (Newton's method, Newton's iteration)
f (x n)
x n+1=x n − (n = 0, 1, 2, . . .).
f ' ( x n)
19
f ( x n)
x n+1=x n − .
f ' ( x n)
Using the Newton-Raphson method, it is important to remember the
following rule: a starting point x 0 can be that very end of the interval [a, b]
which corresponds to the ordinate with the same sign as the sign of f ″ (x ).
The end iteration condition is the inequality|x n−x n−1|< ε where ε is the
given limiting absolute error.
Let c be a given positive number and let x be its positive square root,
so that
x=√ c . Then x 2=c or f ( x)= x −c=0
2
( )
xn c 1 c
x n+1= + = x+ , n=0,1,2 , … ,
2 2 xn 2 n x n
If the derivative f'(x) changes little on the interval [a, b], then in the
calculation formula of Newton’s method, we can put f ' ( x n) ≈ f ' ( x 0).
Hence, for the root ξ f(x) = 0 we get successive approximations
f ( x n)
x n+1=x n − (n = 0, 1, 2, . . .).
f ' ( x 0)
Geometrically, this method means that tangents at the points Bn
[xn, f(xn)] are replaced by lines parallel to the tangent to the curve y = f(x), at
its fixed point B0[x0, f(x0)] (Fig. 1.4). This formula is quite useful when f ' (x n)
is complex.
21
We have seen that the Newton-Raphson method requires the
evaluation of derivatives of the function and this is not always possible,
particularly in the case of functions arising in practical problems. In the
secant method, the derivative at xn is approximated by the formula
|f (x n )|
|x n−ξ|≤ , |f (x )|≥ m 1 , a ≤ x ≤ b .
m1
Thus, |ξ−x n|< ε is guaranteed if |x n−x n−1|< ε where ε is the given limiting
absolute error.
In the laboratory work, you will study numerical methods for refining
approximate roots, including Chord Method, Newton-Raphson Method,
Modified Newton’s Method, Secant and Combined methods, and find out
how to use these methods for calculating all real roots of an algebraic
equation f ( x)=0 at the accuracy ε =1 0−5.
Procedure
1. Isolate all real roots for the given algebraic equation f (x)=0. To
solve this problem, you can use any analytical, graphical, or tabular
method.
2. Correct one of these roots using the combined method by hand
(only five iterations).
3. Solve the problem of finding all real roots by a MathCAD
programming block at the given accuracyε =1 0−5.Calculate and save
required number of iterations.
4. Solve the problem of isolating and correcting the roots at ε =1 0−5 by
the given methods using Python software. Calculate and save the required
number of iterations.
5. Correct the roots by means of the build-in functions. Compare the
obtained values and make a conclusion about the rate of convergence.
Hand calculation
Let us calculate the real root of the equation x 3+ 3 x +8=0 at the
accuracy of ε =1 0−3.
We consider
f (x)= x +3 x +8.
3
The interval [-2, -1] contains the root of this equation because at its
ends, the function f ( x) has different signs:
f (−2)=−6 ,
f (−1)=4 .
The derivative is f ' (x )=3 x 2+ 3>0 for all variables x, so the equation
x + 3 x +8=0 has a single real root and two complex ones. Thus, the interval
3
№ f (bn )−¿
an bn b n−a n f (an ) f (bn ) '
f (bn ) Δ an Δ bn
п/п f (an )
0 -1 -2 -1 4 -6 -10 15 0,4 -0,4
-
1 -1,4 -1,6 --0,2 1,056 -0,896 -0,932 10,68 0,1082
0,0839
-
2 -1,5082 -1,5161 -0,0079 -0,0447 -0,0331 -0,0778 9,8956 0,00455
0,0033
3 -1,51273 -1,51275 -0,00001
26
MathCAD example programs
Let’s define:
5 4
f ( x) 2 x 4 x 3
d 4 2.4
f1( x) f ( x) 10 x 4
dx 0.8
f ( x)
2 0.80.2 0.4 0.6 0.8
d 3
f2 ( x) f ( x) 40 x
2 2.4
dx
4
x
Function hord refines the root of the equation y=f(x) at the interval [a, b]
up to given error ξ (eps).
Input parameters are
a, b as the end points of the root isolation,
ξ (eps) as the given error.
Output parameters include
two last approximations of the root,
number of iterations which will satisfy the accuracy condition.
In a program block, we use
f (x) as the function of the equation;
f1(x) as the function of the first derivative of f(x);l
f2(x) as the function of the second derivative of f(x).
Both functions must be defined before the program main block as the
user function.
For the chord method, fixed points are defined by the following rule: that
very end is fixed, for which the sign of the function f (x) coincides with the
sign of its second derivative f "(x). This value is entered as fix.
27
hord ( a b ) i0 x hord ( a b )
x a if f ( a) f2 ( a) 0 0.67823
b otherwise x 0.678236
fix b if x a 11
a otherwise
prev_x fix
while prev_x x
prev_x x
f ( x) ( fix x)
xx
f ( fix) f ( x)
ii 1
prev_x
x
i
Newton-Raphson Method and Modified Newton's method
28
newton2( a b ) error ( "error" ) if f ( a) f ( b ) 0 x newton2( a b )
otherwise
i0
x
0.67823
x a if f ( a) f2 ( a) 0
20
b otherwise
fix b if x a
a otherwise
prev_x fix
while x prev_x
prev_x x
f ( x)
x prev_x
f1 ( fix)
ii 1
x
i
combinated( a b ) error ( "error" ) if f ( a) f ( b ) 0 seq ( a b ) error ( "error" ) if f ( a) f ( b ) 0
otherwise otherwise
i0 i0
A a if f ( a) f2 ( a) 0 x a if f ( a) f2 ( a) 0
b otherwise b otherwise
B a if f ( a) f2( a) 0 prev_x b if x a
b otherwise a otherwise
while B A 2 f ( x)
xx
f1 ( prev_x)
f ( B) ( B A )
BB while x prev_x
f ( B) f ( A )
dumb x
f (A)
A A f ( x) ( x prev_x)
f1 ( A ) xx
f ( x) f ( prev_x)
i i 1
prev_x dumb
A B ii 1
2
x
i
i
x combinated( a b ) x seq ( a b )
0.67824 0.67824
x x
4 6
29
Python program
Lab1.py
from methods import hord
from methods import newton1
from methods import newton2
from methods import seq
from methods import combined
a = float(input('a: '))
b = float(input('b: '))
eps = float(input('eps: '))
ans = hord(a, b, eps)
print("hord: %.5f" % (ans))
ans = newton1(a, b, eps)
print("newton1: %.5f" % (ans))
ans = newton2(a, b, eps)
print("newton2: %.5f" % (ans))
ans = seq(a, b, eps)
print("seq: %.5f" % (ans))
ans = combined(a, b, eps)
print("combined: %.5f" % (ans))
Func.py
def f(x):
return 2 * x ** 5 + 4 * x - 3
def f1(x):
return 10 * x ** 4 + 4
def f2(x):
return 40 * x ** 3
Methods.py
from func import f
from func import f2
from func import f1
Rezults:
Part 3
Procedure
Hand calculation
1
Let us substitute x=1 to the inequality to get 0< λ< 10 , or x=2 to get
1
0< λ< .
14
1 1
Let us take λ= 20 from 0< λ< 14 . The original equation is transformed
as
x=x−0,05 (2 x 3 +4 x−11) ,
5
f ( x) 2 x 4 x 3
d 4
f1( x) f ( x) 10 x 4
dx
37
Root isolation intervals for a given equation, λ coefficient, reduced equation
a 0
b 1
5
10
1 1
if f1( a) f1( b )
f1 ( a) f1( b )
0.07143 ( x ) x f ( x)
Solving by Fixed point iteration method and verifying the solution with an
inbuilt function
d 0.51 x
f1_t ( x) f_t ( x) 0.49623 e 1
dx
a 0
b 1
5
10
1 1
_t if
f1_t ( a) f1_t ( b )
f1_t ( a) f1_t ( b )
_t 0.66835
39
Solving by Fixed point iteration method and verifying the solution with an
inbuilt function
6
iter_t( a _t )
0.68582
5
TOL 10
main.py
from tkinter import *
from tkinter import messagebox
from sympy import *
import pylab
import math
import numpy as np
import PIL.Image
import PIL.ImageTk
import os
# output processing
def show_res():
a = float(min_entry.get())
b = float(max_entry.get())
if (a > b):
swap(a, b);
n = int(eps_entry.get())
eps = 10 ** (-n)
_str = func_entry.get()
f = sympify(_str)
show_graph(u, f, _lambda)
messagebox.showinfo("Fixed Point Iteration Method", iterations(f, a,
_lambda, eps,n))
# function plotting
def show_graph(u,f,_lambda):
a=float(min_entry.get())
b=float(max_entry.get())
if (a > b):
swap(a, b);
xlist = np.arange(a, b, 0.05)
ylist1 = [u(f, x, _lambda) for x in xlist]
ylist2 = [x for x in xlist]
pylab.plot(xlist, ylist1)
pylab.plot(xlist, ylist2)
pylab.savefig('graph.png')
pylab.cla()
pylab.clf()
img = PIL.Image.open("graph.png")
photo = PIL.ImageTk.PhotoImage(img)
lbl = Label(root, image=photo)
lbl.image = photo
lbl.place(x=300, y=20);
root = Tk();
root.title("Lab work №3")
root.geometry("1000x560")
islambda = BooleanVar()
islambda.set(0);
islambda_checkbutton = Checkbutton(root, text=" input lambda, \ from the
keyboard ", variable=islambda)
# input fields
min_label = Label(text="from:")
42
max_label = Label(text="to:")
eps_label = Label(text=" Fractional decimal places: ")
func_label = Label(text="function:")
lambda_label = Label(text="lambda:")
func_entry = Entry()
min_entry = Entry()
max_entry = Entry()
eps_entry = Entry()
lambda_entry = Entry()
#graph.place(x=18,y=230);
min_label.place(x=16, y=20);
max_label.place(x=16, y=50);
eps_label.place(x=1, y=80);
func_label.place(x=18, y=110);
lambda_label.place(x=18, y=140);
min_entry.place(x=100, y=20);
max_entry.place(x=100, y=50);
eps_entry.place(x=170, y=80);
func_entry.place(x=100, y=110);
lambda_entry.place(x=100, y=140);
islambda_checkbutton.place(x=20, y=160);
# islambda_checkbutton.pack()
root.mainloop();
43
Output results:
44
4. Questions and exercises for Module 1
Questions:
1. What is the difference between algebraic and transcendental equations?
2.Why we are using numerical iterative methods for solving equations?
3.Based on which principle, the bisection and regula-falsi method is
developed?
4. What are the advantages and disadvantages of the bracketing
methods like bisection and regula-falsi?
5. What is the difference between bracketing and open method?
6. What is the importance of Secant method over Newton-Raphson
method?
7. How the accuracy condition is satisfied for each method?
8. Give a geometric interpretation for methods that approximate the root
from only one side.
9. Give a geometric interpretation for methods that approximate the root
from the both side.
10. Which methods use the values of the first derivative?
11. Which methods use the values of the second derivative?
12. How to determine the required number of iterations according to the
accuracy condition before performing calculations? In which method is it
possible?
Exercises:
1. Approximate the real root to two four decimal places of
3
x + 5 x −3=0.
Approximate to four decimal places √3 3
2. Find a positive root of the equation
4
x +2 x+ 1=0
(Choose x0 = 1.3), correct to 4 places of decimals.
3. Explain how to determine the square root of a real number by
Newton-Raphson method and using it determine correct to three
decimal places.
4. Find the value of √ 2 correct to four decimals places using Newton
Raphson method.
5. Use the Newton-Raphson method, with 3 as starting point, to find a
fraction that is within 10-8 of √ 10.
6. Calculate by Newton’s iteration, starting from x 0 = 2 and calculating x1,
x2, x3. Compare the results with the value 2.645751
7. Design a Newton’s iteration for computing k-th root of a positive
number c.
8. Find all real solutions of the following equations by Newton’s iteration
45
metho, Chord Method and Combined Method.
a) sin(x)=x b) ln(x)=1-2x c) cos(x)=√ x
Examples of answers:
1. Ans: An equation f (x)=0 is called an algebraic equation if the
corresponding f (x) is a polynomial, while, f (x)=0 is called transcendental
equation if the f (x) contains trigonometric, or exponential or logarithmic
functions.
2. Ans: As analytic solutions are often either too tiresome or simply do not
exist, we need to find an approximate method of solution. This is where
numerical analysis comes into the picture.
3. Ans: These methods are based on the intermediate value theorem for
continuous functions: stated as , “If f is a continuous function
and f (a) and f(b) have opposite signs, then at least one root lies in between
a and b. If the interval is small enough, it is likely to contain a single root. ”
4. Ans: (i) The bisection and regula-falsi method is always convergent. Since
the method brackets the root, the method is guaranteed to converge. The
main disadvantage is, if it is not possible to bracket the roots, the
methods cannot applicable. For example, if f ()x is such that it always
takes the values with same sign, say, always positive or always negative,
then we cannot work with bisection method. Some examples of such
functions are
f (x)=x2 which take only non-negative values and
f (x)=-x2 which take only non-positive values.
5. Ans: For finding roots of a nonlinear equation f (x) = 0, bracketing method
requires two guesses which contain the exact root. But in open method initial
guess of the root is needed without any condition of bracketing for starting
the iterative process to find the solution of an equation.
6. Ans: Newton-Raphson method requires the evaluation of derivatives of the
function and this is not always possible, particularly in the case of functions
arising in practical problems. In such situations Secant method helps to solve
the equation with an approximation to the derivative.
46
Contents
Module 1 (3-44)
Introduction
1. Part 1
1.1. Isolation of real roots
1.2. Bisection Method
1.3. Laboratory work №1 Using the Bisection Method to Solve Algebraic
Equations
1.4. Typical solution examples
1.5.
2. Part 2
4.1 Chord Method ( Method of false position)
4.2 Newton-Raphson Method and modification
4.3 Secant Method
4.4 Combined Method
4.5 Error of numerical solutions of the equation
4.6 Laboratory work №2 Using Chord Method, Newton-Raphson
Method, and Combined Method to Solve Algebraic Equations
4.7 Typical solution examples
3. Part 3
3.1 Fixed Point Iteration Method
3.2 Laboratory work №3 Using Fixed-point Iteration Method to Solve
Algebraic and Transcendental Equations
3.3 Typical version examples
4. Question and exercises for Module 1
47