当前位置:网站首页>[numerical analysis exercise] numerical integration (complex trapezoid, complex Simpson, Romberg integral) C with STL implementation
[numerical analysis exercise] numerical integration (complex trapezoid, complex Simpson, Romberg integral) C with STL implementation
2022-07-27 21:56:00 【Dream and wake up, happy and carefree】
Explain the algorithm a little
Complex trapezoid and complex Simpson In fact, it's all a train of thought , It's just that the compound trapezoid is added at one time 2 Two function values , Jump a space h distance ; And refolding Simpson It's a one-time addition 3 It's worth , Jump once 2h Distance of .
The formula in the book is beautifully written , But it is not suitable for computer implementation , So the above idea is cyclic plus , And the book is one step .
Romberg The algorithm is troublesome , The formula given in the book is not clear enough , My code is an improvement on the formula in the book .
Logic is :T Construction and SCR There are two kinds of constructions
T structure , The latter is equal to one-half of the former plus one delta_T
This delta_T yes , Sum of function values of all new points , Multiply by the new h It can be concluded that , The formula in the book is not suitable for computer implementation , So I modified it , It is also the first time in my life to use double Measure to write for loop .
SCR The structure belongs to the same system , Very simple formula .
The general idea is to divide the structure into two sections , First construct the first R1, So , We'll construct a total of T1-T8,S1-S4,C1-C2,R1, Such a triangular matrix .
Then there is a simple do while loop , Add accuracy control judgment .
Romberg Because it's an iterative method , Therefore, the accuracy can be approximately regarded as before and after R The difference between the
// Numerical integration
// Complex trapezoidal integral and complex simpson The formula , Romberg formula
#include<cstdio>
#include<climits>
#include<cmath>
#include<map> // Discrete storage
double f(double x)
{
return 1.0 / (x*x+1);
}
double trapzoid(double a,double b,int M)
{
double ans = 0;
double h = (b - a) / M;
for (double x = a; x < b; x += h)
{
printf("%f x\n", x);
ans += (f(x) + f(x + h));
}
ans = ans * h / 2; // The length of the middle segment of the compound trapezoid is h
return ans;
}
double simpson(double a,double b,int M)
{
if (M % 2)// Non even number
{
puts(" Enter even number M");
return -1;
}
double ans = 0;
double h = (b - a) / M;
for (double x = a; x < b; x += 2 * h)
{
printf("%f x\n", x);
ans += (f(x) + 4 * f(x + h) + f(x + 2 * h));
}
ans = ans * 2 * h / 6; // The length of the compound Simpson section 2h
return ans;
}
double romberg(double a, double b, double EPSILON)// Iteration is controlled by precision
{
std::map<int, double>T, S, C, R;
T[1] = (b - a) * (f(a) + f(b)) / 2;
printf("T[1] %f\n", T[1]);
// Construct a triangular matrix
for (int Ti = 2; Ti <= 8; Ti *= 2)// Construct to T8
{
double delta_T = 0;
double h = (b - a) / Ti;
for (double start = a + h; start < b; start += 2 * h)// Insert the new node
{
delta_T += f(start);
}
delta_T *= h;
T[Ti] = T[Ti / 2] / 2 + delta_T;
printf("T[%d] %f\n", Ti, T[Ti]);
}
for (int Si = 1; Si <= 4; Si *= 2)// structure Si To S4
{
S[Si] = pow(4,1) / (pow(4,1)-1) * T[Si * 2]
- 1.0 / (pow(4,1)-1) * T[Si];
printf("S[%d] %f\n", Si, S[Si]);
}
for (int Ci = 1; Ci <= 2; Ci *= 2)// structure Ci To C2
{
C[Ci] = pow(4, 2) / (pow(4, 2) - 1) * S[Ci * 2]
- 1.0 / (pow(4, 2) - 1) * S[Ci];
printf("C[%d] %f\n", Ci, C[Ci]);
}
R[1] = pow(4, 3) / (pow(4, 3) - 1) * C[2] // structure R1
- 1.0 / (pow(4, 3) - 1) * C[1];
printf("R[1] %f\n", R[1]);
// Layer by layer structure R, End by precision control
int Ri = 1;
do {
Ri *= 2;
int Ti = Ri * 8;
int Si = Ri * 4;
int Ci = Ri * 2;
double h = (b - a) / Ti;
double delta_T = 0;
// Current layer T
for (double start = a + h; start < b; start += 2 * h)
{
delta_T += f(start);
}
delta_T *= h;
T[Ti] = T[Ti / 2] / 2 + delta_T;
printf("T[%d] %f\n", Ti, T[Ti]);
// Current layer S
S[Si] = pow(4, 1) / (pow(4, 1) - 1) * T[Si * 2]
- 1.0 / (pow(4, 1) - 1) * T[Si];
printf("S[%d] %f\n", Si, S[Si]);
// Current layer C
C[Ci] = pow(4, 2) / (pow(4, 2) - 1) * S[Ci * 2]
- 1.0 / (pow(4, 2) - 1) * S[Ci];
printf("C[%d] %f\n", Ci, C[Ci]);
// Current layer R
R[Ri] = pow(4, 3) / (pow(4, 3) - 1) * C[Ri * 2]
- 1.0 / (pow(4, 3) - 1) * C[Ri];
printf("R[%d] %f\n", Ri, R[Ri]);
} while (R[Ri] - R[Ri / 2] > EPSILON);
return R[Ri];
}
int main(void)
{
double a, b, epsilon;
int M;
scanf("%lf %lf %d %lf", &a, &b, &M , &epsilon);
printf("%f\n", trapzoid(a, b, M));
printf("%f\n", simpson(a, b, M));
printf("%f\n", romberg(a, b, epsilon));
return 0;
}边栏推荐
- Log4j 漏洞仍普遍存在?
- Openai issued a document to introduce the latest application of Dall · E 2: fully enter the field of artistic creation and design
- In crsctl, the function of displayed home
- @Autowired注解与@Resource注解的区别
- Software testing interview question: how many strategies are there for system testing?
- Finish learning redis cluster solution at one go
- Commercial delay of self-developed 5g chip? Apple iPhone will adopt Qualcomm 5g chip in the next four years
- University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l
- Recursion / backtracking (Part 1)
- 2021-11-05 understanding of class variables and class methods
猜你喜欢

LVS+Keepalived高可用群集

MySQL execution process and order

JVM memory model interview summary

一篇文章带你走进pycharm的世界----别再问我pycharm的安装和环境配置了!!!

Station B collapsed. If we were the developer responsible for the repair that night
![Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?](/img/dd/8ee989f5c9db632f78e79425497e71.png)
Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?

B站崩了,那晚负责修复的开发人员做了什么?

学完4种 Redis 集群方案要多久?我一口气给你说完

@Detailed introduction of requestparam annotation

It seems to be a bug of thread pool, but I think the source code design is unreasonable.
随机推荐
软件测试面试题:通过画因果图来写测试用例的步骤为___、___、___、___及把因果图转换为状态图共五个步骤。 利用因果图生成测试用例的基本步骤是?
Log4j vulnerability is still widespread and continues to cause impact
排序(冒泡排序)后面学习持续更新其它排序方法
对L1正则化和L2正则化的理解[通俗易懂]
How can anyone ask how MySQL archives data?
MySQL data recovery process is based on binlog redolog undo
Talk about MySQL transaction two-phase commit
XML writing gap animation popupwindow realizes the animation of emergence and exit
In depth understanding of recursive method calls (including instance maze problem, tower of Hanoi, monkey eating peach, fiboracci, factorial))
Search, insert and delete of hash table
如何实现一个好的知识管理系统?
枚举和注解
Import word document pictures blocking and non blocking IO operations
Software test interview question: when saving a text file under windows, a save dialog box will pop up. If a test case is established for the file name, how should equivalent classes be divided?
Why use MQ message oriented middleware? These questions must be solved
Log4j 漏洞仍普遍存在,并持续造成影响
美司法部增加针对华为的指控,包括窃取商业秘密等16项新罪名
Qt取出输入框字符串,lineEdit
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
2021-11-05 understand main method syntax, code block and final keyword