自适应辛普森法通过\(Simpson\)公式,用二次函数来拟合,实现时用二分递归来自动控制区间分割的大小,既保证精度,又保证速度
\(Simpson\)公式推导
\[\int_a^bf(x)dx\]
\[\approx\int_a^bAx^2+Bx+C\]
\[=\frac{A}{3}(b^3-a^3)+\frac{B}{2}(b^2-a^2)+C(b-a)\]
\[=\frac{(b-a)}{6}(2Ab^2+2Aab+2Aa^2+3Bb+3Ba+6C)\]
\[=\frac{(b-a)}{6}(Aa^2+Ba+C+Ab^2+Bb+C+4A(\frac{a+b}{2})^2+4B(\frac{a+b}{2})+4C)\]
\[=\frac{(b-a)}{6}(f(a)+f(b)+4f(\frac{a+b}{2}))\]
求\(\displaystyle{\int_l^r\frac{cx+d}{ax+b}\mathrm{d}x}\)
\(code:\)
double f(double x) { return (c*x+d)/(a*x+b); } double simpson(double l,double r) { double mid=(l+r)/2; return (r-l)*(f(l)+f(r)+4*f(mid))/6; } double sol(double l,double r,double eps) { double mid=(l+r)/2; double a=simpson(l,mid),b=simpson(mid,r),m=simpson(l,r); if(fabs(a+b-m)<=eps) return a+b+(a+b-m); return sol(l,mid,eps/2)+sol(mid,r,eps/2); }
来源:https://www.cnblogs.com/lhm-/p/12229789.html