微積分ライブラリ 使用例



[ 簡単な説明 ]

微積分ライブラリの使用例です。
プログラム・ソース 1
("test3.c")
----- 出力例 1 :lagdif( ),spldif( )
プログラム・ソース 2
("test4.c")
----- 出力例 2 :trap( ),simpei( ),simpui( ),splitg( ),cheb3( )〜cheb6( ),dgl3( )〜dgl48( )
プログラム・ソース 3
("test5.c")
----- 出力例 3 :dglg3( )〜dglg10( )
プログラム・ソース 4
("test6.c")
----- 出力例 4 :dgh10( ),dgh15( )
プログラム・ソース 5
("test7.c")
----- 出力例 5 :simpe2( )
プログラム・ソース 6
("test8.c")
----- 出力例 6 :nc1( )〜nc8( ),weddle( ),hardy( ),lomberg( )
プログラム・ソース 7
("test9.c")
----- 出力例 7 :difm1( )〜difm8( ),difb2( )〜difb5( ),diff2( )〜diff5( )

プログラム・ソース("test3.c")           top (先頭に戻る)
/*		test3.c		*/
#include	<stdio.h>
#include	"sslib.h"

double _f(double x){return x;}

int main(void)
{
	int i, n;
	double diff, x[20], y[20], xx;

	n = 11;
	xx = 0.5;

	for(i = 0; i < n; i++)
	{
		x[i] = M_PI / (double)n * (double)i;
		y[i] = sin(x[i]);
	}
	printf("* Difference by lagdif\n");
	printf("   Difference  = %22.15e   x = %5.1f\n",
			(diff = lagdif(x, y, n, xx)), xx);
	printf("   Exact value = %22.15e\n", cos(xx));
	printf("   Error       = %e\n", diff - cos(xx));

	printf("* Difference by spldif\n");
	printf("   Difference  = %22.15e   x = %5.1f\n",
			(diff = spldif(x, y, n, xx)), xx);
	printf("   Exact value = %22.15e\n", cos(xx));
	printf("   Error       = %e\n", diff - cos(xx));

	return 1;
}

出力結果 1           top (先頭に戻る)
* Difference by lagdif
   Difference  =  8.775825640935108e-01   x =   0.5
   Exact value =  8.775825618903728e-01
   Error       = 2.203138e-09
* Difference by spldif
   Difference  =  8.776641556411690e-01   x =   0.5
   Exact value =  8.775825618903728e-01
   Error       = 8.159375e-05

プログラム・ソース("test4.c")           top (先頭に戻る)
/*		test4.c		*/
#include	<stdio.h>
#include	"sslib.h"

/* should be defind by user as _f(x) for cheb3(), cheb4(), chev6()
									 for dgl3(), dgl10(), dgl20(), dgl32(), dgl48()	*/

double _f(double x)
{
	return exp(x);
}

int main(void)
{
	int i, n;
	double x[100], y[100], a, b, exa, h, *p, *q, s;

	n = 100;
	for(i = 0, p = x, q = y; i < n; i++, p++, q++)
	{
		*p = (double)i / (double)(n - 1);
		*q = exp(*p);
	}
	s = trap(x, y, n);
	exa = exp(1.) - 1.;
	printf("* Integral by Trapezoid's rule\n");
	printf("    exp(x)    x = 0 --> 1  (devided point %d)\n", n);
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);

	n = 21;
	h = M_PI / (double)(n - 1);
	for(i = 0, p = y; i < n; i++, p++)	*p = sin(h * (double)i);
	s = simpei(y, n, h);
	exa = 2.;
	printf("* Integral by Simpson's method\n");
	printf("    exp(x)    x = 0 --> 1  (devided point %d)\n", n);
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);

	n = 21;
	h = M_PI / (double)(n - 1);
	for(i = 0, p = x, q = y; i < n; i++, p++, q++)
	{
		*p = h * (double)i;
		if ((i != 0) && (i != n - 1))	*p += .005 * (double)i;
		*q = sin(*p);
	}
	s = simpui(x, y, n);
	exa = 2.;
	printf("* Integral by Simpson's method (unequal interval)\n");
	printf("    exp(x)    x = 0 --> 1  (devided point %d)\n", n);
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);

	n = 21;
	h = M_PI / (double)(n - 1);
	for(i = 0, p = x, q = y; i < n; i++, p++, q++)
	{
		*p = h * (double)i;
		*q = sin(*p);
	}
	s = splitg(x, y, n);
	exa = 2.;
	printf("* Integral by Cubic Spline Function's rule\n");
	printf("    exp(x)    x = 0 --> 1  (devided point %d)\n", n);
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);

	a = 0.;
	b = 1.;
	printf("* Integration by Chebyshev's method\n");
	printf("   exp(x)   x=%4.1f --> %4.1f\n", a, b);
	exa = exp(1.) - 1.;
	s = cheb3(a, b);
	printf("*   3 - point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);
	s = cheb4(a, b);
	printf("*   4 - point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);
	s = cheb6(a, b);
	printf("*   6 - point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);

	a = 0.;
	b = 1.;
	printf("* Integration by Gauss-Legendre's method\n");
	printf("   exp(x)   x=%4.1f --> %4.1f\n", a, b);
	exa = exp(1.) - 1.;
	s = dgl3(a, b);
	printf("*   3 - point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);
	s = dgl10(a, b);
	printf("*   10 - point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);
	s = dgl20(a, b);
	printf("*   20 - point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);
	s = dgl32(a, b);
	printf("*   32 - point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);
	s = dgl48(a, b);
	printf("*   48 - point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);

	return 1;
}

出力結果 2           top (先頭に戻る)
* Integral by Trapezoid's rule
    exp(x)    x = 0 --> 1  (devided point 100)
    Integration =  1.718296438183448e+00
    Exact       =  1.718281828459045e+00
    Error       =  1.46097e-05
* Integral by Simpson's method
    exp(x)    x = 0 --> 1  (devided point 21)
    Integration =  2.000006784441801e+00
    Exact       =  2.000000000000000e+00
    Error       =  6.78444e-06
* Integral by Simpson's method (unequal interval)
    exp(x)    x = 0 --> 1  (devided point 21)
    Integration =  1.999992061966324e+00
    Exact       =  2.000000000000000e+00
    Error       = -7.93803e-06
* Integral by Cubic Spline Function's rule
    exp(x)    x = 0 --> 1  (devided point 21)
    Integration =  2.000048923424123e+00
    Exact       =  2.000000000000000e+00
    Error       =  4.89234e-05
* Integration by Chebyshev's method
   exp(x)   x= 0.0 -->  1.0
*   3 - point
    Integration =  1.718136569435059e+00
    Exact       =  1.718281828459045e+00
    Error       = -1.45259e-04
*   4 - point
    Integration =  1.718281217601509e+00
    Exact       =  1.718281828459045e+00
    Error       = -6.10858e-07
*   6 - point
    Integration =  1.718281827642581e+00
    Exact       =  1.718281828459045e+00
    Error       = -8.16464e-10
* Integration by Gauss-Legendre's method
   exp(x)   x= 0.0 -->  1.0
*   3 - point
    Integration =  1.718281004372524e+00
    Exact       =  1.718281828459045e+00
    Error       = -8.24087e-07
*   10 - point
    Integration =  1.718281828459045e+00
    Exact       =  1.718281828459045e+00
    Error       = -2.22045e-16
*   20 - point
    Integration =  1.718281828459045e+00
    Exact       =  1.718281828459045e+00
    Error       = -2.22045e-16
*   32 - point
    Integration =  1.718281828459045e+00
    Exact       =  1.718281828459045e+00
    Error       = -4.44089e-16
*   48 - point
    Integration =  1.718281828459045e+00
    Exact       =  1.718281828459045e+00
    Error       = -4.44089e-16

プログラム・ソース("test5.c")           top (先頭に戻る)
/*		test5.c		*/
#include	<stdio.h>
#include	"sslib.h"

/* should be defind by user as _f(x) for dglg3(), dglg5(), dglg10()	*/ 

double _f(double x){ return x;}

int main(void)
{
	double exa, s;

	exa = 1.;
	printf("* Integral by Gauss-Laguerre's method\n");
	printf("    f(x)*exp(-x)    x = 0 --> ∞\n");
	s = dglg3();
	printf("  3-point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);
	s = dglg5();
	printf("  5-point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);
	s = dglg10();
	printf("  10-point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);

	return 1;
}

出力結果 3           top (先頭に戻る)
* Integral by Gauss-Laguerre's method
    f(x)*exp(-x)    x = 0 --> ∞
  3-point
    Integration =  9.999999999999145e-01
    Exact       =  1.000000000000000e+00
    Error       = -8.54872e-14
  5-point
    Integration =  9.999999999999944e-01
    Exact       =  1.000000000000000e+00
    Error       = -5.55112e-15
  10-point
    Integration =  1.000000000000000e+00
    Exact       =  1.000000000000000e+00
    Error       =  0.00000e+00

プログラム・ソース("test6.c")           top (先頭に戻る)
/*		test6.c			*/
#include	<stdio.h>
#include	"sslib.h"

/* should be defind by user as _f(x) for dgh10(), dgg15()	*/ 

double _f(double x){ return x * x;}

int main(void)
{
	double exa, s;

	exa = sqrt(M_PI) / 2.;
	printf("* Integral by Gauss-Hermite's method\n");
	printf("    f(x)*exp(-x*x)    x = -∞ --> ∞\n");
	s = dgh10();
	printf("  10-point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);
	s = dgh15();
	printf("  15-point\n");
	printf("    Integration = %22.15e\n", s);
	printf("    Exact       = %22.15e\n", exa);
	printf("    Error       = %12.5e\n", s - exa);

	return 1;
}

出力結果 4           top (先頭に戻る)
* Integral by Gauss-Hermite's method
    f(x)*exp(-x*x)    x = -∞ --> ∞
  10-point
    Integration =  8.862269254527584e-01
    Exact       =  8.862269254527579e-01
    Error       =  4.44089e-16
  15-point
    Integration =  8.862269254527527e-01
    Exact       =  8.862269254527579e-01
    Error       = -5.21805e-15

プログラム・ソース("test7.c")           top (先頭に戻る)
/*		test7.c			*/
#include <stdio.h>
#include <stdlib.h>
#include "sslib.h"

int main(void)
{
	double *y, exa, h1, h2, mn, s;
	int i, ij, j, m, m1, n, n1;

	m = 31;
	n = 21;
	m1 = m - 1;
	n1 = n - 1;
	mn = 1 / (double)(m1 * n1);

	y = (double *)malloc(m * n * sizeof(double));
	if(y == NULL)
	{
		fprintf(stderr, "Error : out of memory\n");
		exit(-1);
	}
	for(i = ij = 0; i < m; i++)
		for (j = 0; j < n; j++, ij++)	y[ij] = mn * i * j;
	h1 = 1. / (double)m1;
	h2 = 1. / (double)n1;
	exa = 0.25;
	s = simpe2(y, m, n, h1, h2);
	printf("*  2-Dimensional Integration of Simpson's method\n");
	printf("    integral  x * y   (x : 0 - 1 , y : 0 - 1)");
	printf("   (Divided point %d, %d)\n", m, n);
	printf("   Result : %22.15e\n", s);
	printf("   Exact  : %22.15e\n", exa);
	printf("   Error  :	%12.5e\n", s - exa);

	return 1;
}

出力結果 5           top (先頭に戻る)
*  2-Dimensional Integration of Simpson's method
    integral  x * y   (x : 0 - 1 , y : 0 - 1)   (Divided point 31, 21)
   Result :  2.500000000000000e-01
   Exact  :  2.500000000000000e-01
   Error  :	 0.00000e+00

プログラム・ソース("test8.c")           top (先頭に戻る)
/*		test8.c			*/
#include	<stdio.h>
#include	"sslib.h"

double _f(double x)
{
	return sin(x);
}

int main(void)
{
	double eps, xmin, xmax;
	int n;

	eps = 1.e-15;
	xmin = 0.;
	xmax = M_PI_2;
	n = 100;
	printf("Integration by NC_1    : %24.17e\n", nc1(xmin, xmax, n));
	printf("Integration by NC_2    : %24.17e\n", nc2(xmin, xmax, n));
	printf("Integration by NC_3    : %24.17e\n", nc3(xmin, xmax, n));
	printf("Integration by NC_4    : %24.17e\n", nc4(xmin, xmax, n));
	printf("Integration by NC_5    : %24.17e\n", nc5(xmin, xmax, n));
	printf("Integration by NC_6    : %24.17e\n", nc6(xmin, xmax, n));
	printf("Integration by NC_7    : %24.17e\n", nc7(xmin, xmax, n));
	printf("Integration by NC_8    : %24.17e\n", nc8(xmin, xmax, n));
	printf("Integration by Weddle  : %24.17e\n", weddle(xmin, xmax, n));
	printf("Integration by Hardy   : %24.17e\n", hardy(xmin, xmax, n));
	printf("Integration by Lomberg : %24.17e\n", lomberg(xmin, xmax, eps));

	return 1;
}

出力結果 6           top (先頭に戻る)
Integration by NC_1    :  9.99979438239607554e-01
Integration by NC_2    :  1.00000000002113953e+00
Integration by NC_3    :  1.00000000000939515e+00
Integration by NC_4    :  9.99999999999999778e-01
Integration by NC_5    :  9.99999999999999889e-01
Integration by NC_6    :  1.00000000000000000e+00
Integration by NC_7    :  1.00000000000000000e+00
Integration by NC_8    :  1.00000000000000000e+00
Integration by Weddle  :  1.00000000000000022e+00
Integration by Hardy   :  1.00000000000000022e+00
Integration by Lomberg :  1.00000000000000022e+00

プログラム・ソース("test9.c")           top (先頭に戻る)
/*	test9.c	*/
#include <stdio.h>
#include "sslib.h"

double _f(double x)
{
	return sin(x);
}

int main(void)
{
	double cosx, h, x, y;

	x = M_PI * 0.0795;
	h = 0.01;
	cosx = cos(x);
	y = diff2(x, h);
	printf("diff2(x)=%e   error=%e\n", y, y - cosx);
	y = diff3(x, h);
	printf("diff3(x)=%e   error=%e\n", y, y - cosx);
	y = diff4(x, h);
	printf("diff4(x)=%e   error=%e\n", y, y - cosx);
	y = diff5(x, h);
	printf("diff5(x)=%e   error=%e\n", y, y - cosx);
	y = difb2(x, h);
	printf("difb2(x)=%e   error=%e\n", y, y - cosx);
	y = difb3(x, h);
	printf("difb3(x)=%e   error=%e\n", y, y - cosx);
	y = difb4(x, h);
	printf("difb4(x)=%e   error=%e\n", y, y - cosx);
	y = difb5(x, h);
	printf("difb5(x)=%e   error=%e\n", y, y - cosx);
	y = difm1(x, h);
	printf("difm1(x)=%e   error=%e\n", y, y - cosx);
	y = difm2(x, h);
	printf("difm2(x)=%e   error=%e\n", y, y - cosx);
	y = difm3(x, h);
	printf("difm3(x)=%e   error=%e\n", y, y - cosx);
	y = difm4(x, h);
	printf("difm4(x)=%e   error=%e\n", y, y - cosx);
	y = difm5(x, h);
	printf("difm5(x)=%e   error=%e\n", y, y - cosx);
	y = difm6(x, h);
	printf("difm6(x)=%e   error=%e\n", y, y - cosx);
	y = difm7(x, h);
	printf("difm7(x)=%e   error=%e\n", y, y - cosx);
	y = difm8(x, h);
	printf("difm8(x)=%e   error=%e\n", y, y - cosx);

	return 1;
}

出力結果 7           top (先頭に戻る)
diff2(x)=9.690048e-01   error=3.223617e-05
diff3(x)=9.689727e-01   error=6.469370e-08
diff4(x)=9.689726e-01   error=-1.929405e-09
diff5(x)=9.689726e-01   error=-4.458767e-12
difb2(x)=9.690050e-01   error=3.235975e-05
difb3(x)=9.689725e-01   error=-5.888007e-08
difb4(x)=9.689726e-01   error=-1.945878e-09
difb5(x)=9.689726e-01   error=3.780198e-12
difm1(x)=9.689565e-01   error=-1.614946e-05
difm2(x)=9.689726e-01   error=-3.229861e-10
difm3(x)=9.689726e-01   error=-5.884182e-15
difm4(x)=9.689726e-01   error=1.110223e-15
difm5(x)=9.689726e-01   error=1.110223e-15
difm6(x)=9.689726e-01   error=1.221245e-15
difm7(x)=9.689726e-01   error=1.221245e-15
difm8(x)=9.689726e-01   error=1.332268e-15