xgauleg.c

来自「< C语言数值算法程序大全>>配套程序」· C语言 代码 · 共 42 行

C
42
字号
/* Driver for routine gauleg */

#include <stdio.h>
#include <math.h>
#include "nr.h"
#include "nrutil.h"

#define NPOINT 10
#define X1 0.0
#define X2 1.0
#define X3 10.0

float func(x)
float x;
{
	return x*exp(-x);
}

main()
{
	int i;
	float xx=0.0;
	float *x,*w;

	x=vector(1,NPOINT);
	w=vector(1,NPOINT);
	gauleg(X1,X2,x,w,NPOINT);
	printf("\n%2s %10s %12s\n","#","x[i]","w[i]");
	for (i=1;i<=NPOINT;i++)
		printf("%2d %12.6f %12.6f\n",i,x[i],w[i]);
	/* Demonstrate the use of gauleg for integration */
	gauleg(X1,X3,x,w,NPOINT);
	for (i=1;i<=NPOINT;i++)
		xx += (w[i]*func(x[i]));
	printf("\nIntegral from GAULEG: %12.6f\n",xx);
	printf("Actual value: %12.6f\n",
		(1.0+X1)*exp(-X1)-(1.0+X3)*exp(-X3));
	free_vector(w,1,NPOINT);
	free_vector(x,1,NPOINT);
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?