↓ Skip to main content
  1. Posts/

辛普森积分学习笔记

·338 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

16沈阳的阴影还在orz,来学习一下辛普森积分。

参考资料:梯形多步法和辛普森积分

辛普森计算定积分

辛普森积分是一种数值积分方法(然后现在只记得教计算方法的是一个小姐姐,并不记得当时学了什么orz

大概就是用二次抛物线近似计算曲边梯形面积,辛普森积分公式如下:

$$ \int_a^b f(x) \, dx \approx \frac{b-a}{6} \left(f(a) + 4 f\left(\frac{a+b}{2}\right) + f(b)\right) $$

下面放代码:

1double f(double x){return sin(x)*x;}//这是被积函数
2double simpson(double l,double r){return (r-l)*(f(l)+f(r)+4*f((l+r)/2))/6;}
3double di(double l,double r){//越二分以得到更精确的结果
4    double m=(l+r)/2;
5    double ans=simpson(l,r);
6    if(sgn(ans-simpson(l,m)-simpson(m,r))==0)return ans; //在误差之内
7    return di(l,m)+di(m,r); //不再误差之内就继续将区间缩小
8}

切了个练手题,慢慢补充总结。

需要注意的是,simpson对精度要求比较高。。。eps开到1E-10才过。。

hdu1724题目链接

hdu1724解题报告

所以问题就在于写出积分公式(如果是多重积分要变成累次积分?orz)

Related

BZOJ 3680: 吊打XXX (广义费马点,模拟退火+爬山)

3680: 吊打XXX # Time Limit: 10 Sec Memory Limit: 128 MBSec Special Judge Submit: 2043 Solved: 732 [Submit][Status][Discuss] Description # gty又虐了一场比赛,被虐的蒟蒻们决定吊打gty。gty见大势不好机智的分出了n个分身,但还是被人多势众的蒟蒻抓住了。蒟蒻们将 n个gty吊在n根绳子上,每根绳子穿过天台的一个洞。这n根绳子有一个公共的绳结x。吊好gty后蒟蒻们发现由于每个gty重力不同,绳 结x在移动。蒟蒻wangxz脑洞大开的决定计算出x最后停留处的坐标,由于他太弱了决定向你求助。 不计摩擦,不计能量损失,由于gty足够矮所以不会掉到地上。

poj 1385 Lifting the Stone (多边形的重心)

·397 words·1 min
poj 1385 题目链接 题意:求多边形的重心。 思路: 抄模板(逃 嘛。。三角形的重心是三个点坐标的平均数。。。 多边形的重心其实就是先求三角形的重心然后再加权平均一下就好了。。。权值是面积比。

poj 1380 Equipment Box (简单几何)

·291 words·1 min
题目链接 题意:问一个小矩形能否放在一个大矩形中,给定两个矩形的尺寸。 思路:主要是斜着放比较难判断。学弟貌似写了离散化角度旋转。。。我的做法是。。直接考虑对角线。。。因为我认为对角线是最有可能放进去的位置。

BZOJ 1656: [Usaco2006 Jan] The Grove 树木(神奇的bfs之射线法)

·1177 words·3 mins
1656: [Usaco2006 Jan] The Grove 树木 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 143 Solved: 88 [Submit][Status][Discuss] Description # The pasture contains a small, contiguous grove of trees that has no ‘holes’ in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She’s just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn’t think you could pass ’through’ the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take. Happily, Bessie lives on a simple world where the pasture is represented by a grid with R rows and C columns (1 <= R <= 50, 1 <= C <= 50). Here’s a typical example where ‘.’ is pasture (which Bessie may traverse), ‘X’ is the grove of trees, ‘’ represents Bessie’s start and end position, and ‘+’ marks one shortest path she can walk to circumnavigate the grove (i.e., the answer): …+… ..+X+.. .+XXX+. ..+XXX+ ..+X..+ …+++ The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she’s starting ‘outside’ the grove instead of in a sort of ‘harbor’ that could complicate finding the best path.