0% found this document useful (0 votes)
9 views6 pages

LAB 4 - Jupyter Notebook

Python Complex Analysis Lab 4
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)
9 views6 pages

LAB 4 - Jupyter Notebook

Python Complex Analysis Lab 4
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/ 6

14 December 2024

LAB 4 ¶

NEWTON RAPHSON METHOD

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)

f(x)= x**3 - 5*x**2


df(x)/dx= 3*x**2 - 10*x
Initial guess= 0
Derivative is 0,can't compute

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 ​

f(x) = x**3 - 5*x**2


df(x)/d(x) = 3*x**2 - 10*x
Initial Guess: 0
Derivative is 0, can't compute further.

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)

f(x)= x**3 - 2*x**2 + x + 1


df(x)/dx= 3*x**2 - 4*x + 1
Initial guess= 0
Iteration no: 1 x= -1.00000000000000
Iteration no: 2 x= -0.625000000000000
Iteration no: 3 x= -0.485785953177258
Iteration no: 4 x= -0.465955919735989
Iteration no: 5 x= -0.465571374907092
Root: -0.465571374907092

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)

f(x)= -x**2 + sin(x) + 1


df(x)/dx= -2*x + cos(x)
Initial guess= 0.5
Iteration no: 1 x= 10.5428955023812
Iteration no: 2 x= 5.38324944022858
Iteration no: 3 x= 2.54804796530244
Iteration no: 4 x= 1.71544102182517
Iteration no: 5 x= 1.44881875199292
Iteration no: 6 x= 1.41045157893933
Iteration no: 7 x= 1.40962438837635
Root: 1.40962438837635

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)

f(x)= -3*x**2 + exp(x)


df(x)/dx= -6*x + exp(x)
Initial guess= 1
Iteration no: 1 x= 0.914155281832543
Iteration no: 2 x= 0.910017665783406
Iteration no: 3 x= 0.910007572548888
Root: 0.910007572548888

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)

f(x)= x**2 + log(x) - 4


df(x)/dx= 2*x + 1/x
Initial guess= 2
Iteration no: 1 x= 1.84596729320890
Iteration no: 2 x= 1.84110183746662
Iteration no: 3 x= 1.84109705845469
Root: 1.84109705845469

In [ ]: 1 ​

In [ ]: 1 ​

You might also like