Project Maths Final
Project Maths Final
University (NINU)
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
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.
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.
# 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.
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
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