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

Project Maths Final

The document outlines a project on the Bisection Method for finding roots of the function f(x) = x^2 - x - 2. It details the steps involved, including defining the function, checking the interval for opposite signs, iterating to find the midpoint, and returning the approximate root. The final output indicates that the method successfully approximated the root as x = 2.

Uploaded by

amaryasser4499
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Project Maths Final

The document outlines a project on the Bisection Method for finding roots of the function f(x) = x^2 - x - 2. It details the steps involved, including defining the function, checking the interval for opposite signs, iterating to find the midpoint, and returning the approximate root. The final output indicates that the method successfully approximated the root as x = 2.

Uploaded by

amaryasser4499
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

New Ismailia National

University (NINU)

Differential equations & numerical analysis

Dr. Amr H. Abdalla

Projec
t
Bisection
method
Maded
by
Ammar Yasser Mohamed
Hassan Mohamed Hassan
Ali Mohamed Ali Elrawy
Ahmed Mostafa Hassan
Ziad Ahmed Ezzat
Ahmed Mohamed Abd El maneam
Helmy Nasser Helmy
Mazen Mohamed Badran
Omar Mohamed Abd Elhakam
Mohamed Hamed Mohamed
Abanob Nageh Kamal
1. Defining the Function f(x)
In the first part of the code, we define the
mathematical function that we want to find the
root for

# Define the function f(x)


def f(x):
return x**2 - x - 2

Explanation:
This function represents the equation f(x) = x^2 - x - 2, which we want to find the root for.
We will use this function in the bisection method to evaluate values at different points a, b, and
c.
2. Checking the Interval (Initial Check)
Before starting the iterations, we need to check if the function
values at aaa and bbb have opposite signs. This ensures that a root
exists between these two points.

# Bisection method function


def bisection_method(a, b, tol=1e-6, max_iter=100):

# Check if f(a) and f(b) have different signs


if f(a) * f(b) > 0:
print(f"Cannot apply the Bisection Method because f(a) and f(b) have the same sign.")
return None

Explanation:
This step checks if the product of f(a)f(a)f(a) and f(b)f(b)f(b) is greater than 0. If so, the
function values at aaa and bbb have the same sign, meaning there is no guarantee that a
root exists between these two points.
If the condition is met, a message is displayed, and the method will not proceed.
3. Main Loop (Iterative Process)
The main part of the bisection method involves iteratively finding the midpoint between
the points aaa and bbb, and narrowing the interval where the root exists.

# Initialize iteration counter


iter_count = 0

# Loop until the interval is small enough or maximum iterations are reached
while (b - a) / 2 > tol and iter_count < max_iter:
c = (a + b) / 2 # Calculate the midpoint
if f(c) == 0:
return c # Exact root found
elif f(a) * f(c) < 0:
b = c # Root is in the left half
else:
a = c # Root is in the right half

iter_count += 1

Explanation:
We calculate the midpoint c of the interval [a, b].
If f(c)=0, we've found the exact root.
If f(a)×f(c)<0 , the root lies in the left half of the interval, so we update b to c.
Otherwise, the root lies in the right half of the interval, so we update a to c.
This loop continues until the difference between a and b is less than the specified tolerance, or until the maximum
number of iterations is reached.
4. Returning the Approximate Root
Once the loop terminates, we return the midpoint ccc as the
approximate root if the interval is sufficiently small.

return (a + b) / 2 # Return the approximated root

Explanation:
After the loop ends, the approximate root is calculated by
taking the midpoint of the final interval [a,b][a, b][a,b].
This value is returned as the result.
5. Setting the Interval and Running the Program
In this section, we define the initial interval [a,b], the tolerance, and the maximum number
of iterations, and then execute the bisection method.

# Interval settings
a = 1 # Lower bound of the interval
b = 3 # Upper bound of the interval
tolerance = 1e-6 # Tolerance for the solution
max_iterations = 100 # Maximum number of iterations

# Execute the method


root = bisection_method(a, b, tolerance, max_iterations)

if root is not None:


print(f"Found the approximated root: {root}")

Explanation:
We set the initial interval [a,b] to [1,3], which is the range where we expect the root to
lie.
The tolerance10^{-6} specifies how precise we want the solution to be.
We also set the maximum number of iterations to 100 to prevent the program from
running indefinitely.
After running the bisection method, if the root is found, it is printed.
Out Put

Found the approximated root: 2.0

This output shows that the bisection method has


successfully approximated the root of the equation
x^2 - x - 2 = 0, which is approximately x=2
Thank
you

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy