
Use MatLab to write a function called trapint.m that takes data sample locations and function samplings at those locations as inputs and returns an approximation of the integral over the sample range based on the trapezoidal rule. The sampling locations need not be uniformly spaced and may be arbitrary in number when using this method. The function should display nothing to the screen unless an error or warning occurs. The equation to implement is:
I = (f(x_0) + (f(x_1) * (x_1 – x_0) / 2) + (f(x_1) + f(x_2)) * (x_2 – x_1) / 2 + … + (f(x_n – 1) + f(x_n)) * (x_n – x_n – 1) / 2
The detailed specification is as follows:
- Valid call: I = trapint(x, fx)
- Inputs:
- x: (N-element vector) numerical data for sample locations.
- fx: (N-element vector) function data taken at locations defined in input x.
- Output: I: Approximation for the integral of fx over its sampled range.
- Assumptions: The ith element of x corresponds to the ith element of fx.
- Validation: Your code should throw an error if any of the following are true:
- x and fx are not the same length
- either of the inputs are not vectors (e.g., a matrix)
- either of the inputs contains something other than numerical dat
The Correct Answer and Explanation is:
✅ trapint.m MATLAB Code:
function I = trapint(x, fx)
% trapint.m
% Approximates the integral of sampled data using the trapezoidal rule.
% Validate input types
if ~isvector(x) || ~isvector(fx)
error('Inputs x and fx must be vectors.');
end
if length(x) ~= length(fx)
error('Vectors x and fx must be of the same length.');
end
if ~isnumeric(x) || ~isnumeric(fx)
error('Inputs must be numeric vectors.');
end
% Ensure column vectors for consistency
x = x(:);
fx = fx(:);
% Calculate integral using trapezoidal rule
I = 0;
for i = 1:length(x)-1
h = x(i+1) - x(i); % width of interval
avg_height = (fx(i) + fx(i+1)) / 2; % average height
I = I + h * avg_height; % area of trapezoid
end
end
📝 300-Word Explanation:
The trapint.m MATLAB function implements numerical integration using the trapezoidal rule, which is a basic yet effective method for approximating the definite integral of a function based on sampled data points. This technique is particularly useful when the function itself is not analytically integrable, or only discrete data is available.
The trapezoidal rule approximates the area under a curve by dividing it into trapezoids rather than rectangles. For each pair of consecutive points (xi,f(xi))(x_i, f(x_i)) and (xi+1,f(xi+1))(x_{i+1}, f(x_{i+1})), the area of the trapezoid formed is computed as: Ai=12(f(xi)+f(xi+1))⋅(xi+1−xi)A_i = \frac{1}{2}(f(x_i) + f(x_{i+1})) \cdot (x_{i+1} – x_i)
Summing these areas for all intervals gives an estimate of the integral.
In this function:
- Inputs
xandfxrepresent vectors of sample locations and their corresponding function values. - Several validations ensure that the inputs are vectors of the same length and consist only of numeric data.
- The vectors are reshaped as column vectors for internal consistency.
- A loop iterates through the intervals, computes each trapezoid’s area, and accumulates the total.
This function does not assume uniform spacing between x values, making it more general than simple implementations. It is also robust in error handling, making it practical for real-world data integration. The final result I is the numerical approximation of the integral of fx over the range defined by x.

.