# 梯形法误差

clear all; close all; clc;

Nlist = 2.^(2:14);

slist = zeros(1, length(Nlist));

elist = zeros(1, length(Nlist));

for s = 1 : length(Nlist)

N = Nlist(s);

h = 1 /N ;

f = sqrt((N: 2*N)./N);

slist(s) = h * (0.5*(f(1) + f(end)) + sum(f(2: end-1)));

elist(s) = abs(slist(s) - 2/3*(2^1.5-1));

end

h0 = figure;

plot(log2(Nlist), log2(elist),'-*')

Nlist = 2.^(2:14);

slist = zeros(1, length(Nlist));

elist = zeros(1, length(Nlist));

for s = 1 : length(Nlist)

N = Nlist(s);

h = 1 /N ;

f = sqrt((0: N)./N);

slist(s) = h * (0.5*(f(1) + f(end)) + sum(f(2: end-1)));

elist(s) = abs(slist(s) - 2/3);

end

h1 = figure;

plot(log2(Nlist), log2(elist),'-*')

Nlist = 2.^(2:14);

slist2 = zeros(1, length(Nlist));

for s = 1 : length(Nlist)

N = Nlist(s);

h = 1 /N ;

x = (1: N)./N ;

f =  [0,(sin (x) - x)./ sqrt(x)];

slist2(s) = h * (0.5*(f(1) + f(end)) + sum(f(2: end-1))) + 2/3 ;

end

elist2 = abs(slist2(1:end-1) - slist2(end));

h2 = figure;

plot(log2(Nlist(1: end-1)), log2(elist2),'-*')

http://blog.sciencenet.cn/blog-100379-1206773.html

## 全部精选博文导读

GMT+8, 2019-12-9 13:51