www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/IntegrateMatrixwithNonuniformSpacingExample.m

    %% Integrate Matrix with Nonuniform Spacing  

%% 
% Create a vector of time values, |X|. Also create a matrix, |Y|,
% containing values evaluated at the irregular intervals in |X|.
X = [1 2.5 7 10]';
Y = [5.2 4.8 4.9 5.1; 7.7 7.0 6.5 6.8; 9.6 10.5 10.5 9.0; 13.2 14.5 13.8 15.2] 

%%
% The columns of |Y| represent velocity data, taken at the times contained
% in |X|, for several different trials.  

%% 
% Use |trapz| to integrate each column independently and find the total
% distance traveled in each trial. Since the function values are not
% evaluated at constant intervals, specify |X| to indicate the spacing
% between the data points.
Q = trapz(X,Y) 

%%
% The result is a row vector of integration values, one for each column
% in |Y|. By default, |trapz| integrates along the first dimension of |Y|
% whose size does not equal 1.  

%% 
% Alternatively, you can integrate the rows of a matrix by specifying |dim
% = 2|. 
%
% In this case, use |trapz| on |Y'|, which contains the velocity data in
% the rows. 
dim = 2;
Q1 = trapz(X,Y',dim) 

%%
% The result is a column vector of integration values, one for each row
% in |Y'|.