1 | function anss = rombquad(f,a,b,m) |
---|
2 | %ROMBQUAD(f,a,b,m) evaluates the integral of f from a to b. |
---|
3 | % f is an inline function or function handle. |
---|
4 | % a is the lower limit of integration. |
---|
5 | % b is the upper limit of integration. |
---|
6 | % m - is the exponent in the relative error. For example m = 10 implies an |
---|
7 | % estimated relative error of 1e-10. |
---|
8 | % |
---|
9 | % Example: |
---|
10 | % |
---|
11 | % f = @(x) x.^2; % Vectoized function handle. |
---|
12 | % rombquad(f,0,2); % The integral from 0 to 2. |
---|
13 | % |
---|
14 | % See also quad, quadl, quadv and gaussquad (on the FEX) |
---|
15 | % |
---|
16 | % Author: Matt Fig |
---|
17 | % Contact: popkenai@yahoo.com |
---|
18 | |
---|
19 | if nargin<3 |
---|
20 | error ('Not enough arguments. See help.') |
---|
21 | elseif nargin<4 || floor(m)~=m || m <1 |
---|
22 | m = 10; |
---|
23 | end |
---|
24 | |
---|
25 | if isinf(a) || isinf(b) |
---|
26 | error('Infinite limits not allowed.') |
---|
27 | end |
---|
28 | |
---|
29 | h = 2.^((1:m)-1)*(b-a)/2^(m-1); % These are the intervals used. |
---|
30 | k1 = 2.^((m-2):-1:-1)*2+1; % Index into the intervals. |
---|
31 | f1 = feval(f,a:h(1):b); % Function evaluations. |
---|
32 | R = zeros(1,m); % Pre-allocation. |
---|
33 | % Define the starting vector: |
---|
34 | for ii = 1:m |
---|
35 | R(ii) = 0.5*h(ii)*(f1(1)+2*... |
---|
36 | sum(f1(k1(end-ii+1):k1(end-ii+1)-1:end-1)) + f1(end)); |
---|
37 | end |
---|
38 | % Interpolations: |
---|
39 | for jj = 2:m |
---|
40 | jpower = (4^(jj-1)-1); |
---|
41 | for kk = 1:(m-jj+1) |
---|
42 | R(kk) = R(kk)+(R(kk)-R(kk+1))/jpower; |
---|
43 | end |
---|
44 | end |
---|
45 | anss = R(1); |
---|