How would you know how many segments to use in a Trapezoidal rule of integration to get an accurate value of the integral? This can be done by applying the Trapezoidal rule for 1 segment rule, then 2 segment rule, followed by 4 segment rule and so on. As soon as the absolute relative approximate error (page 5-6) between the consecutive answers becomes less than the pre-specified tolerance chosen by the user, you would have your integral within the accuracy you desired.
Here is a MATLAB program that does that for you. The MATLAB program that can be downloaded at http://numericalmethods.eng.usf.edu/blog/trapezoidal_rule_automatic.m (better to download it as single quotes from the web-post do not translate correctly with the MATLAB editor). The html file showing the mfile and the command window output is here: http://numericalmethods.eng.usf.edu/blog/html/trapezoidal_rule_automatic.html
% Simulation : Using Trapezoidal rule as an automatic integrator
% Language : Matlab 2007a
% Authors : Autar Kaw, http://numericalmethods.eng.usf.edu
% Mfile available at
% Last Revised : October 12, 2008
% Abstract: This program uses multiple-segment Trapezoidal
% rule to integrate f(x) from x=a to x=b within a pre-specified tolerance
disp(‘This program uses multiple-segment Trapezoidal rule as an automatic integrator’)
disp(‘to integrate f(x) from x=a to x=b within a pre-specified tolerance’)
disp(‘Author: Autar K Kaw.’)
%INPUTS. If you want to experiment, these are the only variables
% you should and can change.
% a = Lower limit of integration
% b = Upper limit of integration
% nmax = Maximum number of segments
% tolerance = pre-specified tolerance in percentage
% f = inline function as integrand
func=[‘ The integrand is =’ char(f)];
fprintf(‘ Lower limit of integration, a= %g’,a)
fprintf(‘\n Upper limit of integration, b= %g’,b)
fprintf(‘\n Maximum number of segments, nmax = %g’,nmax)
fprintf(‘\n Pre-specified percentage tolerance, eps = %g’,tolerance)
% Doing the automatic integration
% Calculating the integral using 1-segment rule
% Initializing ea as greater than pre-specified tolerance for loop to work
% Starting with 2-segments inside the while loop
while (ea>tolerance) & (n<=nmax)
% Keeping track of used number of segments
% Calculating the absolute relative approximate error
ea = abs((current_integral-previous_integral)/current_integral)*100;
% Doubling the number of segments for next estimate of the integral
fprintf(‘ Number of segments used =%g’, nused)
fprintf(‘\n Approximate value of integral is =%g’,current_integral)
fprintf(‘\n Absolute percentage relative approximate error =%g’, ea)
disp(‘ NOTE: The value of integral is not within the pre-specified tolerance’)
This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu.
An abridged (for low cost) book on Numerical Methods with Applications will be in print (includes problem sets, TOC, index) on December 10, 2008 and available at lulu storefront.
Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.
One thought on “An automatic integrator using Trapezoidal rule”
I hope you don’t find this out of place, but I came across your blog post a while back when I was looking for a trapezoidal rule excel function to approximate the exposure of some nuclear control blades. The MATLAB one worked well but I decided to make a VBA tutorial for an excel trapezoidal rule function, too.