View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Modified trapezoidal integration over specific limits of integration.

3.0 | 1 rating Rate this file 5 Downloads (last 30 days) File Size: 1.66 KB File ID: #35738 Version: 1.0
image thumbnail

Modified trapezoidal integration over specific limits of integration.


Damon Bradley (view profile)


This function is similar to trapz, except it specifies upper and lower integration limits a and b.

| Watch this File

File Information

% function y = trapint(f,x,a,b)
% Author: Damon Bradley
% Purpose: Modified trapezoidal integration over specific limits of integration.
% Last revised: Tuesday, March 20, 2012
% Inputs:
% ----------------------------------------------
% x - Domain of function f
% f - A fuction defined by a vector of points
% a - Lower limit of integration
% b - Upper limit of integration
% NOTE: If a and b are not specified, this function is reduced to the
% normal trapz function
% Outputs:
% ----------------------------------------------
% y - Integration result
% Prerequisites:
% None
% Usage Example
% clear
% clc
% close all
% N=1000000; mu=5; sig=1; % Define parameters for an N-sample Gaussian random vector
% [counts bins] = hist(mu+sig*randn(N,1),sqrt(N)); % Compute histogram.
% p = counts/((bins(2)-bins(1))*N); % Compute corresponding PDF
% stairs(bins,p)
% y=trapint(bins,p,mu,inf) % Integrate from the mean to infinity. Should get 0.5 in theory.
% y =
% 0.5019

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (1)
04 Apr 2013 Georg Wiora

Simple and intuitive. This was definitely missing in matlab!

Following improvements could be made:
1. Handle start and end of range in case where it is not exactly on a sample point
2. Check that x is monotonically increasing and resort vectors if necessary.

There is a BUG in the calculation of the index of the upper bound. You must call the min() function to find the correct index, not the max() function.

Contact us