A simplified guide on how to prep up on Mathematics for Artificial Intelligence, Machine Learning and Data Science: Numerical Methods (Important Pointers only)
Module VII : Numerical Methods
I. Bisection Method.
A numerical technique for solving equations of the form . It is a type of root-finding method that repeatedly narrows down an interval where a root of the function exists.
Steps:
Choose the initial interval : Select two points and such that and have opposite signs. This indicates that there is at least one root in the interval .
Compute the midpoint : Calculate the midpoint of the interval:
Evaluate the function at the midpoint: Compute .
Determine the subinterval:
- If , the root lies in the interval . Set .
- If , the root lies in the interval . Set .
- If , then is the root, and the method stops.
Check for convergence: Repeat steps 2-4 until the interval is sufficiently small (i.e., for some tolerance ), or until the value of is close enough to zero.
Output the result: The midpoint is an approximation of the root.
Eg: To find a root of the function .
Choose the initial interval:
- Since and have opposite signs, there is a root in the interval .
Compute the midpoint:
Evaluate the function at the midpoint:
Since , the root is exactly at .
For functions where the root is not exactly at the midpoint, the method would continue iterating until the desired precision is achieved.
II. Trapezoidal Rule.
A numerical method used to approximate the definite integral of a function.
The trapezoidal rule approximates the integral of a function over an interval using the formula:
For better accuracy, the interval can be divided into smaller subintervals of equal width. If the interval is divided into subintervals, each of width , the composite trapezoidal rule is used:
where , , and for .
Steps
Divide the interval : Divide the interval into equal subintervals. The width of each subinterval is .
Calculate the endpoints: Compute the function values at the endpoints of each subinterval. These points are , where .
Apply the trapezoidal rule formula: Sum the function values, multiplying the endpoints by and the interior points by .
Calculate the approximation: Multiply the result by to get the final approximation of the integral.
Eg: Approximate the integral of over the interval using the trapezoidal rule with subintervals.
Divide the interval :
Calculate the endpoints:
Evaluate the function at the points:
Apply the trapezoidal rule formula:
Calculate the sum:
So, the approximate value of the integral using the trapezoidal rule with 4 subintervals is approximately .
The exact value of the integral is . The trapezoidal rule gives a reasonably close approximation, which can be improved by increasing the number of subintervals .
III. Secant Method.
The secant method is an iterative numerical technique used to find roots of a function . Unlike the bisection method, which requires the function values to have opposite signs at the endpoints of an interval, the secant method uses two initial approximations to generate a sequence of improving approximations to the root.
The secant method approximates the root by using the following iterative formula:
where:- and are the current and previous approximations, respectively.
- and are the function values at these approximations.
Steps
Choose initial approximations: Select two initial guesses and close to the root.
Iterate using the secant formula: Use the secant formula to compute the next approximation
Check for convergence: Repeat the iteration until the difference between successive approximations is smaller than a predetermined tolerance or until the function value is close to zero.
Output the result: The final approximation is taken as the root.
Eg: Find a root of the function using the secant method.
Choose initial approximations: and .
First iteration:
Second iteration:
Further iterations: Repeat the above steps until the difference between successive approximations is less than the tolerance .
IV. Newton-Raphson Method.
An iterative numerical technique used to find approximations to the roots of a real-valued function . It is known for its fast convergence properties, especially when the initial guess is close to the actual root.
The Newton-Raphson iteration formula is given by:
where:
- is the current approximation.
- is the value of the function at .
- is the value of the derivative of the function at .
Steps
Choose an initial approximation: Select an initial guess close to the root.
Iterate using the Newton-Raphson formula: Use the formula to compute the next approximation .
Check for convergence: Repeat the iteration until the difference between successive approximations is smaller than a predetermined tolerance or until the function value is close to zero.
Output the result: The final approximation is taken as the root.
Eg: Find a root of the function using the Newton-Raphson method.
Choose an initial approximation: .
First iteration:
Second iteration:
Third iteration:
Further iterations: Continue iterating until the difference between successive approximations is less than the tolerance .
V. Numerical Stability and Error Analysis.
1. Numerical Stability
Numerical stability refers to how errors are propagated by an algorithm. An algorithm is numerically stable if small changes in the input or intermediate calculations lead to small changes in the output. Conversely, if small changes in the input result in large changes in the output, the algorithm is numerically unstable.
Types of Stability:
Forward Stability: An algorithm is forward stable if the computed solution is close to the exact solution of the given problem. This implies that the errors in the output are proportional to the errors in the input.
Backward Stability: An algorithm is backward stable if the computed solution is the exact solution to a slightly perturbed version of the original problem. This means the algorithm produces results that are accurate for some nearby problem.
Mixed Stability: Combines aspects of both forward and backward stability, considering both input and output errors.
2. Error Analysis
Error analysis is the study of the types, sources and propagation of errors in numerical computations. It helps in understanding the accuracy and precision of numerical solutions.
Types of Errors:
Round-off Error: Errors that occur because of the finite precision with which computers represent real numbers. For example, floating-point arithmetic can introduce small errors in calculations due to truncation or rounding.
Truncation Error: Errors that result from approximating a mathematical procedure. For example, truncating an infinite series to a finite number of terms introduces a truncation error.
Approximation Error: Errors that arise when a mathematical model or method approximates a physical process or another mathematical model. This includes discretization errors in methods like finite differences or finite elements.
3. Error Propagation:
Error propagation studies how errors in input data or intermediate steps affect the final result. It is crucial for understanding and mitigating the impact of errors in numerical algorithms.
4. Condition Number:
The condition number of a problem measures its sensitivity to changes in input. A problem with a high condition number is ill-conditioned, meaning small changes in input can cause large changes in output. Conversely, a problem with a low condition number is well-conditioned.
For example, the condition number of a matrix in solving the linear system is given by . If is large, the system is ill-conditioned.
5. Mitigating Errors
- Improving Precision: Use higher precision arithmetic (e.g., double precision instead of single precision).
- Algorithm Choice: Choose stable algorithms (e.g., using LU decomposition with pivoting instead of Gaussian elimination without pivoting).
- Conditioning: Precondition the problem (e.g., scaling the input data to improve the condition number).
- Error Estimation: Use a posteriori error estimates to assess the accuracy of the computed solution.
Eg: Error Propagation in Polynomial Evaluation
Evaluating a polynomial using Horner's method:
Horner's method is more stable than the naive approach because it minimizes the number of operations and thus the potential round-off errors.
VI. Euler's Method for ODEs.
Euler's method is a simple and widely used numerical technique for solving ordinary differential equations (ODEs).
Consider an initial value problem of the form:
where is a given function, is the initial condition, and we seek the value of at
Euler's method uses the following iterative formula to approximate the solution:
where:
- is the approximation of at .
- is the next value of .
- is the step size.
Steps of Euler's Method
- Initialization: Set the initial values and , and choose the step size .
- Iteration: Use the Euler formula to compute from for until the desired interval is covered.
- Output: The values give the approximate solution to the ODE at discrete points.
Eg : Use Euler's method to solve the initial value problem:
on the interval with a step size of .
Initialization:
Iteration:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Output:
The approximate solution at is .
Error and Stability:
Euler's method is simple and easy to implement, but it has limitations:
- Global Truncation Error: The error accumulates over steps and is proportional to . The global error is , making it less accurate for large .
- Stability: For stiff ODEs, Euler's method can be unstable unless the step size is very small.
VII. Runge-Kutta Methods.
The most commonly used Runge-Kutta method is the fourth-order method, often referred to simply as the Runge-Kutta method.
The general form of an -stage Runge-Kutta method for solving the initial value problem
is given by:
where:
for . The coefficients , , and define a specific Runge-Kutta method and are typically arranged in a Butcher tableau.
Fourth-Order Runge-Kutta Method (RK4)
The fourth-order Runge-Kutta method is the most popular Runge-Kutta method due to its accuracy and simplicity. It is given by the following formulas:
Compute the intermediate slopes:
Update the solution:
Eg : Use the fourth-order Runge-Kutta method to solve the initial value problem:
on the interval with a step size of .
Initialization:
First iteration:
Compute the intermediate slopes:
Update the solution:
Continue this process for more iterations to find the solution at desired points.
VIII. Simpson's Rule.
Simpson's rule is a numerical method for approximating the definite integral of a function. Simpson's rule uses parabolic arcs instead of straight lines to approximate the area under a curve.
For a function defined on the interval , Simpson's rule approximates the integral as follows:
Simpson's 1/3 Rule:
This rule requires that the interval is divided into an even number of subintervals, each of equal width .
Composite Simpson's 1/3 Rule: For better accuracy, especially over larger intervals, the interval is divided into equal subintervals (where is even), each of width . The composite Simpson's rule is then:
where for .
Eg: Approximate the integral using Simpson's rule with subintervals.
Define the function:
Set the interval and subinterval width:
Compute the function values at the required points:
Apply the composite Simpson's rule formula:
The exact value of the integral is , so the approximation using Simpson's rule is quite accurate.