MATLAB Trapz
Palavras-chave:
Publicado em: 05/09/2025Numerical Integration with MATLAB Trapz
The `trapz` function in MATLAB provides a simple and efficient way to approximate the definite integral of a function using the trapezoidal rule. This article explores the functionality of `trapz`, its implementation details, and considerations for its use.
Fundamental Concepts / Prerequisites
To understand the `trapz` function, you should have a basic understanding of the following:
- Numerical Integration: Approximating the definite integral of a function using numerical methods.
- Trapezoidal Rule: A numerical integration technique that approximates the area under a curve by dividing it into trapezoids.
- MATLAB Basics: Familiarity with MATLAB syntax, vectors, and functions.
Implementation in MATLAB
The `trapz` function takes either a single vector of y-values or a vector of x-values and a vector of y-values as input. It returns the numerical approximation of the integral.
% Example 1: Integrate y = x^2 from x = 0 to x = 1 using evenly spaced x values
x = 0:0.1:1;
y = x.^2;
integral_approximation = trapz(x, y);
disp(['Approximation of integral of x^2 from 0 to 1: ', num2str(integral_approximation)]);
% Example 2: Integrate a function using only y values, assuming x values are 1, 2, 3,...
y = [1, 4, 9, 16, 25];
integral_approximation_no_x = trapz(y);
disp(['Approximation of integral using only y values: ', num2str(integral_approximation_no_x)]);
% Example 3: Integrate a sine wave
x = linspace(0, 2*pi, 100);
y = sin(x);
integral_sine = trapz(x, y);
disp(['Approximation of integral of sin(x) from 0 to 2*pi: ', num2str(integral_sine)]);
% Example 4: Illustrating error with coarse sampling
x_coarse = linspace(0, 2*pi, 10); % Fewer points
y_coarse = sin(x_coarse);
integral_sine_coarse = trapz(x_coarse, y_coarse);
disp(['Approximation of integral of sin(x) from 0 to 2*pi (coarse): ', num2str(integral_sine_coarse)]);
Code Explanation
Example 1:
We define a vector `x` representing the x-values from 0 to 1 with a step of 0.1. Then, we calculate the corresponding `y` values based on the function y = x^2. The `trapz(x, y)` function then uses the trapezoidal rule to approximate the integral of x^2 between 0 and 1.
Example 2:
Here, we only provide the `y` values. In this case, `trapz` assumes the `x` values are 1, 2, 3, and so on. This is useful when you only have y-data and want a quick approximation.
Example 3:
This example demonstrates integrating a sine wave. `linspace(0, 2*pi, 100)` creates a vector of 100 evenly spaced values between 0 and 2π. This ensures a reasonable approximation of the sine wave. The result should be close to 0, as the integral of sin(x) from 0 to 2π is theoretically 0.
Example 4:
This example demonstrates the importance of sample rate. If we use few sample points (coarse sampling), then the trapezoidal rule approximation is poor and far from the true integral.
Complexity Analysis
The time and space complexity of the `trapz` function in MATLAB are primarily determined by the size of the input vectors.
- Time Complexity: O(n), where n is the number of elements in the input vectors (x and y). The `trapz` function iterates through the vectors once to calculate the sum of the trapezoid areas.
- Space Complexity: O(1). The `trapz` function requires a constant amount of extra space for storing intermediate calculations, regardless of the input size. It does not create new vectors that are proportional to the input size.
Alternative Approaches
While `trapz` is a straightforward and often efficient method, other numerical integration techniques exist. One alternative is:
- `integral` function: The `integral` function in MATLAB offers adaptive quadrature methods for more accurate numerical integration. Unlike `trapz`, it can handle singularities and provides error estimates. However, it may be slower than `trapz` for simple, well-behaved functions. The adaptive nature means that the number of function evaluations is not necessarily predetermined, leading to potentially longer execution times.
integral(@(x) x.^2, 0, 1)
Conclusion
The `trapz` function in MATLAB is a convenient tool for approximating definite integrals using the trapezoidal rule. It offers a good balance between simplicity and accuracy, especially for functions sampled at a sufficient rate. Understanding its time and space complexity, along with its limitations, allows developers to choose the most appropriate numerical integration method for their specific needs. For more precise integration, especially with complex functions, consider the `integral` function with adaptive quadrature techniques.