Numerical Analysis Formatted Guide
Numerical Analysis Formatted Guide
Question:
Use the Bisection Method to find a root of f(x) = x^3 - 5x + 1.
Answer:
The Bisection Method is a root-finding algorithm that repeatedly divides an interval in half and selects
Solution:
Using numanal-2.py:
```python
def bisection(f, a, b, tol=1e-6):
while abs(b - a) > tol:
mid = (a + b) / 2
if f(mid) == 0:
return mid
elif f(a) * f(mid) < 0:
b = mid
else:
a = mid
return (a + b) / 2
```
Question:
Apply Newton-Raphson method to approximate a root of f(x) = sin(x) - x/2.
Answer:
Newton-Raphson is an iterative method for finding successively better approximations to the roots of a
real-valued function.
Solution:
Using numanal-2.py:
```python
def newton_raphson(f, df, x0, tol=1e-6):
while True:
x1 = x0 - f(x0) / df(x0)
if abs(x1 - x0) < tol:
return x1
x0 = x1
```
Question:
Implement the Trapezoidal Rule to estimate the integral of e^(-x^2) from 0 to 1.
Answer:
The Trapezoidal Rule approximates the integral by summing up the areas of trapezoids under the
curve.
Solution:
Using numanal-2.py:
```python
def trapezoid(f, a, b, n=256):
h = (b - a) / n
I = (f(a) + f(b)) / 2 + sum(f(a + i * h) for i in range(1, n))
return I * h
```
Question:
Solve dy/dx = x^2 - y with Eulers method.
Answer:
Euler's method is a simple numerical approach for solving first-order differential equations.
Solution:
Using numanal-2.py:
```python
def solve_euler(f, a, b, y_a, steps=256):
x, y = a, y_a
h = (b - a) / steps
for _ in range(steps):
y += f(x, y) * h
x += h
return y
```