Mercurial > hg > octave-lyh
comparison scripts/general/quadv.m @ 11078:2aec7e3b8553
Fix help string in general/quadv.m
author | Carlo de Falco <kingcrimson@tiscali.it> |
---|---|
date | Tue, 05 Oct 2010 03:29:43 -0400 |
parents | be55736a0783 |
children | fd0a3ac60b0e |
comparison
equal
deleted
inserted
replaced
11077:5dd5df43d392 | 11078:2aec7e3b8553 |
---|---|
21 ## @deftypefnx {Function File} {@var{q} =} quadv (@var{f}, @var{a}, @var{b}, @var{tol}) | 21 ## @deftypefnx {Function File} {@var{q} =} quadv (@var{f}, @var{a}, @var{b}, @var{tol}) |
22 ## @deftypefnx {Function File} {@var{q} =} quadv (@var{f}, @var{a}, @var{b}, @var{tol}, @var{trace}) | 22 ## @deftypefnx {Function File} {@var{q} =} quadv (@var{f}, @var{a}, @var{b}, @var{tol}, @var{trace}) |
23 ## @deftypefnx {Function File} {@var{q} =} quadv (@var{f}, @var{a}, @var{b}, @var{tol}, @var{trace}, @var{p1}, @var{p2}, @dots{}) | 23 ## @deftypefnx {Function File} {@var{q} =} quadv (@var{f}, @var{a}, @var{b}, @var{tol}, @var{trace}, @var{p1}, @var{p2}, @dots{}) |
24 ## @deftypefnx {Function File} {[@var{q}, @var{fcnt}] =} quadv (@dots{}) | 24 ## @deftypefnx {Function File} {[@var{q}, @var{fcnt}] =} quadv (@dots{}) |
25 ## | 25 ## |
26 ## Numerically evaluate integral using adaptive Simpson's rule. | 26 ## Numerically evaluate the integral of @var{f} from @var{a} to @var{b} |
27 ## @code{quadv (@var{f}, @var{a}, @var{b})} approximates the integral of | 27 ## using adaptive Simpson's rule. |
28 ## @code{@var{f}(@var{x})} to the default absolute tolerance of @code{1e-6}. | |
29 ## @var{f} is either a function handle, inline function or string | 28 ## @var{f} is either a function handle, inline function or string |
30 ## containing the name of the function to evaluate. The function @var{f} | 29 ## containing the name of the function to evaluate. |
31 ## must accept a string, and can return a vector representing the | 30 ## The function defined by @var{f} may be a scalar, vector or array-valued. |
32 ## approximation to @var{n} different sub-functions. | |
33 ## | 31 ## |
34 ## If defined, @var{tol} defines the absolute tolerance to which to | 32 ## If a value for @var{tol} is given, it defines the tolerance used to stop |
35 ## which to integrate each sub-interval of @code{@var{f}(@var{x})}. | 33 ## the adaptation procedure, otherwise the default value of 1e-6 is used. |
36 ## While if @var{trace} is defined, displays the left end point of the | 34 ## |
37 ## current interval, the interval length, and the partial integral. | 35 ## The algorithm used by @code{quadv}, involves recursively subdividing the |
36 ## integration interval and applying Simpson's rule on each sub-interval. | |
37 ## If @var{trace} is @var{true}, after computing each of these partial integrals, | |
38 ## display the total number of function evaluations, the left end of the sub-interval, | |
39 ## the length of the sub-interval and the approximation of the integral over the sub-interval. | |
38 ## | 40 ## |
39 ## Additional arguments @var{p1}, etc., are passed directly to @var{f}. | 41 ## Additional arguments @var{p1}, etc., are passed directly to @var{f}. |
40 ## To use default values for @var{tol} and @var{trace}, one may pass | 42 ## To use default values for @var{tol} and @var{trace}, one may pass |
41 ## empty matrices. | 43 ## empty matrices. |
44 ## | |
42 ## @seealso{triplequad, dblquad, quad, quadl, quadgk, trapz} | 45 ## @seealso{triplequad, dblquad, quad, quadl, quadgk, trapz} |
43 ## @end deftypefn | 46 ## @end deftypefn |
44 | 47 |
45 function [q, fcnt] = quadv (f, a, b, tol, trace, varargin) | 48 function [q, fcnt] = quadv (f, a, b, tol, trace, varargin) |
46 if (nargin < 3) | 49 if (nargin < 3) |
78 endif | 81 endif |
79 if (isinf (fb)) | 82 if (isinf (fb)) |
80 fb = feval (f, b - myeps * (b-a), varargin{:}); | 83 fb = feval (f, b - myeps * (b-a), varargin{:}); |
81 endif | 84 endif |
82 | 85 |
83 h = (b - a) / 2; | 86 h = (b - a); |
84 q = (b - a) / 6 * (fa + 4 * fc + fb); | 87 q = (b - a) / 6 * (fa + 4 * fc + fb); |
85 | 88 |
86 [q, fcnt, hmin] = simpsonstp (f, a, b, c, fa, fb, fc, q, fcnt, abs (b - a), | 89 [q, fcnt, hmin] = simpsonstp (f, a, b, c, fa, fb, fc, q, fcnt, abs (h), |
87 tol, trace, varargin{:}); | 90 tol, trace, varargin{:}); |
88 | 91 |
89 if (fcnt > 10000) | 92 if (fcnt > 10000) |
90 warning ("maximum iteration count reached"); | 93 warning ("maximum iteration count reached"); |
91 elseif (isnan (q) || isinf (q)) | 94 elseif (isnan (q) || isinf (q)) |
132 %!assert (quadv (@sin, 0, pi), 2, 1e-5) | 135 %!assert (quadv (@sin, 0, pi), 2, 1e-5) |
133 | 136 |
134 %% Handles weak singularities at the edge | 137 %% Handles weak singularities at the edge |
135 %!assert (quadv (@(x) 1 ./ sqrt(x), 0, 1), 2, 1e-5) | 138 %!assert (quadv (@(x) 1 ./ sqrt(x), 0, 1), 2, 1e-5) |
136 | 139 |
140 %% Handles vector-valued functions | |
141 %!assert (quadv (@(x) [(sin (x)), (sin (2 * x))], 0, pi), [2, 0], 1e-5) | |
142 |