Solution Set
Solution Set
1 def f ( x ) :
2 return x **3 - 4* x - 9
3
4 a , b = 2.0 , 3.0
5 tol = 1e -5
6
7 if f ( a ) * f ( b ) > 0:
8 print ( " Root is not bracketed . " )
9 else :
10 while ( b - a ) / 2.0 > tol :
11 mid = ( a + b ) / 2.0
12 if f ( mid ) == 0:
13 break
14 elif f ( a ) * f ( mid ) < 0:
15 b = mid
16 else :
17 a = mid
18 root = ( a + b ) / 2.0
19 print ( f " Root : { root :.5 f } " )
1
Problem 2: Newton-Raphson Method
Question: Apply the Newton-Raphson method to determine the root of e−x − x = 0
starting from x0 = 0.5.
Explanation: Let f (x) = e−x − x. The first derivative is f ′ (x) = −e−x − 1. The
Newton-Raphson update formula is:
f (xn )
xn+1 = xn −
f ′ (xn )
We iterate this formula starting from x0 = 0.5 until the difference between successive ap-
proximations is negligible.
Python Code:
1 import math
2
3 def f ( x ) :
4 return math . exp ( - x ) - x
5
6 def df ( x ) :
7 return - math . exp ( - x ) - 1
8
9 x0 = 0.5
10 tol = 1e -6
11
12 while True :
13 x1 = x0 - f ( x0 ) / df ( x0 )
14 if abs ( x1 - x0 ) < tol :
15 break
16 x0 = x1
17
2
We use the initial guesses x0 = 0 and x1 = 1 and iterate until convergence.
Python Code:
1 import math
2
3 def f ( x ) :
4 return math . cos ( x ) - x
5
6 x0 = 0.0
7 x1 = 1.0
8 tol = 1e -6
9
1 import math
2
3 def f ( k ) :
4 return k * math . tan ( k ) - math . sqrt (25 - k **2)
5
6 a , b = 1.0 , 1.5
7 tol = 1e -5
8
3
15
16 k_lowest = ( a + b ) / 2.0
17 print ( f " Lowest allowed k : { k_lowest :.5 f } " )
1 import numpy as np
2
9 # Forward Elimination
10 for i in range ( n ) :
11 for j in range ( i +1 , n ) :
12 factor = A [ j ][ i ] / A [ i ][ i ]
13 for k in range (i , n ) :
14 A [ j ][ k ] -= factor * A [ i ][ k ]
15 B [ j ] -= factor * B [ i ]
16
17 # Back Substitution
18 x = np . zeros ( n )
19 x [n -1] = B [n -1] / A [n -1][ n -1]
20
4
Problem 6: Gauss-Seidel Method
Question: Solve the system 10x − y + 2z = 6, −x + 11y − z = 25, 2x − y + 10z = −11
using the Gauss-Seidel method. Perform four iterations starting from (0, 0, 0).
Explanation: The Gauss-Seidel method is an iterative technique. First, we rewrite the
system by isolating x, y, and z on the left side:
1
x= (6 + y − 2z)
10
1
y = (25 + x + z)
11
1
z = (−11 − 2x + y)
10
Starting with initial guesses x0 = 0, y0 = 0, z0 = 0, we update each variable sequentially,
using the most recently calculated values of the other variables immediately in the next step.
Python Code:
1 x1 , T1 = 2.0 , 65.0
2 x2 , T2 = 3.0 , 50.0
3 x_target = 2.5
5
4
5 T_target = T1 + (( T2 - T1 ) / ( x2 - x1 ) ) * ( x_target - x1 )
6 print ( f " Estimated Temperature at x =2.5: { T_target :.1 f } C " )
For our three points, we calculate L0 (1.5), L1 (1.5), and L2 (1.5) and sum their products with
the corresponding y values.
Python Code:
5 result = 0.0
6 n = len ( x_data )
7
8 for i in range ( n ) :
9 term = y_data [ i ]
10 for j in range ( n ) :
11 if j != i :
12 term *= ( x_target - x_data [ j ]) / ( x_data [ i ] - x_data [ j ])
13 result += term
14
6
• Velocity (1st derivative): v(t) ≈ x(t+h)−x(t−h)
2h
1 t = [0 , 1 , 2 , 3 , 4]
2 x = [0 , 2 , 8 , 18 , 32]
3 h = 1.0
4 index = 2 # t =2 is at index 2
5
" n−1
#
h X
I≈ f (x0 ) + 2 f (xi ) + f (xn )
2 i=1
For n = 4, h = (1 − 0)/4 = 0.25. To determine the required n for 10−4 accuracy, we use the
3
error bound |E| ≤ (b−a)
12n2
max |f ′′ (x)|. Since max |f ′′ (x)| for 1+x
1
2 on [0, 1] is 2 (at x = 0), we
2 −4
solve 12n2 ≤ 10 , which yields n ≥ 41.
Python Code:
1 def f ( x ) :
2 return 1.0 / (1.0 + x **2)
3
4 a , b = 0.0 , 1.0
5 n = 4
6 h = (b - a) / n
7
8 integral = f ( a ) + f(b)
9 for i in range (1 , n):
10 x_i = a + i * h
11 integral += 2 * f ( x_i )
7
12
13 integral *= h / 2.0
14
Rπ
The exact analytical value is 0
sin x dx = [− cos x]π0 = −(−1) − (−1) = 2.
Python Code:
1 import math
2
3 def f ( x ) :
4 return math . sin ( x )
5
6 a , b = 0.0 , math . pi
7 n = 6
8 h = (b - a) / n
9
10 integral = f ( a ) + f ( b )
11 for i in range (1 , n ) :
12 x_i = a + i * h
13 if i % 2 == 0:
14 integral += 2 * f ( x_i )
15 else :
16 integral += 4 * f ( x_i )
17
18 integral *= h / 3.0
19 exact = 2.0
20 error = abs ( exact - integral )
21
8
Problem 12: Normalization Integral of a Gaussian
R∞ 2
Question: Compute the normalization integral I = −∞
e−x dx using the trapezoidal
method over the interval [−4, 4]. Choose suitable h to get the result accurate up to third
decimal place.
Explanation: We approximate the infinite limits with [−4, 4] since e−16 is negligible.
To guarantee an accuracy of 5 × 10−4 (third decimal place), we use the error bound for
2 2
the trapezoidal rule: |E| ≤ (b−a)h
12
max |f ′′ (x)|. For f (x) = e−x , the second derivative is
2
f ′′ (x) = e−x (4x2 − 2), which has a maximum absolute value of 2 at x = 0.
(4 − (−4))h2 16h2 √
· 2 ≤ 0.0005 =⇒ ≤ 0.0005 =⇒ h ≤ 0.000375 ≈ 0.019
12 12
Choosing h = 0.01
√ (or n = 800) will safely achieve the desired accuracy. The exact value of
the integral is π ≈ 1.772.
Python Code:
1 import math
2
3 def f ( x ) :
4 return math . exp ( - x **2)
5
6 a , b = -4.0 , 4.0
7 h = 0.01 # Chosen to ensure 3 rd decimal accuracy
8 n = int (( b - a ) / h )
9
10 integral = f ( a ) + f(b)
11 for i in range (1 , n):
12 x_i = a + i * h
13 integral += 2 * f ( x_i )
14
15 integral *= h / 2.0
16 exact = math . sqrt ( math . pi )
17
9
method up to x = 0.5 with step size h = 0.1.
Explanation: Euler’s method estimates the next value using the tangent at the current
point:
yn+1 = yn + h · f (xn , yn )
Given f (x, y) = x + y, x0 = 0, y0 = 1, h = 0.1, we perform 5 iterations to reach x = 0.5.
Python Code:
1 def f (x , y ) :
2 return x + y
3
4 x0 , y0 = 0.0 , 1.0
5 x_end = 0.5
6 h = 0.1
7
8 x , y = x0 , y0
9 n_steps = int (( x_end - x0 ) / h )
10
k1 = hf (tn , yn )
h k1
k2 = hf tn + , yn +
2 2
h k2
k3 = hf tn + , yn +
2 2
k4 = hf (tn + h, yn + k3 )
1
yn+1 = yn + (k1 + 2k2 + 2k3 + k4 )
6
Python Code:
10
1 def f (t , y ) :
2 return -2.0 * y
3
4 t0 , y0 = 0.0 , 1.0
5 h = 0.2
6
7 k1 = h * f ( t0 , y0 )
8 k2 = h * f ( t0 + h /2.0 , y0 + k1 /2.0)
9 k3 = h * f ( t0 + h /2.0 , y0 + k2 /2.0)
10 k4 = h * f ( t0 + h , y0 + k3 )
11
12 y1 = y0 + (1.0/6.0) * ( k1 + 2* k2 + 2* k3 + k4 )
13 print ( f " y (0.2) approx = { y1 :.5 f } " )
1 import numpy as np
2
11
10 def rk4_step (F , t , Y , h ) :
11 k1 = h * F (t , Y )
12 k2 = h * F ( t + h /2 , Y + k1 /2)
13 k3 = h * F ( t + h /2 , Y + k2 /2)
14 k4 = h * F ( t + h , Y + k3 )
15
16 Y_next = Y + (1.0/6.0) * ( k1 + 2* k2 + 2* k3 + k4 )
17 return Y_next
18
19 # Example initialization
20 t0 = 0.0
21 Y0 = np . array ([1.0 , 0.0]) # Initial position x =1 , velocity v =0
22 h = 0.1
23
24 Y1 = rk4_step (F , t0 , Y0 , h )
25 print ( f " After one step : x ={ Y1 [0]:.4 f } , v ={ Y1 [1]:.4 f } " )
1 import numpy as np
2
3 h = 0.2
4 x = np . arange (0 , 2.0 + h , h )
5 V = np . zeros ( len ( x ) )
6
7 # Initial Conditions
8 V [0] = 0.0
12
9 V [1] = V [0] + h * 1.0 # From V ’(0) = 1.0
10
11 # Forward Stepping
12 for i in range (1 , len ( x ) - 1) :
13 V [ i +1] = 2* V [ i ] - V [i -1] - h **2
14
1 import math
2
3 N0 = 1000.0
4 lam = 0.3
5
Python Code:
1 import numpy as np
2
10 for i in range ( N ) :
11 H [i , i ] = diagonal_val
12 if i > 0:
13 H [i , i -1] = off_diagonal_val
14 if i < N - 1:
15 H [i , i +1] = off_diagonal_val
16
−Vi−1
• Central difference for interior points (x = 1, 2, 3): Ei ≈ − Vi+12h
Python Code:
1 x = [0 , 1 , 2 , 3 , 4]
2 V = [10.0 , 8.0 , 5.0 , 2.0 , 1.0]
3 h = 1.0
4 E = [0.0] * len ( x )
5
14
7 E [0] = -( V [1] - V [0]) / h
8 E [ -1] = -( V [ -1] - V [ -2]) / h
9
dx dvx
= vx , =0
dt dt
dy dvy
= vy , = −g
dt dt
Using Euler’s method with a time step ∆t, the updates are: xnew = x+vx ∆t, ynew = y+vy ∆t,
and vy,new = vy − g∆t. We stop the simulation when the projectile hits the ground (y < 0).
Python Code:
1 import math
2
3 g = 9.8
4 v0 = 20.0
5 theta = math . radians (45)
6 dt = 0.05
7
8 x , y = 0.0 , 0.0
9 vx = v0 * math . cos ( theta )
10 vy = v0 * math . sin ( theta )
11 t = 0.0
12
15
Problem 21: Monte Carlo Estimation of π
Question: Generate 104 random numbers uniformly distributed between 0 and 1. Use
them to estimate the value of π using the Monte Carlo method.
Explanation: We can estimate π by generating random points (x, y) in a 1 × 1 square
where 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1. We then check if the point falls inside the quarter-circle
of radius r = 1 by evaluating the condition x2 + y 2 ≤ 1. The ratio of points inside the
quarter-circle (M ) to the total number of points (N ) will approximate the ratio of their
areas:
Area of quarter circle π(1)2 /4 π
= =
Area of square 12 4
Thus, π ≈ 4 M
N
.
Python Code:
1 import random
2
3 N = 10000
4 M = 0
5
6 for _ in range ( N ) :
7 x = random . uniform (0 , 1)
8 y = random . uniform (0 , 1)
9
13 pi_estimate = 4.0 * M / N
14 print ( f " Estimated value of pi : { pi_estimate :.4 f } " )
16
The average energy (expectation value of energy) is given by:
X 1X
⟨E⟩ = Ei Pi = Ei e−Ei /kB T
i
Z i
1 import math
2
6 Z = 0.0
7 for e in E :
8 Z += math . exp ( - e / kT )
9
10 avg_E = 0.0
11 for e in E :
12 P_i = math . exp ( - e / kT ) / Z
13 avg_E += e * P_i
14
We must choose an initial guess x0 . Since f (0) = −0.2 and f (1) = e−1 − 0.2 ≈ 0.16, a root
exists between 0 and 1. We will use x0 = 0.5.
Python Code:
1 import math
2
17
3 def f ( x ) :
4 return x * math . exp ( - x ) - 0.2
5
6 def df ( x ) :
7 return math . exp ( - x ) * (1.0 - x )
8
9 x0 = 0.5
10 tol = 1e -6
11
12 while True :
13 x1 = x0 - f ( x0 ) / df ( x0 )
14 if abs ( x1 - x0 ) < tol :
15 break
16 x0 = x1
17
1 import math
2
3 def f ( x ) :
4 return x - math . cos ( x )
5
6 a , b = 0.0 , 1.0
7 tol = 1e -5
8 iterations = 0
9
18
17 a = mid
18 iterations += 1
19
20 root = ( a + b ) / 2.0
21 print ( f " Root : { root :.5 f } found in { iterations } iterations . " )
1 import numpy as np
2 from scipy . optimize import fsolve
3
12 for j in range (1 , n ) :
13 for i in range ( n - j ) :
14 diff_table [ i ][ j ] = diff_table [ i +1][ j -1] - diff_table [ i ][ j -1]
15
23 for i in range (1 , n ) :
19
24 u_term *= ( u - ( i - 1) )
25 factorial *= i
26 result += ( u_term * diff_table [0][ i ]) / factorial
27 return result
28
By calculating the sums from the given data (n = 5), we can solve this 2 × 2 system of linear
equations to find the slope a and intercept b. Analytically, the solutions are:
P P P P P
n xy − x y y−a x
a= P P , b=
n x2 − ( x)2 n
Python Code:
5 sum_x = sum ( x )
6 sum_y = sum ( y )
7 sum_xx = sum ( xi **2 for xi in x )
8 sum_xy = sum ( xi * yi for xi , yi in zip (x , y ) )
9
20
10 # Using the analytical formulas
11 denominator = n * sum_xx - sum_x **2
12 a = ( n * sum_xy - sum_x * sum_y ) / denominator
13 b = ( sum_y - a * sum_x ) / n
14
1 import math
2
10 sum_t = sum ( t )
11 sum_Y = sum ( Y )
12 sum_tt = sum ( ti **2 for ti in t )
13 sum_tY = sum ( ti * Yi for ti , Yi in zip (t , Y ) )
14
20 I0 = math . exp ( B )
21 decay_constant = -A # This represents R / L
22
21
23 print ( f " Fitted Model : I ( t ) = { I0 :.2 f } * exp ( -{ decay_constant :.4 f } *
t)")
1 import math
2
3 def f ( x ) :
4 return 1.0 / (1.0 + x )
5
6 a , b = 0.0 , 3.0
7 exact = math . log (4.0)
8
14 for i in range (1 , n ) :
15 x_i = a + i * h
16 if i % 2 == 0:
17 integral += 2 * f ( x_i )
18 else :
19 integral += 4 * f ( x_i )
20
21 integral *= h / 3.0
22 error = abs ( exact - integral )
23 print ( f " { n }\ t { integral :.6 f }\ t { error :.2 e } " )
22
for 3rd decimal place accuracy? If not, find the required h.
Explanation: First, we evaluate the integral with h = 0.25. To check accuracy, we
2
analyze the error bound |E| ≤ (b−a)h 12
max |f ′′ (x)|. For f (x) = x2 e−x , the second derivative
is f ′′ (x) = (x2 −4x+2)e−x . On the interval [0, 2], max |f ′′ (x)| = 2 (at x = 0). The maximum
2
error for h = 0.25 is E ≤ (2)(0.25)
12
(2) ≈ 0.0208, which is much larger than 0.0005 (required for
2
3rd decimal place accuracy). To find the required h: 2·h 12
· 2 ≤ 0.0005 =⇒ h2 ≤ 0.0015 =⇒
h ≤ 0.0387. A step size of h = 0.02 or smaller is required.
Python Code:
1 import math
2
3 def f ( x ) :
4 return ( x **2) * math . exp ( - x )
5
6 a , b = 0.0 , 2.0
7 h = 0.25
8 n = int (( b - a ) / h )
9
10 integral = f ( a ) + f ( b )
11 for i in range (1 , n ) :
12 integral += 2 * f ( a + i * h )
13 integral *= h / 2.0
14
20 # Required h calculation
21 M = 2.0 # Max of | f ’ ’( x ) | on [0 , 2]
22 desired_error = 0.0005
23 required_h = math . sqrt ((12 * desired_error ) / (( b - a ) * M ) )
24 print ( f " Required step size h <= { required_h :.4 f } " )
23
(0)
1. Predictor: Estimate the next y using standard Euler: yn+1 = yn + hf (xn , yn )
(0)
2. Corrector: Refine the estimate: yn+1 = yn + h2 [f (xn , yn ) + f (xn+1 , yn+1 )]
Python Code:
1 def f (x , y ) :
2 return y - x **2
3
4 x = 0.0
5 y = 1.0
6 h = 0.1
7 x_target = 0.4
8
16 # Corrector step
17 x_next = x + h
18 slope2 = f ( x_next , y_predict )
19 y_next = y + ( h / 2.0) * ( slope1 + slope2 )
20
21 # Update variables
22 x = x_next
23 y = y_next
24 print ( f " x = { x :.1 f } , y = { y :.5 f } " )
24
1 import numpy as np
2 import math
3
4 def F (t , Y ) :
5 return np . array ([ Y [1] , -Y [0]])
6
7 def rk4_step (F , t , Y , h ) :
8 k1 = h * F (t , Y )
9 k2 = h * F ( t + h /2.0 , Y + k1 /2.0)
10 k3 = h * F ( t + h /2.0 , Y + k2 /2.0)
11 k4 = h * F ( t + h , Y + k3 )
12 return Y + (1.0/6.0) * ( k1 + 2* k2 + 2* k3 + k4 )
13
1 r = 0.5
2 K = 100.0
3 N = 10.0
4 t = 0.0
5 t_end = 10.0
6 h = 0.1
7
25
10 if round (t , 1) % 1.0 == 0: # Print every 1 time unit
11 print ( f " { t :.1 f }\ t { N :.2 f } " )
12
1 import numpy as np
2 # Note : In a real environment , uncomment the matplotlib lines to see
the plot
3 # import matplotlib . pyplot as plt
4
5 L = 1.0
6 x = np . linspace (0 , L , 100)
7
26
Problem 34: 1D Random Walk Simulation
Question: A particle performs a one-dimensional random walk. Write an algorithm to
simulate 1000 steps and calculate: (a) Mean displacement. (b) Mean square displacement.
Explanation: To find the statistical mean displacement ⟨x⟩ and mean square dis-
placement ⟨x2 ⟩, we must simulate an ensemble of many independent random walkers (e.g.,
M = 1000 particles), each taking N = 1000 steps. At each step, a particle moves either
+1 or −1 with equal probability. For an unbiased random walk, theory predicts: 1. Mean
displacement ⟨x⟩ ≈ 0 2. Mean square displacement ⟨x2 ⟩ ≈ N = 1000
Python Code:
1 import random
2
3 n_steps = 1000
4 n_particles = 1000 # Number of trials to average over
5
6 sum_x = 0.0
7 sum_x_squared = 0.0
8
16 sum_x += x
17 sum_x_squared += x **2
18
27
integration, the integral of f (x) over [a, b] can be approximated by generating N uniformly
distributed random numbers xi in [a, b] and computing the average function value:
N
1 X
I ≈ (b − a) f (xi )
N i=1
√
Here, f (x) = 1 − x2 , a = 0, and b = 1.
Python Code:
1 import random
2 import math
3
4 N = 100000
5 sum_f = 0.0
6
7 for _ in range ( N ) :
8 x = random . uniform (0 , 1)
9 f_x = math . sqrt (1.0 - x **2)
10 sum_f += f_x
11
12 integral = sum_f / N
13 pi_estimate = 4.0 * integral
14
We use the Boltzmann constant kB ≈ 8.617 × 10−5 eV/K to ensure consistent units (eV).
Python Code:
28
1 import math
2
3 kB = 8.617 e -5 # eV / K
4 epsilon = 1.0 # eV
5 temperatures = [300 , 1000]
6
7 for T in temperatures :
8 print ( f " --- Temperature : { T } K ---" )
9 kT = kB * T
10
11 # ( a ) Partition function
12 Z = 1.0 + math . exp ( - epsilon / kT )
13 print ( f " Partition Function Z : { Z :.5 e } " )
14
15 # ( b ) Probabilities
16 P0 = 1.0 / Z
17 P1 = math . exp ( - epsilon / kT ) / Z
18 print ( f " P ( State 0) : { P0 :.5 f } , P ( State 1) : { P1 :.5 e } " )
19
20 # ( c ) Average Energy
21 avg_E = epsilon * P1
22 print ( f " Average Energy : { avg_E :.5 e } eV \ n " )
d2 u
1 2
= −2 0.5 + u=− 1+ u
dr2 r r
1 h = 0.01
2 r_end = 5.0
3
29
4 # Start at r = h to avoid division by zero
5 r = h
6 u = h * 1.0 # u ( h ) approx h * u ’(0)
7 v = 1.0 # v = du / dr
8
18 u = u + h * du_dr
19 v = v + h * dv_dr
20 r = r + h
df
g(x) = = 2x − 4 = 0
dx
We check the bounds: g(0) = −4 and g(5) = 6. Since the signs are opposite, a root is
bracketed in [0, 5]. We apply the standard bisection algorithm to g(x).
Python Code:
1 def g ( x ) :
2 return 2 * x - 4
3
4 a , b = 0.0 , 5.0
5 tol = 1e -5
6
30
14 a = mid
15
16 min_x = ( a + b ) / 2.0
17 print ( f " Location of minimum : x = { min_x :.5 f } " )
f ′ (x) = 4x3 − 6x
f ′′ (x) = 12x2 − 6
We iterate this starting from x0 = 1. Note: f ′′ (x) > 0 at the root confirms it is a local
minimum.
Python Code:
1 def df ( x ) :
2 return 4 * x **3 - 6 * x
3
4 def ddf ( x ) :
5 return 12 * x **2 - 6
6
7 x0 = 1.0
8 tol = 1e -6
9 iterations = 0
10
11 while True :
12 x1 = x0 - df ( x0 ) / ddf ( x0 )
13 iterations += 1
14 if abs ( x1 - x0 ) < tol :
15 break
16 x0 = x1
17
31
Problem 40: Gradient Descent Optimization
Question: Use the gradient descent method to determine the minimum of f (x) = x2 +
3x + 2. Use learning rate η = 0.1 and initial value x0 = 5.
Explanation: Gradient descent minimizes a function by iteratively moving in the
direction of steepest descent (the negative gradient). The update rule is:
df
xn+1 = xn − η (xn )
dx
The derivative is f ′ (x) = 2x + 3. With η = 0.1 and x0 = 5, we iterate until the step size
becomes smaller than a defined tolerance.
Python Code:
1 def df ( x ) :
2 return 2 * x + 3
3
4 x = 5.0
5 eta = 0.1
6 tol = 1e -6
7 iterations = 0
8
9 while True :
10 gradient = df ( x )
11 step = eta * gradient
12 x_new = x - step
13 iterations += 1
14
32
derivative of the potential energy is zero: U ′ (x) = 4x3 − 4x = 0. To determine stability, we
evaluate the second derivative U ′′ (x) = 12x2 − 4 at these points. If U ′′ (x) > 0, the point
is a local minimum (stable equilibrium). If U ′′ (x) < 0, it is a local maximum (unstable
equilibrium). We can find the roots numerically by scanning the domain to bracket roots,
then using the Bisection or Newton-Raphson method.
Python Code:
1 def dU ( x ) :
2 return 4 * x **3 - 4 * x
3
4 def ddU ( x ) :
5 return 12 * x **2 - 4
6
18 stability = " Stable " if ddU ( x ) > 0 else " Unstable "
19 print ( f " x = { x :.4 f } \ t U ’ ’( x ) = { ddU ( x ) :.4 f } \ t ({ stability }) " )
1 import math
2
3 def df ( x ) :
4 return math . exp ( x ) + 2 * x
5
33
6 def ddf ( x ) :
7 return math . exp ( x ) + 2
8
13 while True :
14 x1 = x0 - df ( x0 ) / ddf ( x0 )
15 iterations += 1
16 if abs ( x1 - x0 ) < tol :
17 break
18 x0 = x1
19
1 def df ( x ) :
2 return 3 * x **2 - 12 * x + 9
3
9 while True :
10 gradient = df ( x )
11 step = eta * gradient
12 x_new = x - step
13 iterations += 1
14
34
16 break
17 x = x_new
18
1 import math
2
3 def dV ( x ) :
4 return math . cos ( x ) + x / 5.0
5
6 def ddV ( x ) :
7 return - math . sin ( x ) + 0.2
8
13 while True :
14 x1 = x0 - dV ( x0 ) / ddV ( x0 )
15 iterations += 1
16 if abs ( x1 - x0 ) < tol :
17 break
18 x0 = x1
19
35