Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

UNIVERSITY INSTITUTE OF ENGINEERING

Subject Name – Numerical Methods and Optimization Using Python

Course Code- 22CSH-259/22ITH-259

Submitted To: Submitted By:


Faculty Name: Neha Kapur Name: Sahil Singh
UID: 23BCS80031
Section:905
Group: B

1
Department of Computer Science & Engineering

INDEX
Exp. List of Experiments Conduct Viva Record Total Remarks
No (M.M:12) (M.M:10) (M.M:8) (M.M:30)

1. Library
Management

2. Write a program
using secant
method to find the
optimal pressure
of the car.

2
1. Source Code:

def secant method(f, x0, x1, e, max_iter=1000):


if f(x0) * f(x1) >= 0:
print ("Secant method fails: Initial points do not bracket the root.")
return None
iteration = 0
while abs(x1 - x0) >= e and iteration < max_iter:
x_temp = x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
x0, x1 = x1, x_temp
iteration += 1
if iteration == max_iter:
print("Secant method fails: Maximum iterations reached.")
return None
else:
return x1
from scipy.optimize import minimize_scalar
def fuel_efficiency(pressure):
return -0.5 * pressure + 40
result = minimize_scalar(lambda x: -fuel_efficiency(x), bounds=(5, 45), method='bounded')
if result.success:
optimal_pressure = result.x
max_fuel_efficiency = -result.fun
print(f"The optimal tire pressure that maximizes fuel efficiency is {optimal_pressure} psi.")
print(f"The maximum fuel efficiency achieved is {max_fuel_efficiency} mpg.")
else:
print("Unable to find optimal tire pressure. Adjust the initial interval or check the
function.")

3
2. Screenshot of Outputs:

3. Learning Outcomes

i. Implementing the Secant Method for Root-Finding.


ii. Utilizing Optimization Libraries for Efficiency.
iii. Function Definition and Function Calls.
iv. Handling Conditional Logic and Error Messages.

You might also like