๐Ÿ“
B.TECH โ€ข R25 โ€ข COMPUTATIONAL MATHEMATICS

Computational Mathematics - Lab Manual

Computational Mathematics Laboratory โ€ข Using Python / MATLAB Software

๐Ÿ“˜ R25 Regulation ๐Ÿ“ Computational Mathematics ๐Ÿ Python / MATLAB ๐Ÿงช Lab Experiment
โ† All Experiments

Aim:

☛  
UNIT - II: Solution of Algebraic and Transcendental Equations:
Bisection method, Newton Raphson Method
Programs:
      โ€ข Root of a given equation using Bisection method.
      โ€ข Root of a given equation Newton Raphson Method.

Solution :

โ€ข Root of a given equation using bisection method.

PROGRAM: (Bisection_Method.py)

 
# Function for which root is found
def f(x):
    return x*x - 4   # change this equation as needed

# Bisection method
def bisection(a, b, e):
    if f(a) * f(b) > 0:
        print("Invalid interval. Root not guaranteed.")
        return

    while (b - a) >= e:
        c = (a + b) / 2

        if f(c) == 0.0:
            break
        elif f(a) * f(c) < 0:
            b = c
        else:
            a = c

    print("Root =", c)

# Input from user
a = float(input("Enter a: "))
b = float(input("Enter b: "))
e = float(input("Enter tolerance: "))

bisection(a, b, e)


OUTPUT:

 
Enter a: 0
Enter b: 3
Enter tolerance: 0.001
Root = 2.000244140625



โ€ข Root of a given equation using Newton-Raphson method.

PROGRAM: (Newton_Raphson.py)

 
# Function f(x)
def f(x):
    return x*x - 4   # change this equation if needed

# Derivative f'(x)
def df(x):
    return 2*x

# Newton-Raphson method
def newton_raphson(x0, e):
    x1 = x0

    while True:
        x0 = x1
        x1 = x0 - f(x0)/df(x0)

        if abs(x1 - x0) < e:
            break

    print("Root =", x1)

# Input
x0 = float(input("Enter initial guess: "))
e = float(input("Enter tolerance: "))

newton_raphson(x0, e)


OUTPUT:

 
Enter initial guess: 3
Enter tolerance: 0.001
Root = 2.000000000026214


๐Ÿ’ป Run This Python Program

triangle using for loop
The selected Python program is loaded automatically into the editor. Edit the program, provide input if required and click Run.

The compiler is embedded using OneCompiler. Your Python program runs inside the embedded Python environment; Study Glance does not execute Python code directly on your web server. MATLAB programs shown in the experiment should be run in MATLAB (or GNU Octave), and programs that use libraries such as NumPy, SciPy or SymPy depend on what the embedded environment provides.
โ† Back to Computational Mathematics Lab ๐Ÿ“š All Lab Manuals