📄 no.7.c
字号:
#include "stdio.h"
#include "conio.h"
#define ROOT_PRECISION 10e-5
#define PI 3.1415926
float func1(float x)
{
return (1 / (1 + x*x));
}
float func2(float x)
{
return (1 / x);
}
float sinpson(float a, float b, int n, float (*f)(float x))
{
float h;
float s = 0;
float x;
int i;
h = ( b - a ) / ( 2 * n );
x = a;
s = f(x);
for (i = 1; i <= 2 * n - 1; i++) {
x = x + h;
if (i % 2)
s = s + 4 * f(x);
else
s = s + 2 * f(x);
}
x = x + h;
s = s + f(x);
s = s * ( ( b - a ) / ( 6 * n ) );
return s;
}
float preciseIntegral(float a, float b, float (*f)(float x), float precision)
{
float s1, s2;
int n = 10;
s2 = sinpson(a, b, n, f);
do {
n = n * 2;
s1 = s2;
s2 = sinpson(a, b, n, f);
}while ( s2 - s1 > precision || s1 - s2 > precision);
return s1;
}
int main()
{
float s;
s = preciseIntegral(0, 1, func1, ROOT_PRECISION);
printf("%.5f\n", s);
s = sinpson(1, 2, 10, func2);
printf("%.4f\n", s);
getch();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -