c语言指针函数定积分 c语言函数里的指针

求c语言指向函数的指针求定积分公式的原理讲解

下面的代码就是使用矩形法求定积分的。

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了富源免费建站欢迎大家使用!

分别求了sin(x) cos(x) e^x 的定积分

#include iostream

#include cmath

using namespace std;

float integral (float (*p) (float), float a, float b, int n);

float fsin (float); // 对fsin函数作声明

float fcos (float); // 对fcos函数作声明

float fexp (float); // 对fexp函数作声明

int main()

{

float a1, b1, a2, b2, a3, b3, c, (*p) (float);

int n = 40; // 划分40个小矩形

cout "input a1,b1(sin(x) 定积分的下限和上限):"; //输入求sin(x) 定积分的下限和上限

cin a1 b1;

cout "input a2,b2:(cos(x) 定积分的下限和上限 )"; // 输入求cos(x) 定积分的下限和上限

cin a2 b2;

cout "input a3,b3:(e^x定积分的下限和上限)";

cin a3 b3;

p = fsin;

c = integral(p, a1, b1, n); // 求出sin(x)的定积分

cout "The integral of sin(x) is :" c endl;

p = fcos;

c = integral(p, a2, b2, n); // 求出cos(x)的 定积分

cout "The integral of cos(x) is :" c endl

p = fexp;

c = integral(p, a3, b3, n); // 求出 的定积分

cout "The integral of exp(x) is :" c endl;

return 0;

}

float integral(float (*p) (float), float a, float b, int n)

//用矩形法求定积分的通用函数

{

int i;

float x, h, s;

h = (b - a) / n;

x = a;

s = 0;

for (i = 1; i = n; i++)

{

x = x + h;

s = s + (*p) (x) * h;

}

return(s);

}

float fsin(float x) // 计算sin(x) 的函数

{

return sin(x);

}

float fcos(float x) // 计算cos(x) 的函数

{

return cos(x);

}

float fexp(float x) // 计算exp(x)的函数

{

return exp(x);

}

用C语言求给定函数给定定义域的定积分

#include stdio.h

#include math.h

double f1( double x )

{

return 1 / ( 1 + 4 * x * x );

}

double f2( double x )

{

return ( log(x+1) ) / ( 1 + x*x) ;

}

double jifen( double a, double b, int n, double (*f)(double) )

{

double h = (b-a)/2;

double s = 0.0;

int i;

for( i=0; in; i++ )

s = s + 0.5 * ( f(a+i*h) + f(a+(i+1)*h) ) * h;

return s;

}

int main()

{

double a, b, s;

printf( "函数1 f(x) = 1/(1+4x^2) 区间[-1,1]定积分:%f\n", jifen( -1, 1, 1000, f1) );

printf( "函数2 f(x) = ln(1+x)/(1+x^2) 区间[0,1]定积分:%f\n", jifen( 0, 1, 1000, f2) );

}

C语言用指向函数的指针作函数参数写求通用定积分公式。

看不出有什么编译或执行的问题。但您的integral函数中的循环控制有点小问题,应该是

for ( i=a;ib;i+=trace )

您现在的写法,多加了一次计算,不知道这个对运算结果会不会造成影响。


标题名称:c语言指针函数定积分 c语言函数里的指针
网站路径:http://lszwz.com/article/ddshjei.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款