Matlab实现——Recursive Trapezoidal Rule

欢迎前往个人博客 驽马点滴 和视频空间 哔哩哔哩-《挨踢日志》

rctrap.m

%Program 7.3 (Recursive Trapezoidal Rule).

function T=rctrap(f,a,b,n,tol)

%Input    - f is the integrand input as a string ’f’

%   - a and b are upper and lower limits of integration

%   - n is the number of times for recursion

%   -tol is the tolerance

%Output  - T is the recursive trapezoidal rule list

M=1;

h=b-a;

T=zeros(1,n+1);

T(1)=h*(feval(f,a)+feval(f,b))/2;

err=1;

while(err>tol)

    for j=1:n

         M=2*M;

         h=h/2;

         s=0;

         for k=1:M/2

            x=a+h*(2*k-1);

            s=s+feval(f,x);

         end

         T(j+1)=T(j)/2+h*s;

         err=abs(T(j)-T(j+1));

    end

end

fun.m

 

function f=fun(x)

f=2*x-x.^2;

 

 

 

untitled.m

 

a=0;

b=2;

n=20;

tol=5*10e-6;

f='fun';

T=rctrap(f,a,b,n,tol)

欢迎前往个人博客 驽马点滴 和视频空间 哔哩哔哩-《挨踢日志》

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值