【发布时间】:2020-11-22 19:04:10
【问题描述】:
我想在 matlab 中计算我的复合辛普森规则的步长。这是我的代码
% Estimate the number of steps n required for the three point composite Simpson’s rule,
% function's integral from 0 to 1 and the function is ∫ 4/(1+x.^2)=pi within an error bound of 10−6
h=0.01;
n=1000;
x=pi;
a=0;
b=1;
x=zeros(1,n);
f=@(x)4./(1+x.^2);
nn=(b-a)/h;
xexact=integral(f,a,b);
p=0;
q=0;
for i=1:n
x(i)=a+(i-1)*h;
end
for i=1:n-1
p=p+2*(f(x(i)))+4*(f(x(i)+h/2));
end
nn=((b-a)*(f(a)+f(a+h/2)+p+f(b)))./(6*x)
当我运行代码时,我得到一个 1 * 1000 个元素的向量,但我想得到步数 nn=(b-a)/h .. 我做错了什么?
谢谢
【问题讨论】:
标签: matlab