function [I, B] = romberg(f, a, b, n)

% n: order of Romberg method % f: integrand % a, b: integration interval % I: integral value % B: Romberg table

h = b - a; R = zeros(n, n);

% compute the first column R(1,1) = h/2 * (f(a) + f(b)); for i = 2:n h = h/2; x = a + h:h:b-h; R(i,1) = 1/2 * R(i-1,1) + h * sum(f(x)); end

% compute the rest of the table for j = 2:n for i = j:n R(i,j) = (4^(j-1) * R(i,j-1) - R(i-1,j-1)) / (4^(j-1) - 1); end end

I = R(n,n); B = R;

end

% example usage f = @(x) exp(-x.^2); a = 0; b = 1; n = 6; [I, B] = romberg(f, a, b, n); disp(B

用matlab编写龙贝格求积公式要求误差小于10^−6并列出龙贝格表格

原文地址: https://www.cveoy.top/t/topic/hnhz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录