?? quad_m.m
字號:
function [Q,fcnt] = quad_m(funfcn,a,b,tol,trace,varargin)
%QUAD_M Numerically evaluate integral, adaptive Simpson quadrature.
% difference with QUAD: QUAD_M accepts vector arguments
%
% Q = QUAD(FUN,A,B) tries to approximate the integral of function
% FUN from A to B to within an error of 1.e-6 using recursive
% adaptive Simpson quadrature. The function Y = FUN(X) should
% accept a column-vector argument X and return a column-vector
% result Y, the integrand evaluated at each element of X.
% The function should return a matrix when one of the additional
% arguments (see below) is a row-vecor. The resulting matrix should
% have integrand evaluated at each element of X in the column-direction
% and of the argument in the row-direction.
%
%
% Q = QUAD(FUN,A,B,TOL) uses an absolute error tolerance of TOL
% instead of the default, which is 1.e-6. Larger values of TOL
% result in fewer function evaluations and faster computation,
% but less accurate results. The QUAD function in MATLAB 5.3 used
% a less reliable algorithm and a default tolerance of 1.e-3.
%
% [Q,FCNT] = QUAD(...) returns the number of function evaluations.
%
% QUAD(FUN,A,B,TOL,TRACE) with non-zero TRACE shows the values
% of [fcnt a b-a Q] during the recursion.
%
% QUAD(FUN,A,B,TOL,TRACE,P1,P2,...) provides for additional
% arguments P1, P2, ... to be passed directly to function FUN,
% FUN(X,P1,P2,...). Pass empty matrices for TOL or TRACE to
% use the default values.
% The additional arguments can be vectors.
%
% Use array operators .*, ./ and .^ in the definition of FUN
% so that it can be evaluated with a vector argument.
%
% Function QUADL may be more efficient with high accuracies
% and smooth integrands.
%
% Example:
% FUN can be specified three different ways.
%
% A string expression involving a single variable:
% Q = quad('1./(x.^3-2*x-5)',0,2);
%
% An inline object:
% F = inline('1./(x.^3-2*x-5)');
% Q = quad(F,0,2);
%
% A function handle:
% Q = quad(@myfun,0,2);
% where myfun.m is an M-file:
% function y = myfun(x)
% y = 1./(x.^3-2*x-5);
%
% See also QUADL, DBLQUAD, INLINE, @.
% Based on "adaptsim" by Walter Gander.
% Ref: W. Gander and W. Gautschi, "Adaptive Quadrature Revisited", 1998.
% http://www.inf.ethz.ch/personal/gander
% Copyright 1984-2001 The MathWorks, Inc.
% Based on QUAD. (see above)
% M.F.P. Tolsma, Signals, Systems and Control Group, Applied Physics, TU Delft
% http://www.tn.tudelft.nl/mmr
% copyright remains by author
% $Revision: 1.4 $ $Date: 2002/01/21 11:45:00 $
mxsize=1;
for cnt=1:length(varargin)
tval=length(varargin{cnt});
if (tval>1)
if mxsize==1
mxsize=tval;
else
if ~(mxsize==tval)
error('The optional arguments must either be a scalar or an fixed sized row-vector')
end;
end;
end;
end;
f = fcnchk(funfcn);
if nargin < 4 | isempty(tol), tol = 1.e-6; end;
if nargin < 5 | isempty(trace), trace = 0; end;
% Initialize with three unequal subintervals.
h = 0.13579*(b-a);
x = [a a+h a+2*h (a+b)/2 b-2*h b-h b];
y = feval(f, x, varargin{:});
fcnt = 7;
% Fudge endpoints to avoid infinities.
% for each element of vector y
if ~all(isfinite(y(:,1))) | ~all(isfinite(y(:,7))) %check only when there are at least some problems
for cnt=1:mxsize
if ~isfinite(y(cnt,1))
for cnt2=1:length(varargin)
varsubset{cnt2}=varargin{cnt2}(1);
end;
y(cnt,1) = feval(f,a+eps*(b-a),varsubset{:});
fcnt = fcnt+1;
end;
if ~isfinite(y(cnt,7))
for cnt2=1:length(varargin)
if length(varargin{cnt2})>1
varsubset{cnt2}=varargin{cnt2}(13)
else
varsubset{cnt2}=varargin{cnt2}(1);
end;
end;
y(cnt,13) = feval(f,a+eps*(b-a),varsubset{:});
fcnt = fcnt+1;
end;
end;
end;
% Call the recursive core integrator.
hmin = eps/1024*abs(b-a);
[Q(:,1),fcnt,warn(1)] = ...
quadstep(f,x(1),x(3),y(:,1),y(:,2),y(:,3),tol,trace,fcnt,hmin,varargin{:});
[Q(:,2),fcnt,warn(2)] = ...
quadstep(f,x(3),x(5),y(:,3),y(:,4),y(:,5),tol,trace,fcnt,hmin,varargin{:});
[Q(:,3),fcnt,warn(3)] = ...
quadstep(f,x(5),x(7),y(:,5),y(:,6),y(:,7),tol,trace,fcnt,hmin,varargin{:});
Q = sum(Q,2);
warn = max(warn);
switch warn
case 1
warning('Minimum step size reached; singularity possible.')
case 2
warning('Maximum function count exceeded; singularity likely.')
case 3
warning('Infinite or Not-a-Number function value encountered.')
otherwise
if ~all(isfinite(Q))
warning('Some Infinite or Not-a-Number function values encountered.')
end
end
% ------------------------------------------------------------------------
function [Q,fcnt,warn] = quadstep (f,a,b,fa,fc,fb,tol,trace,fcnt,hmin,varargin)
%QUADSTEP Recursive core routine for function QUAD.
maxfcnt = 10000;
% Evaluate integrand twice in interior of subinterval [a,b].
h = b - a;
c = (a + b)/2;
if abs(h) < hmin | c == a | c == b
% Minimum step size reached; singularity possible.
Q = h*fc;
warn = 1;
return
end
x = [(a + c)/2, (c + b)/2];
y = feval(f, x, varargin{:});
fcnt = fcnt + 2;
if fcnt > maxfcnt
% Maximum function count exceeded; singularity likely.
Q = h*fc;
warn = 2;
return
end
fd = y(:,1);
fe = y(:,2);
% Three point Simpson's rule.
Q1 = (h/6)*(fa + 4*fc + fb);
% Five point double Simpson's rule.
Q2 = (h/12)*(fa + 4*fd + 2*fc + 4*fe + fb);
% One step of Romberg extrapolation.
Q = Q2 + (Q2 - Q1)/15;
if ~any(isfinite(Q)) %all are infinite or NAN (no one is finite)
% Infinite or Not-a-Number function value encountered.
warn = 3;
return
end
% Check accuracy of integral over this subinterval.
if all((abs(Q2 - Q) <= tol)) %all are below than tolerance
warn = 0;
return
end;
varlist=find(isfinite(Q) & (abs(Q2 - Q) > tol)); %these must still be done
if ~(length(varlist)==length(fa)) %more arguments as the ones that must be done
targ=varargin;
for cnt=1:length(targ)
if length(targ{cnt})>1
varargin{cnt}=targ{cnt}(varlist);
else
varargin{cnt}=targ{cnt};
end;
end;
end;
if trace
disp(sprintf('%8.0f %16.10f %18.8e %16.10f',fcnt,a,h,Q))
end
% Subdivide into two subintervals.
[Qac,fcnt,warnac] = quadstep(f,a,c,fa(varlist),fd(varlist),fc(varlist),tol,trace,fcnt,hmin,varargin{:});
[Qcb,fcnt,warncb] = quadstep(f,c,b,fc(varlist),fe(varlist),fb(varlist),tol,trace,fcnt,hmin,varargin{:});
Q(varlist) = Qac + Qcb;
warn = max(warnac,warncb);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -