0% found this document useful (0 votes)
13 views

2024 Week 10 Roots - Jupyter Notebook

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

2024 Week 10 Roots - Jupyter Notebook

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In [1]: 1 import numpy as np

𝑥3 + 3𝑥2 + 6𝑥 + 9 = 0 using Bisection−4 method.


Write a Python program to find the root of the equation
[−5,5]
Take the initial interval to be 𝑥[0] = −5,𝑥[1] = 5 . Take tolerance 𝑒𝑝𝑠 = 10 and 𝑁 = 10 iterations.
, that is,
What is the value of root rounded to two decimal places?

Bisection

𝑥𝑛+1 = 𝑥𝑛 +2𝑥𝑛−1
In [2]: 1 def bisection_method(f, a, b, tolerance, N=100):
2 x = np.zeros(N)
3 x[0] = a
4 x[1] = b
5 for i in range(2,N):
6 mid = (x[i-2] + x[i-1])/2
7 if (abs(mid-x[i-1]) < tolerance):
8 x[i] = mid
9 break
10 if f(x[i-2])*f(mid) > 0:
11 x[i] = mid
12 else:
13 x[i-1] = x[i-2]; x[i] = mid
14 return (i, x[i])
15 ​
16 def f(x):
17 return x**3 + 3*x**2 + 6*x + 9
18 ​
19 a = -5
20 b = 5
21 tolerance = 1e-4
22 N = 10
23 root = bisection_method(f, a, b, tolerance, N)[1]
24 round(root, 2)

Out[2]: -2.15

Write a python program to find the root of 𝑒𝑥 − 6 = 0, using−4newton-raphson method.


Take initial guess to be 𝑥 = 3.0 , tolerance to be 𝑒𝑝𝑠 = 10 and 𝑁 = 10 iterations.
What is the value of root rounded to 4 decimal places and at which index does the loop breaks due to tolerance condition being met?
Note: your loop will go from 𝑖 = 1 to 𝑁 , since 𝑥[0] = 3.0 is already given. You should have a tolerance condition 𝑎𝑏𝑠(𝑥[𝑖] − 𝑥[𝑖 − 1]) < 𝑒𝑝𝑠 ,
after calculating successive value of 𝑥 so that you can break out of the for loop if the tolerance condition is met.
When the condition is met break out of loop and store the answer as 𝑎𝑛𝑠 = 𝑥[𝑖] where 𝑖 is the loop index at which the for loop breaks.

Newton-Raphson

𝑥𝑛+1 = 𝑥𝑛 − 𝑓𝑓(′ (𝑥𝑥𝑛𝑛))


In [3]: 1 def newton_raphson(f, df, a, tolerance, N=100):
2 x = np.zeros(N)
3 x[0] = a
4 for i in range(1,N):
5 x[i] = x[i-1] - f(x[i-1])/df(x[i-1])
6 if (abs(x[i]-x[i-1]) < tolerance):
7 break
8 return (i, x[i])
9 ​
10 def f(x):
11 return np.exp(x) - 6
12 ​
13 def df(x):
14 return np.exp(x)
15 ​
16 x0 = 3.0
17 tol = 1e-4
18 N = 10
19 i, r = newton_raphson(f, df, x0, tol, N)
20 round( r, 4), i

Out[3]: (1.7918, 5)
Write a python program to find the root of the equation sin(𝑥) = 𝑥, using relaxation method.
Take the initial guess to be𝑥=1 𝑒𝑝𝑠 = 10−4 𝑁 = 1000
, tolerance to be and iterations. What is the value of the root rounded to 3 decimal places?.
𝑥
Hint : define array with size𝑁 and store𝑥[0] = 1 .
1 𝑁
Now enter a for loop from to 𝑥[𝑖] = 𝑓(𝑥[𝑖 − 1])
and calculate 𝑎𝑏𝑠(𝑥[𝑖] − 𝑥[𝑖 − 1]) < 𝑒𝑝𝑠
until the ,
when that happens break out of the loop and store the answer as 𝑎𝑛𝑠 = 𝑥[𝑖] .
Note: The value of 𝑁 is only given so that program does not go into infinite loop.
You will get the answer within given tolerance much earlier than loop completes.

In [4]: 1 def f(x):


2 return np.sin(x)
3 ​
4 x0 = 1.0
5 eps = 1e-4
6 N = 1000
7 x = np.zeros(N)
8 x[0] = x0
9 ​
10 for i in range(1, N):
11 x[i] = f(x[i-1])
12 if abs(x[i] - x[i-1]) < eps:
13 ans = x[i]
14 break
15 else:
16 ans = x[-1]
17 ​
18 round(ans,3)

Out[4]: 0.084

Write a python program to find the root of 𝑓(𝑥) = 𝑒𝑥𝑝(𝑥) − 6, using secant method.
𝑥0 = 3, 𝑥1 = 2.5,𝑒𝑝𝑠 = 10−4 and 𝑁 = 10 iterations.
Take the two initial points to be
𝑎
Let be the value of the root by the secant method.
Now you solved the same equation with newton-raphson method with initial value 3.0 , tolerance 10 and 10 iterations in question 4.
−4
Let 𝑏 be the answer by newton-raphson method (the complete answer not the rounded one).
Now define 𝑒 = 𝑏 ∗ 10 , What is the value of 𝑒 rounded to 4 decimal places?
𝑎−𝑏 5
Note: for secant method the loop will start from 𝑖 = 2 to 𝑁 . Since 𝑥[0] = 3.0 and 𝑥[1] = 2.5 are already given.
You should have a tolerance condition 𝑎𝑏𝑠(𝑥[𝑖] − 𝑥[𝑖 − 1]) < 𝑒𝑝𝑠 , after calculating successive value of 𝑥 so that
you can break out of the for loop if the tolerance condition is met.
When the condition is met break out of loop and store the answer as 𝑎𝑛𝑠 = 𝑥[𝑖] where 𝑖 is the loop index at which the for loop breaks.

Secant

𝑥𝑛+1 = 𝑥𝑛 − 𝑓(𝑥𝑛 ) 𝑓(𝑥𝑥𝑛𝑛) −− 𝑓(𝑥𝑛−1𝑥𝑛−1 )


In [5]: 1 def secant_method(f, a, b, tolerance, N=100):
2 x = np.zeros(N)
3 x[0] = a
4 x[1] = b
5 for i in range(2,N):
6 x[i] = x[i-1] - f(x[i-1])*(x[i-1]-x[i-2])/(f(x[i-1])-f(x[i-2]))
7 if (abs(x[i]-x[i-1]) < tolerance):
8 break
9 return (i,x[i])
10 ​
11 def f(x):
12 return np.exp(x) - 6
13 ​
14 def df(x):
15 return np.exp(x)
16 ​
17 x0 = 3
18 x1 = 2.5
19 tol = 1e-3
20 N = 10
21 ​
22 # secant
23 _, a = secant_method(f, x0, x1, tol, N)
24 ​
25 # newton
26 _, b = newton_raphson(f, df, x0, tol, N)
27 ​
28 # error
29 e = ((a - b) / b) * 1e5
30 round( e, 4)

Out[5]: 0.2572
Write a python program to find the roots of the equation 𝑓(𝑥) = 𝑠𝑖𝑛(𝑥) using bisection method. Take 𝑁 = 100 𝑒𝑝𝑠 = 10−4
, and .
𝑥
You have to find two roots, one negative and one positive. For the negative root take0 = −5, 𝑥 1 = 0
. For the positive root take𝑥0 = 1, 𝑥1 = 5.
Let 𝑟𝑝 be the positive root and 𝑟𝑛 be the negative root. Now define 𝑒 = 𝑟𝑛−𝑟𝑝 × 10 , What is the value of 𝑒 to 4 decimal places?
𝑟𝑛+𝑟𝑝 6

In [6]: 1 def f(x):


2 return np.sin(x)
3 ​
4
5 tolerance = 1e-4
6 N = 100
7 ​
8 # case 1
9 a = -5
10 b = 0
11 _, rn = bisection_method(f, a, b, tolerance, N)
12 print("estimated -ve root rn :", rn)
13 ​
14 # case 2
15 a = 1
16 b = 5
17 _, rp = bisection_method(f, a, b, tolerance, N)
18 print("estimated +ve root rp : ", rp)
19 ​
20 # error
21 e = ((rn + rp) / (rn - rp)) * 1e6
22 print(f"Estimated Error (e) : {e:.4f} ")

estimated -ve root rn : -3.1415557861328125


estimated +ve root rp : 3.14154052734375
Estimated Error (e) : 2.4285

𝑓(𝑥) = 𝑥2 − 1 , using the brentq function from scipy.optimize module. Take the interval to be
Write a python program to find the root of the function
[0.5,1.5] .
Let the value of root be 𝑟 . Now define 𝑒 = 𝑟 ∗ 10 , What is the value of 𝑒 rounded to three decimal places?
1−𝑟 15

In [7]: 1 from scipy.optimize import brentq


2 ​
3 def f(x):
4 return x**2 - 1
5 ​
6 x0, x1 = 0.5, 1.5
7 r = brentq(f, x0, x1)
8 e = ((1 - r) / r) * 1e15
9 round(e, 3)

Out[7]: 0.666

Write a python program to find the roots of the function 𝑓(𝑥) = 𝑥2 − 1 .


Use the brentq function in the interval[0.5,1.5] [−2,−0.5]
to find positive root and use the brenth function in the interval to find the negative root.
Both these functions are available in the scipy.optimize module. Let the value of positive root be 𝑟𝑝 𝑟𝑛
and the value of negative root be .
Now define 𝑒 = 𝑟𝑝+𝑟𝑛
𝑟𝑝−𝑟𝑛 ∗ 10 , What is the value of 𝑒 rounded to 4 decimal places?
15

In [8]: 1 from scipy.optimize import brentq, brenth


2 ​
3 def f(x):
4 return x**2 - 1
5 ​
6 rp = brentq(f, 0.5, 1.5)
7 rn = brenth(f, -2, -0.5)
8 e = ((rp + rn) / (rp - rn)) * 1e15
9 round( e, 4)

Out[8]: 4.4964

You might also like