[数值分析]数值积分 - 梯形/辛普森Simpson/柯特斯Newton-cotes方法

clear;close all;clc;
format long

% matlab 中精度较高的积分
f = @(x) sin(x)/(x);

x       = 0.0000001:0.0000001:2*pi;
y       = sin(x)./(x);

figure(1);
plot(x,y);grid on;

% 采用高采样率进行积分 [0,1]
sum = 0;
for i = 1:1:10000000
    sum = sum + y(i);
end
sum = sum * 0.0000001;
disp('High Resulotion');
disp(sum);

%% 对比区 [0,1]区间 1000点采样
x       = 0.001:0.001:2*pi;
y       = sin(x)./(x);

% 1000点采样黎曼和方法
sum = 0;
for i = 1:1:1000
    sum = sum + y(i);
end
sum = sum * 0.001;
disp('黎曼和矩形逼近');
disp(sum);

% 辛普森3点方法
sum = 0;
sum = sum + (0.002 - 0)*((1/6)*(1)+(4/6)*(y(1))+(1/6)*(y(2)));
for i = 4:2:1000
    sum = sum + (0.002 - 0)*((1/6)*(y(i-2))+(4/6)*(y(i-1))+(1/6)*(y(i)));
end
disp('辛普森3点方法');
disp(sum);

% 柯特思 Cotes 4点方法
sum = 0;
sum = sum + y(1)*0.001;
for i = 4:3:1000
    sum = sum + (0.003 - 0)*((1/8)*(y(i-3))+(3/8)*(y(i-2))+(3/8)*(y(i-1))+(1/8)*(y(i)));
end
disp('柯特思 Cotes 4点方法');
disp(sum);








[数值分析]数值积分 - 梯形/辛普森Simpson/柯特斯Newton-cotes方法_第1张图片

[数值分析]数值积分 - 梯形/辛普森Simpson/柯特斯Newton-cotes方法_第2张图片

 

 

 

你可能感兴趣的:(数值分析,计算方法)