romberg.m
来自「小的源码Matlab编程」· M 代码 · 共 37 行
M
37 行
function [rn, r1] = Romberg(fun, a, b, n, varargin)
% Numerical approximation rn of the definite integral from a to b
% that is obtained with the aid of Romberg's method with n rows
% and n columns. fun is a string that names the integrand.
% If integrand depends on parameters, say p1, p2, ... , then
% they should follow the parameter n.
% Second output parameter r1 holds approximate values of the
% computed integral obtained with the aid of the composite
% trapezoidal rule using 1, 2, ... , n subintervals.
h = b - a;
d = 1;
r = zeros(n,1);
r(1) = .5*h*sum(feval(fun,[a b],varargin{:}));
for i=2:n
h = .5*h;
d = 2*d;
t = a + h*(1:2:d);
s = feval(fun, t, varargin{:});
r(i) = .5*r(i-1) + h*sum(s);
end
r1 = r;
d = 4;
for j=2:n
s = zeros(n-j+1,1);
s = r(j:n) + diff(r(j-1:n))/(d - 1);
r(j:n) = s;
d = 4*d;
end
rn = r(n);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?