Hot Posts

3/recent/ticker-posts

Trapzoidal Rule Solved by Matlab

 

Understanding the Trapezoidal Rule in MATLAB: A Beginner's Guide

The Trapezoidal Rule is a numerical method for approximating the definite integral of a function. It’s particularly useful when an analytical integration is difficult or impossible to perform. In this blog, we will explore how to use MATLAB to apply the Trapezoidal Rule to solve an integral in a simple and easy-to-understand manner.

What is the Trapezoidal Rule?

The Trapezoidal Rule approximates the area under a curve by dividing it into trapezoids rather than rectangles (as in the Riemann sum). The formula for the Trapezoidal Rule is:

 

[ int_{a}^{b} f(x) , dx approx frac{h}{2} left[ f(a) + 2 sum_{i=1}^{n-1} f(x_i) + f(b) right] ]

Where:
- ( a ) and ( b ) are the limits of integration.
- ( n ) is the number of subintervals.
- ( h ) is the width of each subinterval (left( h = frac{b - a}{n} right)).
- ( x_i ) are the points dividing the interval ([a, b]).

Implementing the Trapezoidal Rule in MATLAB

Let's walk through the process of solving an integral using the Trapezoidal Rule in MATLAB with an example.
 

Example Problem

Suppose we want to approximate the integral of the function ( f(x) = x^2 ) from 0 to 1.
 

[ int_{0}^{1} x^2 , dx ]

Step-by-Step Solution in MATLAB

1. Define the Function

First, we define the function ( f(x) = x^2 ). In MATLAB, we can define this using an anonymous function.

```matlab
f = @(x) x.^2;
```

2. Set the Limits of Integration and Number of Subintervals

We need to set the limits of integration (a and b) and choose the number of subintervals (n).

```matlab
a = 0;
b = 1;
n = 100;  % The number of subintervals
```

3. Calculate the Width of Each Subinterval

We compute the width of each subinterval (h).

```matlab
h = (b - a) / n;
```

4. Create the x Values

We create the x values that divide the interval [a, b] into n subintervals.

```matlab
x = linspace(a, b, n+1);
```

5. Evaluate the Function at Each x Value

We evaluate the function at each of these x values.

```matlab
y = f(x);
```

6. Apply the Trapezoidal Rule Formula

We apply the formula for the Trapezoidal Rule. MATLAB makes this step straightforward with vectorized operations.

```matlab
integral_approximation = h * (sum(y) - (y(1) + y(end))/2);
```

7. Display the Result

Finally, we display the result.

```matlab
disp(integral_approximation);
```


Complete MATLAB Code

Putting it all together, here’s the complete MATLAB code for approximating the integral using the Trapezoidal Rule.

```matlab
% Define the function
f = @(x) x.^2;

% Set the limits of integration and number of subintervals
a = 0;
b = 1;
n = 100;

% Calculate the width of each subinterval
h = (b - a) / n;

% Create the x values
x = linspace(a, b, n+1);

% Evaluate the function at each x value

y = f(x);

% Apply the Trapezoidal Rule formula
integral_approximation = h * (sum(y) - (y(1) + y(end))/2);

% Display the result
disp(integral_approximation);
```

Explanation of the Code

- Defining the Function: The function ( f(x) = x^2 ) is defined using an anonymous function handle.
- Setting Parameters: The limits of integration (0 and 1) and the number of subintervals (100) are defined.
- Subinterval Width: The width of each subinterval (h) is calculated.
- Creating x Values: The interval [0, 1] is divided into 101 points (100 subintervals).
- Evaluating the Function: The function ( f(x) ) is evaluated at each of these points.
- Applying the Formula: The trapezoidal rule formula is applied to approximate the integral.
- Displaying the Result: The result is displayed using `disp`.

Conclusion

The Trapezoidal Rule is a simple yet powerful technique for numerical integration, and MATLAB provides an easy-to-use platform for implementing this method. By following the steps outlined in this blog, you can apply the Trapezoidal Rule to approximate integrals of various functions. With practice, you’ll be able to tackle more complex numerical integration problems with confidence. Happy coding!


Post a Comment

0 Comments