Monday, 8 July 2024

Numerical Methods in Python

 

5. Runge-Kutta Method (RK4):
A fourth-order numerical method for solving ordinary differential equations (ODEs), more accurate than Euler's method for many types of problems.

def runge_kutta_4(func, initial_x, initial_y, step_size, num_steps):
    x = initial_x
    y = initial_y
    for _ in range(num_steps):
        k1 = step_size * func(x, y)
        k2 = step_size * func(x + step_size / 2, y + k1 / 2)
        k3 = step_size * func(x + step_size / 2, y + k2 / 2)
        k4 = step_size * func(x + step_size, y + k3)
        y += (k1 + 2*k2 + 2*k3 + k4) / 6
        x += step_size
    return x, y

# Example usage:
def dy_dx(x, y):
    return x + y

x_final, y_final = runge_kutta_4(dy_dx, initial_x=0, 
                                 initial_y=1, step_size=0.1, num_steps=100)
print(f"At x = {x_final}, y = {y_final}")

#clcoding.com
At x = 9.99999999999998, y = 44041.593801752446

4. Bisection Method:
A root-finding algorithm that repeatedly bisects an interval and then selects a subinterval in which a root must lie for further processing.

def bisection_method(func, a, b, tolerance=1e-10, max_iterations=100):
    if func(a) * func(b) >= 0:
        raise ValueError("Function does not change sign over interval")
    
    for _ in range(max_iterations):
        c = (a + b) / 2
        if abs(func(c)) < tolerance:
            return c
        if func(c) * func(a) < 0:
            b = c
        else:
            a = c
    raise ValueError("Failed to converge")

# Example usage:
def h(x):
    return x**3 - 2*x - 5

root = bisection_method(h, a=2, b=3)
print(f"Root found at x = {root}")

#clcoding.com
Root found at x = 2.0945514815393835
3. Secant Method:
A root-finding algorithm that uses a succession of roots of secant lines to better approximate a root of a function.

def secant_method(func, x0, x1, tolerance=1e-10, max_iterations=100):
    for _ in range(max_iterations):
        fx1 = func(x1)
        if abs(fx1) < tolerance:
            return x1
        fx0 = func(x0)
        denominator = (fx1 - fx0) / (x1 - x0)
        x_next = x1 - fx1 / denominator
        x0, x1 = x1, x_next
    raise ValueError("Failed to converge")

# Example usage:
def g(x):
    return x**3 - 2*x - 5

root = secant_method(g, x0=2, x1=3)
print(f"Root found at x = {root}")

#clcoding.com
Root found at x = 2.094551481542327
2. Euler's Method:
A first-order numerical procedure for solving ordinary differential equations (ODEs).

def euler_method(func, initial_x, initial_y, step_size, num_steps):
    x = initial_x
    y = initial_y
    for _ in range(num_steps):
        y += step_size * func(x, y)
        x += step_size
    return x, y

# Example usage:
def dy_dx(x, y):
    return x + y

x_final, y_final = euler_method(dy_dx, initial_x=0, 
                                initial_y=1, step_size=0.1, num_steps=100)
print(f"At x = {x_final}, y = {y_final}")

#clcoding.com
At x = 9.99999999999998, y = 27550.224679644543
1. Newton-Raphson Method:
Used for finding successively better approximations to the roots (or zeroes) of a real-valued function.

import numdifftools as nd

def newton_raphson(func, initial_guess, tolerance=1e-10, max_iterations=100):
    x0 = initial_guess
    for _ in range(max_iterations):
        fx0 = func(x0)
        if abs(fx0) < tolerance:
            return x0
        fprime_x0 = nd.Derivative(func)(x0)
        x0 = x0 - fx0 / fprime_x0
    raise ValueError("Failed to converge")

# Example usage:
import math

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

root = newton_raphson(f, initial_guess=3)
print(f"Root found at x = {root}")

#clcoding.com
Root found at x = 2.0945514815423474

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (13) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (161) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (127) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (53) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (917) Python Coding Challenge (304) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses