导航:首页 > 源码编译 > 龙贝格算法matlab

龙贝格算法matlab

发布时间:2023-09-17 03:33:16

⑴ 用龙贝格法求积分

先用另外2种方法。

format long
%【1】精确值。符号积分
it=int('(2/sqrt(pi))*exp(-x)',0,1)
Accurate=eval(it)
y=inline('(2/sqrt(pi))*exp(-x)')
%【2】Simpson方法
Simpson=quad(y,0,1)
delta=Simpson-Accurate

结果:
Accurate = 0.713271669674918

y = Inline function:
y(x) = (2/sqrt(pi))*exp(-x)

Simpson = 0.713271671228492

delta = 1.553574158208448e-009

【3】从网上找到一个,存为romberg.m
%=================
function R = romberg(f, a, b, n)
format long
% ROMBERG -- Compute Romberg table integral approximation.
%
% SYNOPSIS:
% R = romberg(f, a, b, n)
%
% DESCRIPTION:
% Computes the complete Romberg table approximation to the integral
%
% / b
% I = | f(x) dx
% / a
%
% PARAMETERS:
% f - The integrand. Assumed to be a function callable as
% y = f(x)
% with `x' in [a, b].
% a - Left integration interval endpoint.
% b - Right integration interval endpoint.
% n - Maximum level in Romberg table.
%
% RETURNS:
% R - Romberg table. Represented as an (n+1)-by-(n+1) lower
% triangular matrix of integral approximations.
%
% SEE ALSO:
% TRAPZ, QUAD, QUADL.

% NOTE: all indices adjusted for MATLAB's one-based indexing scheme.

% Pre-allocate the Romberg table. Avoids subsequent re-allocation which
% is often very costly.
R = zeros([n + 1, n + 1]);

% Initial approximation. Single interval trapezoidal rule.
R(0+1, 0+1) = (b - a) / 2 * (feval(f, a) + feval(f, b));

% First column of Romberg table. Increasingly accurate trapezoidal
% approximations.
for i = 1 : n,
h = (b - a) / 2^i;

s = 0;
for k = 1 : 2^(i-1),
s = s + feval(f, a + (2*k - 1)*h);
end

R(i+1, 0+1) = R(i-1+1, 0+1)/2 + h*s;
end

% Richardson extrapolation gives remainder of Romberg table.
%
% Note: The table is computed by columns rather than the more
% traditional row version. The reason is that this prevents frequent
% (and needless) re-computation of the `fac' quantity.
%
% Moreover, MATLAB matrices internally use ``column major'' ordering so
% this version is less harmful to computer memory cache systems. This
% reason is an implementational detail, though, and less important in
% introctory courses such as MA2501.
for j = 1 : n,
fac = 1 / (4^j - 1);
for m = j : n,
R(m+1, j+1) = R(m+1, j-1+1) + fac*(R(m+1, j-1+1) - R(m-1+1, j-1+1));
end
end

function ff=f(x)
ff=2/sqrt(pi)*exp(-x);
%=================

运行:
>> R=romberg('f', 0, 1, 5)

R =

0.771743332258054 0 0 0 0 0
0.728069946441243 0.713512151168973 0 0 0 0
0.716982762290904 0.713287034240791 0.713272026445579 0 0 0
0.714200167058928 0.713272635314936 0.713271675386546 0.713271669814180 0 0
0.713503839348432 0.713271730111600 0.713271669764711 0.713271669675476 0.713271669674932 0
0.713329714927254 0.713271673453528 0.713271669676323 0.713271669674920 0.713271669674918 0.713271669674918

⑵ 用matlab,龙贝格算法计算∫(0到1)[x/(4+x²)]dx的近似值。 求程序代码!!!

%龙贝格求积算法function I=romberg(a,b)h=b-a;T(1)=h/2*(fun(a)+fun(b));m=1;while 1 h=h/2; S(1)=1/2*T(1)+h*sumf(2^(m-1),a,h); for j=1:m S(j+1)=S(j)+(S(j)-T(j))/(4^j-1); end if abs(S(m+1)-T(m))<1e-6 break; end T=S;m=m+1;endI=S(m+1);end
function f=sumf(m,a,h)for j=1:m y(j)=fun(a+(2*j-1)*h);endf=sum(y);end
function f=fun(x)f=x/(4+x^2);end结果:
>> I=romberg(0,1)
I =
0.111571775646293
>>

⑶ 龙贝格算法 椭圆周长 请帮忙修改一下程序~!!!!!!

1、在for(i=1;i<=pow(2,k-1);i++)之前加t=0;否则t重复累加;

2、for(i=1;i<pow(2,k-1);i++)改成for(i=1;i<=pow(2,k-1);i++),i需执行到i=pow(2,k-1);

3、调换for(k=0;k<20;k++)

{

for(m=1;m<4;m++)

的顺序,改为

for(m=1;m<4;m++)

{

for(k=0;k<21-m;k++)否则在算T[k][m]时,T[k+1][m-1]还未知

4、并把k<20改成k<21-m;因为辛普森序列比梯形序列少一项,科茨序列比辛普森序列少一项,龙贝格序列比科茨序列少一项

话说哦。k取20是不太大了点啊,2的20次方可是很大的咯。其实k取6应该就能达到精度要求了吧。

阅读全文

与龙贝格算法matlab相关的资料

热点内容
php登陆次数 浏览:742
python字符转成数字 浏览:822
海川用的是什么服务器 浏览:374
口才是练出来的pdf 浏览:458
云服务器哪个公司性价比高 浏览:515
源码论坛打包 浏览:556
php怎么做成word 浏览:690
python批量生成密钥 浏览:490
程序员要不要考社区人员 浏览:150
app的钱怎么充q币 浏览:813
android银行卡识别 浏览:751
怎么在app投放广告 浏览:11
手机文件管理怎么看app名称 浏览:192
程序员学数学哪本书最全 浏览:784
macd实战选股公式源码 浏览:644
加密芯片的计算方法 浏览:191
手机存储为什么找不到微信文件夹 浏览:697
msf端口迁移命令 浏览:880
工商app积分怎么查询 浏览:146
铁路app怎么买火车票 浏览:311