📄 repeat_s.cpp
字号:
//////////////////////////////////////////////////////
// 程序3.2 — 复化Simpson公式
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#include "expressi.cpp"
// 复化Simpson求积
// 被积函数: f(x) = FxString, 积分区间 : [a,b], 等份数 : n
float RepeatS( char FxString[], float a, float b, int n )
{
int i;
float h, Value1, Value2;
if( n <= 0) return 0;
h = ( b - a ) / n;
if( CreateFx( FxString ) ) return 0; // 建立表达式
Value1 = 0;
Value2 = 0;
for( i = 0; i < n; ++i )
{
Value1 += f( a + ( i + 0.5 ) * h );
}
for( i = 1; i < n; ++i )
{
Value2 += f( a + i * h );
}
return( ( f(a) + f(b) + 4 * Value1 + 2 * Value2 ) * h / 6 );
}
void main()
{
float i1, a, b;
int n;
char FxString[200];
printf( "\nInput function,a,b,n: " );
scanf( "%s %f %f %d", FxString, &a, &b, &n );
i1 = RepeatS( FxString, a, b, n );
printf( "\n%f", i1 );
getch( );
}
/*
运行实例:
Input function,a,b,n: 1/(1+x) 0 1 10
0.693147
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -