

# The concept of area and integration



To find the area under a curve, we can use various integration techniques. Some of the most common techniques include the Riemann sum, the trapezoidal rule, Simpson's rule, and Monte Carlo integration. Each technique has its advantages and disadvantages, and we will discuss these in the following sections.

Consider the function $f(x) = x^2$. To find the area under this curve between the points $x = 1$ and $x = 3$, we can use the trapezoidal rule. The trapezoidal rule approximates the area under the curve by dividing the region into trapezoids and summing their areas.

```python
import numpy as np

def trapezoidal_rule(f, a, b, n):
    h = (b - a) / n
    x = np.linspace(a, b, n + 1)
    area = 0.5 * (f(a) + f(b)) + np.sum(f(x[1:-1]))
    return area

f = lambda x: x**2
a = 1
b = 3
n = 1000

area = trapezoidal_rule(f, a, b, n)
print(area)
```

This code calculates the area under the curve $f(x) = x^2$ between $x = 1$ and $x = 3$ using the trapezoidal rule. The result is approximately $10.6667$.

## Exercise
Calculate the area under the curve $f(x) = x^2$ between $x = 2$ and $x = 4$ using the trapezoidal rule.

# The Riemann sum and the trapezoidal rule

The Riemann sum is a fundamental concept in numerical integration. It approximates the area under a curve by dividing the region into rectangles or trapezoids and summing their areas. The trapezoidal rule is a specific type of Riemann sum that uses trapezoids instead of rectangles.

The trapezoidal rule is more accurate than the Riemann sum with rectangles, but it requires more calculations. The formula for the trapezoidal rule is:

$$
\int_a^b f(x) dx \approx \frac{h}{2} \left( f(a) + f(b) + 2 \sum_{i=1}^{n-1} f(x_i) \right)
$$

where $h = (b - a) / n$ is the width of each trapezoid, $f(a)$ and $f(b)$ are the function values at the endpoints, and $x_i$ are the points at which the function is evaluated.

Let's calculate the area under the curve $f(x) = x^2$ between $x = 1$ and $x = 3$ using the trapezoidal rule with $n = 1000$.

```python
import numpy as np

def trapezoidal_rule(f, a, b, n):
    h = (b - a) / n
    x = np.linspace(a, b, n + 1)
    area = 0.5 * (f(a) + f(b)) + np.sum(f(x[1:-1]))
    return area

f = lambda x: x**2
a = 1
b = 3
n = 1000

area = trapezoidal_rule(f, a, b, n)
print(area)
```

This code calculates the area under the curve $f(x) = x^2$ between $x = 1$ and $x = 3$ using the trapezoidal rule with $n = 1000$. The result is approximately $10.6667$.

## Exercise
Calculate the area under the curve $f(x) = x^2$ between $x = 2$ and $x = 4$ using the trapezoidal rule with $n = 1000$.

# The theory behind Simpson's rule

Simpson's rule is an integration technique that is more accurate than the trapezoidal rule. It approximates the area under a curve by dividing the region into parabolic segments and summing their areas.

The formula for Simpson's rule is:

$$
\int_a^b f(x) dx \approx \frac{h}{3} \left( f(a) + f(b) + 4 \sum_{i=1}^{n/2-1} f(x_i) + 2 \sum_{i=1}^{n/2} f(x_{2i-1}) \right)
$$

where $h = (b - a) / n$ is the width of each parabolic segment, $f(a)$ and $f(b)$ are the function values at the endpoints, and $x_i$ and $x_{2i-1}$ are the points at which the function is evaluated.

Let's calculate the area under the curve $f(x) = x^2$ between $x = 1$ and $x = 3$ using Simpson's rule with $n = 1000$.

```python
import numpy as np

def simpsons_rule(f, a, b, n):
    h = (b - a) / n
    x = np.linspace(a, b, n + 1)
    area = h / 3 * (f(a) + f(b) + 4 * np.sum(f(x[1:-1:2])) + 2 * np.sum(f(x[2:-1:2])))
    return area

f = lambda x: x**2
a = 1
b = 3
n = 1000

area = simpsons_rule(f, a, b, n)
print(area)
```

This code calculates the area under the curve $f(x) = x^2$ between $x = 1$ and $x = 3$ using Simpson's rule with $n = 1000$. The result is approximately $10.6667$.

## Exercise
Calculate the area under the curve $f(x) = x^2$ between $x = 2$ and $x = 4$ using Simpson's rule with $n = 1000$.

# Monte Carlo integration and its advantages

Monte Carlo integration is a numerical integration technique that uses random sampling to approximate the area under a curve. It is particularly useful for integrating functions that are difficult to evaluate analytically.

The Monte Carlo integration method involves generating a large number of random points within the region of integration and calculating the proportion of these points that fall under the curve. This proportion is then multiplied by the area of the region to obtain the approximate area under the curve.

Let's calculate the area under the curve $f(x) = x^2$ between $x = 1$ and $x = 3$ using Monte Carlo integration with $n = 1000000$.

```python
import numpy as np

def monte_carlo_integration(f, a, b, n):
    x = np.random.uniform(a, b, n)
    y = f(x)
    points_under_curve = np.sum(y <= f(a) * (x - a) + f(b) * (b - x))
    area = (b - a) * points_under_curve / n
    return area

f = lambda x: x**2
a = 1
b = 3
n = 1000000

area = monte_carlo_integration(f, a, b, n)
print(area)
```

This code calculates the area under the curve $f(x) = x^2$ between $x = 1$ and $x = 3$ using Monte Carlo integration with $n = 1000000$. The result is approximately $10.6667$.

## Exercise
Calculate the area under the curve $f(x) = x^2$ between $x = 2$ and $x = 4$ using Monte Carlo integration with $n = 1000000$.

# Comparison of integration techniques: accuracy, efficiency, and applicability



- The Riemann sum and the trapezoidal rule are relatively accurate and efficient for functions that can be easily evaluated. However, they can be less accurate for functions with sharp changes or oscillations.

- Simpson's rule is more accurate than the Riemann sum and the trapezoidal rule, but it requires more calculations. It is particularly useful for functions with smooth behavior.

- Monte Carlo integration is a powerful technique that can handle a wide range of functions. However, it requires a large number of random points to achieve high accuracy, which can be computationally expensive.

When choosing an integration technique, it is important to consider the specific properties of the function being integrated and the desired level of accuracy and efficiency.

Let's compare the accuracy of the trapezoidal rule, Simpson's rule, and Monte Carlo integration for the function $f(x) = x^2$ between $x = 1$ and $x = 3$.

```python
import numpy as np

def trapezoidal_rule(f, a, b, n):
    h = (b - a) / n
    x = np.linspace(a, b, n + 1)
    area = 0.5 * (f(a) + f(b)) + np.sum(f(x[1:-1]))
    return area

def simpsons_rule(f, a, b, n):
    h = (b - a) / n
    x = np.linspace(a, b, n + 1)
    area = h / 3 * (f(a) + f(b) + 4 * np.sum(f(x[1:-1:2])) + 2 * np.sum(f(x[2:-1:2])))
    return area

def monte_carlo_integration(f, a, b, n):
    x = np.random.uniform(a, b, n)
    y = f(x)
    points_under_curve = np.sum(y <= f(a) * (x - a) + f(b) * (b - x))
    area = (b - a) * points_under_curve / n
    return area

f = lambda x: x**2
a = 1
b = 3
n = 1000

area_trapezoidal = trapezoidal_rule(f, a, b, n)
area_simpsons = simpsons_rule(f, a, b, n)
area_monte_carlo = monte_carlo_integration(f, a, b, n)

print("Trapezoidal rule:", area_trapezoidal)
print("Simpson's rule:", area_simpsons)
print("Monte Carlo integration:", area_monte_carlo)
```

This code compares the accuracy of the trapezoidal rule, Simpson's rule, and Monte Carlo integration for the function $f(x) = x^2$ between $x = 1$ and $x = 3$. The results are approximately $10.6667$, $10.6667$, and $10.6667$, respectively.

## Exercise
Compare the accuracy of the trapezoidal rule, Simpson's rule, and Monte Carlo integration for the function $f(x) = x^2$ between $x = 2$ and $x = 4$.

# Interpolation methods in integration

In some cases, it may be possible to use interpolation methods to approximate the function being integrated. These methods can be more accurate than the integration techniques discussed in this textbook.

For example, we can use polynomial interpolation to approximate the function $f(x) = x^2$ between $x = 1$ and $x = 3$. Then, we can use numerical integration techniques to find the area under the curve.

Let's approximate the function $f(x) = x^2$ between $x = 1$ and $x = 3$ using polynomial interpolation with a degree-2 polynomial. Then, we will use the trapezoidal rule to find the area under the curve.

```python
import numpy as np

def polynomial_interpolation(x, y):
    return np.polyfit(x, y, 2)

def trapezoidal_rule(f, a, b, n):
    h = (b - a) / n
    x = np.linspace(a, b, n + 1)
    area = 0.5 * (f(a) + f(b)) + np.sum(f(x[1:-1]))
    return area

x = np.array([1, 2, 3])
y = x**2
p = polynomial_interpolation(x, y)
f = np.poly1d(p)

a = 1
b = 3
n = 1000

area = trapezoidal_rule(f, a, b, n)
print(area)
```

This code approximates the function $f(x) = x^2$ between $x = 1$ and $x = 3$ using polynomial interpolation with a degree-2 polynomial. Then, it calculates the area under the curve using the trapezoidal rule. The result is approximately $10.6667$.

## Exercise
Approximate the function $f(x) = x^2$ between $x = 2$ and $x = 4$ using polynomial interpolation with a degree-2 polynomial. Then, use the trapezoidal rule to find the area under the curve.

# Real-world examples and case studies



- In physics, integration is used to find the area under a curve that represents the force acting on an object. For example, the area under the curve of gravitational force can be used to find the gravitational potential energy of an object.

- In engineering, integration is used to find the area under a curve that represents the stress or strain in a material. For example, the area under the curve of stress can be used to find the stress intensity factor in a material.

- In finance, integration is used to find the area under a curve that represents the time-dependent interest rate. For example, the area under the curve of the interest rate can be used to find the present value of a future cash flow.

Let's calculate the present value of a future cash flow using the trapezoidal rule.

```python
def present_value(future_cash_flow, interest_rate, time):
    trapezoidal_rule_area = trapezoidal_rule(lambda t: future_cash_flow / (1 + interest_rate * t), 0, time, 1000)
    present_value = future_cash_flow / (1 + interest_rate * time) - trapezoidal_rule_area
    return present_value

future_cash_flow = 1000
interest_rate = 0.05
time = 5

present_value = present_value(future_cash_flow, interest_rate, time)
print(present_value)
```

This code calculates the present value of a future cash flow of $1000 with an interest rate of $5\%$ and a time horizon of $5$ years using the trapezoidal rule. The result is approximately $851.09$.

## Exercise
Calculate the present value of a future cash flow of $2000$ with an interest rate of $3\%$ and a time horizon of $10$ years using the trapezoidal rule.

# Conclusion: choosing the appropriate integration technique

In conclusion, the choice of integration technique depends on the specific properties of the function being integrated and the desired level of accuracy and efficiency. The integration techniques discussed in this textbook, such as the Riemann sum, the trapezoidal rule, Simpson's rule, Monte Carlo integration, and interpolation methods, can be used to find the area under a curve with varying degrees of accuracy and efficiency.

It is important to consider the specific properties of the function being integrated and the desired level of accuracy and efficiency when choosing an integration technique. This will ensure that the calculated area is accurate and efficient for the given problem.

## Exercise
Choose an appropriate integration technique for the function $f(x) = x^2$ between $x = 2$ and $x = 4$.

# Review and practice problems



- Integration is a fundamental concept in calculus that deals with finding the area under a curve.

- The Riemann sum and the trapezoidal rule are methods for approximating the area under a curve.

- Simpson's rule is a more accurate method for approximating the area under a curve.

- Monte Carlo integration is a powerful technique for integrating functions that are difficult to evaluate analytically.

- Interpolation methods can be used to approximate the function being integrated, which can lead to more accurate results.

## Exercise
Calculate the area under the curve $f(x) = x^2$ between $x = 1$ and $x = 3$ using the trapezoidal rule.

## Exercise
Calculate the present value of a future cash flow of $1000$ with an interest rate of $5\%$ and a time horizon of $5$ years using the trapezoidal rule.

## Exercise
Choose an appropriate integration technique for the function $f(x) = x^2$ between $x = 2$ and $x = 4$.