Bisection Method for Root Finding
Bisection Method for Root Finding
wxmx 1 / 58
Bisection Methods:
bisection(f, a, b, tol) := block(
[c],
if float(f(a)) · float(f(b)) > 0 then error("f(a) and f(b) must have opposite signs"),
while abs(float(b − a)) > tol do (
c: float((a + b) / 2),
print("a: ", float(a), " b: ", float(b), " c: ", c, " f(c): ", float(f(c))),
if float(f(c)) = 0 then return(c),
if float(f(a)) · float(f(c)) < 0 then b: c else a: c
),
return(float((a + b) / 2))
);
a+b
return float )
2
44906.723860095175 7.853955078125
Third Root: 7.853955078125 7.853955078125
−4
5.451844176196374 10
−4
f(c): 6.343408529119188 10 − 2.912200927734375
a: − 2.0 b: 0.0 c: − 1.0 f(c): 3.0
a: − 1.0 b: 0.0 c: − 0.5 f(c): 0.875
a: − 0.5 b: 0.0 c: − 0.25 f(c): − 0.140625
a: − 0.5 b: − 0.25 c: − 0.375 f(c): 0.353515625
a: − 0.375 b: − 0.25 c: − 0.3125 f(c): 0.102294921875
a: − 0.3125 b: − 0.25 c: − 0.28125 f(c): − 0.020294189453125
a: − 0.3125 b: − 0.28125 c: − 0.296875 f(c):
0.040729522705078125
a: − 0.296875 b: − 0.28125 c: − 0.2890625 f(c):
0.01014852523803711
a: − 0.2890625 b: − 0.28125 c: − 0.28515625 f(c): −
0.005090296268463135
a: − 0.2890625 b: − 0.28515625 c: − 0.287109375 f(c):
0.0025247707962989807
a: − 0.287109375 b: − 0.28515625 c: − 0.2861328125 f(c): −
0.0012838514521718025
a: − 0.287109375 b: − 0.2861328125 c: − 0.28662109375 f(c):
−4
6.201878422871232 10
a: − 0.28662109375 b: − 0.2861328125 c: − 0.286376953125
−4
f(c): − 3.318998060422018 10
a: − 0.28662109375 b: − 0.286376953125 c: − 0.2864990234375
−4
f(c): 1.4412702330446336 10
a: − 0.2864990234375 b: − 0.286376953125 c: −
−5
0.28643798828125 f(c): − 9.389064075548958 10 −
0.286468505859375
a: 0.0 b: 2.0 c: 1.0 f(c): − 1.0
a: 1.0 b: 2.0 c: 1.5 f(c): 2.375
a: 1.0 b: 1.5 c: 1.25 f(c): 0.328125
a: 1.0 b: 1.25 c: 1.125 f(c): − 0.419921875
a: 1.125 b: 1.25 c: 1.1875 f(c): − 0.067626953125
a: 1.1875 b: 1.25 c: 1.21875 f(c): 0.124725341796875
a: 1.1875 b: 1.21875 c: 1.203125 f(c): 0.027179718017578125
a: 1.1875 b: 1.203125 c: 1.1953125 f(c): −
0.020564556121826172
a: 1.1953125 b: 1.203125 c: 1.19921875 f(c):
0.003222167491912842
a: 1.1953125 b: 1.19921875 c: 1.197265625 f(c): −
0.008692525327205658
a: 1.197265625 b: 1.19921875 c: 1.1982421875 f(c): −
Numerical [Link] 11 / 58
0.00274051446467638
a: 1.1982421875 b: 1.19921875 c: 1.19873046875 f(c):
−4
2.3949227761477232 10
a: 1.1982421875 b: 1.19873046875 c: 1.198486328125 f(c): −
0.0012508446088759229
a: 1.198486328125 b: 1.19873046875 c: 1.1986083984375 f(c):
−4
− 5.057595499238232 10
a: 1.1986083984375 b: 1.19873046875 c: 1.19866943359375
−4
f(c): − 1.3315448290995846 10 1.198699951171875
Root 1: − 2.912200927734375 − 2.912200927734375
Root 2: − 0.286468505859375 − 0.286468505859375
Root 3: 1.198699951171875 1.198699951171875
f(x) := sin(2·x+9);
wxplot2d(f(x), [x,−2,2]);
root: bisection(f, 0, 1, 1e-4);
print("Root: ", root);
f ( x ) := sin ( 2 x + 9 )
Numerical [Link] 12 / 58
Regula Falsi
regula_falsi(f, a, b, tol) := block(
[c],
if float(f(a)) · float(f(b)) > 0 then error("f(a) and f(b) must have opposite signs"),
while abs(float(b − a)) > tol do (
c: float(a − f(a) · (b − a) / (f(b) − f(a))),
print("a: ", float(a), " b: ", float(b), " c: ", float(c), " f(c): ", float(f(c))),
if float(f(c)) = 0 then return(c),
if float(f(a)) · float(f(c)) < 0 then b: c else a: c
),
return(float(c))
);
Numerical [Link] 14 / 58
regula_falsi
(f,a ,b ,tol):=block([c ],if float(f(a )) float(f(b ))>
−11
1.5707963268154532 f(c): − 2.0556606247014516 10
1.5707963268154532
Root: 1.5707963268154532 1.5707963268154532
−4
regula_falsi ( f , 0 , 1 , 1.0 10 )
−4
Root: regula_falsi ( f , 0 , 1 , 1.0 10 )
−4
regula_falsi ( f , 0 , 1 , 1.0 10 )
f(x ):=sin(2 x+ 9 )
regula_falsi
(f,0 ,1 ,0.01)
Root: regula_falsi
(f,0 ,1 ,0.01) regula_falsi
(f,0 ,1 ,0.01)
f(x) := x^3−2·x−5;
wxplot2d(f(x), [x,−5,5],[y,−5,5]);
root: regula_falsi_iter(f, 2, 3, 4);
print("Root: ", root);
Numerical [Link] 17 / 58
regula_falsi_iter
(f,a ,b ,max_iter):=block([c,iter
:0 ],if
(c:floata −
f(a) (b−a)
, print ( Iteration: , iter + 1 , a: , float ( a ) , b: ,
f ( b )− f ( a )
−6
float ( b ) , c: , c , f(c): , float ( f ( c ) ) ) , if float ( f ( c ) ) < 1.0 10 then
regula-falsi method.
f(x) := x·exp(x)−3;
wxplot2d(f(x), [x,−5,5],[y,−5,5]);
root: regula_falsi(f, 1, 2, 1e-4);
print("Root: ", root);
f ( x ) := x exp ( x ) − 3
plot2d: some values will be clipped.
−4
regula_falsi ( f , 1 , 2 , 1.0 10 )
−4
Root: regula_falsi ( f , 1 , 2 , 1.0 10 )
−4
regula_falsi ( f , 1 , 2 , 1.0 10 )
Secant Method
Numerical [Link] 19 / 58
do (x2 :floatx1 −
f ( x1 ) ( x1 − x0 )
, print
f ( x1 ) − f ( x0 )
d f ( x0 )
f ( x ) , x1 : float x0 − , while float ( f ( x1 ) ) > tol
dx ev ( f_prime , x = x0 )
f ( x0 )
float x0 − ) , return ( x1 ) )
ev ( f_prime , x = x0 )
1.6005988023952096
Root: 1.6005988023952096 1.6005988023952096
Trapezoidal Rule
trapezoidal_rule(f, a, b, n) := block(
[h, sum, i, x, I, err],
h: (b − a) / n,
sum: f(a) + f(b),
I: float(integrate(f(x), x, a, b)),
err: float(abs(I − h · sum / 2)),
h sum
float f ( x ) d x , err : float I − ,
2
a
h sum
print Approximation: , float , print ( Actual Value: , I ) ,
2
print ( Error: , err ) )
f(x) := (1+x)/(1+x^3);
trapezoidal_rule(f, 0, 1, 2);
1+x
f ( x ) :=
3
1+x
Approximation: 1.1666666666666667
Actual Value: 1.2091995761561454
Error: 0.04253290948947863 0.04253290948947863
h: (b − a) / n,
sum_even: 0,
sum_odd: 0,
simpsons_rule(f,a ,b ,n ):=block(
1/4
%e + 4 %e +1
− 1.4626517459071817
6
Euler's Method
euler_method(f, x0, y0, h, x_end) := block(
[x: float(x0), y: float(y0), steps: floor((x_end − x0) / h)],
print("Iteration: 0", " x: ", x, " y: ", y),
for i: 1 thru steps do (
y: y + h · f(x, y),
x: x + h,
print("Iteration: ", i, " x: ", x, " y: ", y)
)
);
euler_method ( f , x0 , y0 , h , x ) := block (
end
x − x0
x : float ( x0 ) , y : float ( y0 ) , steps : floor end ,
h
Numerical [Link] 36 / 58
h=1
f2(x, y) := x + 2 · y;
rk4_method(f2, 0, 0, 0.25, 1);
f2 ( x , y ) := x + 2 y
Iteration: 0 x: 0.0 y: 0.0
Iteration: 1 x: 0.25 y: 0.6301814371757238
Iteration: 2 x: 0.5 y: 1.060997592910006
Iteration: 3 x: 0.75 y: 1.3422547723035547
Iteration: 4 x: 1.0 y: 1.5121990856142926 done
n: length(b),
x: x0,
iter: 0,
for j: 1 thru n do (
if i # j then sum: sum + A[i][j] · x[j]
),
print("Iteration: ", iter + 1, " x_new: ", x_new, " Error: ", err),
3x1+x2+x3=1
x1+3x2+x3=1
x1+x2+3x3=1
gauss_jacobi(
[[3, 1, 1],
[1, 3, 1],
[1, 1, 3]],
[1, 1, 1],
[0, 0, 0],
1e-4,
10
);
1 1 1 1 1 1
Iteration: 1 x_new: , , Error: max , ,
3 3 3 3 3 3
1 1 1 2 2 2
Iteration: 2 x_new: , , Error: max , ,
9 9 9 9 9 9
7 7 7 4 4 4
Iteration: 3 x_new: , , Error: max , ,
27 27 27 27 27 27
13 13 13 8 8 8
Iteration: 4 x_new: , , Error: max , ,
81 81 81 81 81 81
55 55 55
Iteration: 5 x_new: , , Error:
243 243 243
16 16 16
max , ,
243 243 243
32 32 32
max , ,
729 729 729
64 64 64
max , ,
2187 2187 2187
Numerical [Link] 41 / 58
17 5 17 5
Iteration: 1 x_new: , ,3 Error: max , ,3
10 3 10 3
19 11
Iteration: 2 x_new: 0, ,− Error:
60 30
17 27 101
max , ,
10 20 30
1051 127 61
max , ,
600 90 20
13 1237 863
Iteration: 4 x_new: , ,− Error:
120 3600 1800
Gauss-Jacobi method.
8x1 + x2 -x3 = 2
2x1 + x2 + 9x3 = 12
gauss_jacobi(
[[8, 1, −1],
[−1, 7, −2],
[2, 1, 9]],
[2, 4, 12],
[0, 0, 0],
1e-4,
10
);
1 4 4 1 4 4
Iteration: 1 x_new: , , Error: max , ,
4 7 3 4 7 3
29 83 17 2 5 5
Iteration: 2 x_new: , , Error: max , ,
84 84 14 21 12 42
15 1 17
max , ,
224 49 252
83 407 121
max , ,
14112 14112 7056
67 5003 683
max , ,
1185408 2370816 395136
Numerical [Link] 44 / 58
1 2 3 1 2 3
Iteration: 1 x_new: − , ,− Error: max , ,
5 9 7 5 9 7
Numerical [Link] 45 / 58
46 64 163
Iteration: 2 x_new: , ,− Error:
315 315 315
109 2 4
max , ,
315 105 45
8 71 32
max , ,
175 567 315
22 8 479
max , ,
2025 2025 99225
2221 551 52
max , ,
496125 178605 14175
6071 211 17
max , ,
22325625 1607445 3472875
n: length(b),
x: x0,
iter: 0,
print ( Iteration: , iter + 1 , x: , x , Error: , err ) , if err < tol then return ( x ) ,
x0 : x , iter : iter + 1 ) , return
( Solution did not converge within the maximum iterations. ) )
20 335
Iteration: 1 x: 2, , Error: max ( [ 0 , 0 , 0 ] )
9 63
4 566 17821
Iteration: 2 x: − ,− , Error: max ( [ 0 , 0 , 0 ] )
7 567 3969
max ( [ 0 , 0 , 0 ] )
max ( [ 0 , 0 , 0 ] )
Error: max ( [ 0 , 0 , 0 ] )
Iteration: 7 x:
38016550906 6351648170584 460964696354627
,− , Error:
173676395025 14067787997025 98474515979175
Numerical [Link] 48 / 58
max ( [ 0 , 0 , 0 ] )
11919700475176 2004245895720538
Iteration: 8 x: [ ,− ,
54708064432875 4431353219062875
145195906071843593
] Error: max ( [ 0 , 0 , 0 ] )
31019472533440125
751836321350174 126211428142757924
Iteration: 9 x: [ ,− ,
3446608059271125 279175252800961125
1829494131263664659
] Error: max ( [ 0 , 0 , 0 ] )
390845353921345575
236756264492215292
Iteration: 10 x: [ ,−
1085681538670404375
39761120112605701766 2881442890177254631261
, ] Error:
87940204632302754375 615581432426119280625
max ( [ 0 , 0 , 0 ] )
Solution did not converge within the maximum iterations.
17 49 29
Iteration: 1 x: , , Error: max ( [ 0 , 0 , 0 ] )
10 60 60
757 3439 2819
Iteration: 2 x: , , Error: max ( [ 0 , 0 , 0 ] )
600 3600 3600
39607 212989 197369
Iteration: 3 x: , , Error: max ( [ 0 , 0 , 0 ] )
36000 216000 216000
Numerical [Link] 49 / 58
gauss_seidel(
[[3, 1, 1],
[1, 3, 1],
[1, 1, 3]],
[1, 1, 1],
[0, 0, 0],
1e-4,
10
);
Iteration:1 x: 1 2 4
, , Error: max ( [ 0 , 0 , 0 ] )
3 9 27
17 52 140
Iteration: 2 x: , , Error: max ( [ 0 , 0 , 0 ] )
81 243 729
433 1334 3928
Iteration: 3 x: , , Error: max ( [ 0 , 0 , 0 ] )
2187 6561 19683
11753 35512 106376
Iteration: 4 x: , , Error: max ( [ 0 , 0 , 0 ] )
59049 177147 531441
318529 956666 2870716
Iteration: 5 x: , , Error:
1594323 4782969 14348907
max ( [ 0 , 0 , 0 ] )
8608193 25826380 77489204
Iteration: 6 x: , , Error:
43046721 129140163 387420489
max ( [ 0 , 0 , 0 ] )
232452145 697341710 2092086256
Iteration: 7 x: , , Error:
1162261467 3486784401 10460353203
max ( [ 0 , 0 , 0 ] )
6276241817 18828559024 56485894352
Iteration: 8 x: , ,
31381059609 94143178827 282429536481
Error: max ( [ 0 , 0 , 0 ] )
169457965057 508372961330 1525118971828
Iteration: 9 x: , ,
847288609443 2541865828329 7625597484987
Error: max ( [ 0 , 0 , 0 ] )
Iteration: 10 x:
4575359629169 13726075910308 41178222567068
, , Error:
22876792454961 68630377364883 205891132094649
max ( [ 0 , 0 , 0 ] )
Solution did not converge within the maximum iterations.
Numerical [Link] 51 / 58
Gauss-Seidel method
-3x1 + 9x2 + x3 = 2
2x1 - x2 - 7x3 = 3
gauss_seidel(
[[5, −2, 3],
[−3, 9, 1],
[2, −1, −7]],
[−1, 2, 3],
[0, 0, 0],
1e-4,
10
);
1 7 32
Iteration: 1 x: − , ,− Error: max ( [ 0 , 0 , 0 ] )
5 45 63
max ( [ 0 , 0 , 0 ] )
max ( [ 0 , 0 , 0 ] )
Error: max ( [ 0 , 0 , 0 ] )
Iteration: 6 x:
2886067931153 6603853889237 82592384208406
, ,− Error:
15506820984375 19937341265625 195385944403125
max ( [ 0 , 0 , 0 ] )
909133722255691 2080215427447639
Iteration: 7 x: [ , ,−
4884648610078125 6280262498671875
26016522692728082
] Error: max ( [ 0 , 0 , 0 ] )
61546572486984375
Numerical [Link] 52 / 58
286376089845867977 655267137315003533
Iteration: 8 x: [ , ,−
1538664312174609375 1978282687081640625
8195207354538770854
] Error: max ( [ 0 , 0 , 0 ] )
19387170333400078125
90208438107951518419 206409144979618247551
Iteration: 9 x: [ ,
484679258335001953125 623159046430716796875
2581490420791850627138
,− ] Error: max ( [ 0 , 0 , 0 ] )
6106958655021024609375
28415659244775224620193
Iteration: 10 x: [ ,
152673966375525615234375
65018881572167595715397 813169479347682147634486
,− ]
196295099625675791015625 1923691976331622751953125
Error: max ( [ 0 , 0 , 0 ] )
Solution did not converge within the maximum iterations.
1 17 305
Iteration: 1 x: , , Error: max ( [ 0 , 0 , 0 ] )
4 28 252
41 425 4580
Iteration: 2 x: , , Error: max ( [ 0 , 0 , 0 ] )
126 441 3969
8693 208981 2336485
Iteration: 3 x: , , Error: max ( [ 0 , 0 , 0 ] )
31752 222264 2000376
557051 13231525 147001345
Iteration: 4 x: , , Error:
2000376 14002632 126023688
Numerical [Link] 53 / 58
max ( [ 0 , 0 , 0 ] )
69991249 1666186133 18525915965
Iteration: 5 x: , , Error:
252047376 1764331632 15878984688
max ( [ 0 , 0 , 0 ] )
1102756567 52489398475 583550774545
Iteration: 6 x: , ,
3969746172 55576446408 500188017672
Error: max ( [ 0 , 0 , 0 ] )
Iteration: 7 x:
555761111807 13227175590919 147055342781575
, , Error:
2000752070688 14005264494816 126047380453344
max ( [ 0 , 0 , 0 ] )
Iteration: 8 x:
35013190421249 833313397797775 9264481894385635
, , Error:
126047380453344 882331663173408 7940984968560672
max ( [ 0 , 0 , 0 ] )
4411657812831751 104997465138859667
Iteration: 9 x: [ , ,
15881969937121344 111173789559849408
1167324800199688715
] Error: max ( [ 0 , 0 , 0 ] )
1000564106038644672
69483619563351283 3307420251403680625
Iteration: 10 x: [ , ,
250141026509661168 3501974371135256352
36770730854445559675
] Error: max ( [ 0 , 0 , 0 ] )
31517769340217307168
Solution did not converge within the maximum iterations.
Langrange Interpolation
Numerical [Link] 54 / 58
for i: 1 thru n do (
L: 1,
for j: 1 thru n do (
if i # j then L: expand(L · (x − x_vals[j]) / (x_vals[i] − x_vals[j]))
),
P: expand(P + y_vals[i] · L)
),
lagrange_interpolation
(x , y , eval_point ):= block (
vals vals
[ n , L , P , x , i , j , result ] , n : length ( x ) , P : 0 , for i thru n do ( L : 1 , for j
vals
L x − xvals
j
thru n do if i ≠ j then L : expand ,P:
x −x
vals vals
i j
f(1)=1
f(3)=27
f(5)=55
2
[1 ,3 ,5 ] [1 ,27 ,55 ] 1 ,13 , 1 x
+ 12
4 4
45
x− 20.3125
4
2 2
x 45 x 45
Interpolating Polynomial: + 12 x − + 12 x −
4 4 4 4
f(2.5): 20.3125 20.3125
[1,2,3] [ 5 , 10 , 15 ] [5,5,0] 5x
5
Interpolating Polynomial: 5 x 5x
f(1): 5 5
Numerical [Link] 58 / 58
interpolation
EVALUATE f(3.5).
3
1 1 x
[0,1,2,3] [1,2,4,8] 1,1, ,
2 6 6
5x
+ +1 11.0625
6
3 3
x 5x x 5x
Interpolating Polynomial: + +1 + +1
6 6 6 6
f(3.5): 11.0625 11.0625