LAB 4 - Jupyter Notebook
LAB 4 - Jupyter Notebook
LAB 4 ¶
Write the python code to find the root of f(x)=x^3-5*x^2=0 using newton raphson method
In [7]: 1 x=symbols("x")
2 def NR(f,x0,e):
3 it=1
4 d=diff(f,x)
5 print("f(x)=",f)
6 print("df(x)/dx=",d)
7 print("Initial guess=",x0)
8 x0=float(x0)
9 cond=True
10 while cond:
11 if d.subs({x:x0})==0:
12 print("Derivative is 0,can't compute")
13 break
14 h=f.subs({x:x0})/d.subs({x:x0})
15 x1=x0-h
16 x0=x1
17 print("Iteration no:\t",it,"\t x=",x0)
18 it+=1
19 cond=np.abs(f.subs({x:x1}))>e
20 if it>25:
21 print("Solution diverges")
22 break
23 return x0
24 NR(x**3-5*x**2,0,0.000005)
Out[7]: 0.0
In [5]: 1 from sympy import symbols, diff
2 import numpy as np
3
4 x = symbols("x") # Define the symbol x
5
6 def NR(f, x0, e):
7 """
8 Newton-Raphson Method for solving f(x) = 0
9 Parameters:
10 f: sympy expression - The function whose root is to be determined.
11 x0: float - Initial guess for the root.
12 e: float - Tolerance level for stopping criterion.
13
14 Returns:
15 x0: float - Approximation of the root.
16 """
17 it = 1 # Iteration counter
18 d = diff(f, x) # Derivative of the function
19 print("f(x) =", f)
20 print("df(x)/d(x) =", d)
21 print("Initial Guess:", x0)
22
23 # Ensure x0 is a floating-point number
24 x0 = float(x0)
25 cond = True # Condition to continue iteration
26
27 while cond:
28 if d.subs({x: x0}) == 0: # Check if derivative is zero
29 print("Derivative is 0, can't compute further.\n")
30 break
31 h = f.subs({x: x0}) / d.subs({x: x0}) # Compute h = f(x0)/f'(x0)
32 x1 = x0 - h # Update guess
33 print(f"Iteration no: {it}, x = {x1}")
34
35 # Check convergence criterion
36 cond = np.abs(f.subs({x: x1})) > e
37 x0 = float(x1) # Update x0 for the next iteration
38 it += 1
39
40 if it > 25: # Stop if the method does not converge within 25 iterations
41 print("Solution diverges or did not converge within 25 iterations.")
42 return None
43
44 return x0
45
46 # Example usage
47 result = NR(x**3 - 5*x**2, 0, 0.000005)
48 print("Root:", result)
49
Root: 0.0
Write the python code to find the root of f(x)=x^3-2*x^2+x+1=0 using newton raphson
method
In [16]: 1 x=symbols("x")
2 def NR(f,x0,e):
3 it=1
4 d=diff(f,x)
5 print("f(x)=",f)
6 print("df(x)/dx=",d)
7 print("Initial guess=",x0)
8 x0=float(x0)
9 cond=True
10 while cond:
11 if d.subs({x:x0})==0:
12 print("Derivative is 0,can't compute")
13 break
14 h=f.subs({x:x0})/d.subs({x:x0})
15 x1=x0-h
16 x0=x1
17 print("Iteration no:\t",it,"\t x=",x0)
18 it+=1
19 cond=np.abs(f.subs({x:x1}))>e
20 if it>25:
21 print("Solution diverges")
22 break
23 return x0
24 result = NR(x**3 - 2*x**2+x+1, 0, 0.000005)
25 print("Root:", result)
Write the python code to find the root of f(x)=sin(x)+x**-1=0 using newton raphson method
using x0=.5
In [13]: 1 x=symbols("x")
2 def NR(f,x0,e):
3 it=1
4 d=diff(f,x)
5 print("f(x)=",f)
6 print("df(x)/dx=",d)
7 print("Initial guess=",x0)
8 x0=float(x0)
9 cond=True
10 while cond:
11 if d.subs({x:x0})==0:
12 print("Derivative is 0,can't compute")
13 break
14 h=f.subs({x:x0})/d.subs({x:x0})
15 x1=x0-h
16 x0=x1
17 print("Iteration no:\t",it,"\t x=",x0)
18 it+=1
19 cond=np.abs(f.subs({x:x1}))>e
20 if it>25:
21 print("Solution diverges")
22 break
23 return x0
24 result = NR(sin(x)-x**2+1, 0.5, 0.000005)
25 print("Root:", result)
Write the python code to find the root of f(x)=exp(x)-3x*2=0 using newton raphson method using
x0=1
In [19]: 1 x=symbols("x")
2 def NR(f,x0,e):
3 it=1
4 d=diff(f,x)
5 print("f(x)=",f)
6 print("df(x)/dx=",d)
7 print("Initial guess=",x0)
8 x0=float(x0)
9 cond=True
10 while cond:
11 if d.subs({x:x0})==0:
12 print("Derivative is 0,can't compute")
13 break
14 h=f.subs({x:x0})/d.subs({x:x0})
15 x1=x0-h
16 x0=x1
17 print("Iteration no:\t",it,"\t x=",x0)
18 it+=1
19 cond=np.abs(f.subs({x:x1}))>e
20 if it>25:
21 print("Solution diverges")
22 break
23 return x0
24 result = NR(exp(x)-3*x**2, 1, 0.000005)
25 print("Root:", result)
Write the python code to find the root of f(x)=log(x)+x**2-4=0 using newton raphson method
using x0=2
In [20]: 1 x=symbols("x")
2 def NR(f,x0,e):
3 it=1
4 d=diff(f,x)
5 print("f(x)=",f)
6 print("df(x)/dx=",d)
7 print("Initial guess=",x0)
8 x0=float(x0)
9 cond=True
10 while cond:
11 if d.subs({x:x0})==0:
12 print("Derivative is 0,can't compute")
13 break
14 h=f.subs({x:x0})/d.subs({x:x0})
15 x1=x0-h
16 x0=x1
17 print("Iteration no:\t",it,"\t x=",x0)
18 it+=1
19 cond=np.abs(f.subs({x:x1}))>e
20 if it>25:
21 print("Solution diverges")
22 break
23 return x0
24 result = NR(log(x)+x**2-4, 2, 0.000005)
25 print("Root:", result)
In [ ]: 1
In [ ]: 1