0% found this document useful (0 votes)
66 views4 pages

Python Assignment: Numerical Methods

The document discusses using the secant method and optimization libraries in Python to find the optimal car pressure for maximum fuel efficiency. It includes the source code to implement the secant method, defines a fuel efficiency function, and uses scipy minimize_scalar to find the optimal pressure. Screenshots show the output of optimal pressure and maximum efficiency.
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)
66 views4 pages

Python Assignment: Numerical Methods

The document discusses using the secant method and optimization libraries in Python to find the optimal car pressure for maximum fuel efficiency. It includes the source code to implement the secant method, defines a fuel efficiency function, and uses scipy minimize_scalar to find the optimal pressure. Screenshots show the output of optimal pressure and maximum efficiency.
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

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 [Link] 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 [Link]:
optimal_pressure = result.x
max_fuel_efficiency = -[Link]
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.

Common questions

Powered by AI

Conditional logic in the provided Python implementations is handled through 'if' statements that validate whether conditions such as bracketing of roots and the success of optimization processes are met. This is significant in numerical methods and optimization as it ensures robustness by preventing invalid operations (like calculating with non-bracketing points) and provides informative error messages or outputs to guide the user in case of failure .

The Python implementation demonstrates the application of function definition and function calls by explicitly declaring functions such as 'secant_method' and 'fuel_efficiency'. These functions encapsulate specific computational processes—root finding and fuel efficiency calculation—enabling their reuse and modularity. Function calls are made to execute these processes with given inputs, demonstrating how structured programming is applied in numerical analysis .

The secant method's iterative process is fundamentally a root-finding technique that approximates the roots of a function using linear interpolation, relying heavily on initial guesses and their updates per iteration. This contrasts with optimization strategies in Python libraries like 'minimize_scalar', which are designed to find extremes (minima or maxima) of functions over specified intervals. These optimization algorithms, such as Brent's method, usually adopt more sophisticated strategies that combine root-finding and optimization principles, allowing them to efficiently narrow down the interval of interest and converge to optimum values. Therefore, Python's optimization methods are generally more robust and versatile for optimization tasks beyond simple root-finding .

The 'minimize_scalar' method improves efficiency by leveraging a bounded optimization approach that systematically explores possible values within a specified interval to find the min or max of a scalar function. Unlike the secant method, which requires manual iteration and checking for root bracketing, 'minimize_scalar' automates the process and efficiently identifies the optimal tire pressure by minimizing (or maximizing) the desired function over the given range .

The secant method might fail to find a root if the initial points do not bracket a root or if the maximum number of iterations is reached without convergence. To mitigate these failures, the implementation requires an initial condition check where the function values at the two initial guesses should not have the same sign, indicating they bracket a root. Additionally, setting a high enough maximum iteration count can allow sufficient attempts for finding the root, while informative error messages guide adjustments or reassessments of the method's use .

The course integrates conditional logic into numerical methods and optimization by requiring students to implement error handling and decision-making processes in their code. This involves understanding when and how to use 'if' statements to manage different outcomes in a computational process, such as checking for root bracketing or ensuring convergence in iterations. The pedagogical value lies in teaching students to build robust, error-tolerant programs that can adapt to different user inputs and algorithmic conditions, thereby enhancing their technical and analytical skills in computational problem-solving .

The main challenges in implementing the secant method include ensuring the initial points bracket the root and handling cases where maximum iterations are reached without finding a root. The presented implementation addresses these challenges by checking if the product of the function values at the initial points is non-negative, which indicates they do not bracket a root. It also includes a maximum iteration count to prevent infinite loops, returning None if the root is not found within the specified iterations .

The learning outcomes from implementing numerical methods and optimization tasks using Python include gaining practical skills in using methods like the secant method for root-finding, understanding optimization libraries for efficiency, mastering function definition and function calls, and handling conditional logic and error messages. These outcomes provide students with a strong foundation in both theoretical and applied aspects of numerical methods and optimization, enhancing problem-solving abilities in computational contexts .

Optimization libraries play a crucial role in computational efficiency by providing pre-built, tested algorithms that simplify complex optimization tasks. In the course, the example of using Python's 'minimize_scalar' method showcases how these libraries streamline the process of finding optimal solutions—like the tire pressure for maximum fuel efficiency—by handling much of the computational overhead internally. This allows students to focus on applying optimization concepts without delving into algorithmic complexities .

The advantages of using Python for implementing numerical methods and optimization include its readability, large standard library, and powerful third-party modules that facilitate complex computations and visualizations. Python's syntax is conducive to learning, making it accessible for educational settings. However, potential limitations include performance inefficiencies compared to compiled languages like C++, which may impact the execution speed for very large-scale computations, and the requirement for students to learn Python-specific coding practices or paradigms that may not directly translate to other programming environments .

You might also like