連立1次方程式ライブラリ 使用例



[ 簡単な説明 ]

連立1次方程式ライブラリの使用例です。

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

main()
{
	int error, i, iter, j, k, l, m, n;

	static double a1[12] = { 5, 1, 1,10, 1,-7, 2,-7, 1, 1, 6,21};
	static double a2[15] = { 5, 1, 1,10,18, 1,-7, 2,-7,-9, 1, 1, 6,21,11};
	static double ar[12] = { 2, 1, 3,10, 1,-2, 5,-3,-4, 5, 1, 2};
	static double ai[12] = {-1, 2, 1, 2, 1,-4, 3,-1,-1, 1, 4, 4};
	Complex a[12], true[3], cerr;
	double ax[12], aw[15], x[3], err[3], eps;
	double al[6], am[6], au[6], b[6], xx[6];

	printf("*** Gauss-Seidel method ***\n");
	m = 3;
	l = m + 1;
	iter = 20;
	eps = 1.e-6;
	for(i = k = 0; i < m; i++)	for(j = 0; j < l; j++, k++)	ax[k] = a1[k];
	for(i = k = 0; i < m; i++)
	{
		printf("    [ ");
		for(j = 0; j < m; j++)	printf("%5.1f  ", ax[k++]);
		printf("] [ x%1d ] = [ %5.1f ]\n", i + 1, ax[k++]);
	}
	gausei(ax, l, m, iter, eps, x);
	printf("\n* test result from gausei()\n");
	err[0] = x[0] - 1.0;
	err[1] = x[1] - 2.0;
	err[2] = x[2] - 3.0;
	for(i = 0; i < m; i++)	printf(" x%1d = %22.15e  (err=%e)\n", i + 1, x[i], err[i]);

	printf("\n\n*** Gauss method ***\n");
	m = 3;
	l = n = 5;
	eps = 1.e-6;
	for(i = k = 0; i < m; i++)	for(j = 0; j < n; j++, k++)	aw[k] = a2[k];
	for(i = k = 0; i < m; i++)
	{
		printf("   [ ");
		for(j = 0; j < m; j++)	printf("%5.1f ", aw[k++]);
		printf("] [ x%1d ] = ", i + 1);
		for(j = m; j < l; j++)	printf("[ %5.1f ] ", aw[k++]);
		putchar('\n');
	}
	gauss(aw, l, m, n, eps);
	printf("\n* test result from gauss()\n");
	for(i = k = 0; i < m; i++)
	{
		printf(" x%1d = ", i + 1);
		for(j = m, k += m; j < l; j++)	printf("%22.15e  ", aw[k++]);
		putchar('\n');
	}

	printf("\n\n*** Gauss-Jordan method ***\n");
	m = 3;
	l = n = 5;
	eps = 1.e-6;
	for(i = k = 0; i < m; i++)	for (j = 0; j < l; j++, k++)	aw[k] = a2[k];
	for(i = k = 0; i < m; i++)
	{
		printf("   [ ");
		for(j = 0; j < m; j++)	printf("%5.1f ", aw[k++]);
		printf("] [ x%1d ] = ", i + 1);
		for(j = m; j < l; j++)	printf("[ %5.1f ] ", aw[k++]);
		putchar('\n');
	}
	gaujor(aw, l, m, n, eps);
	printf("\n* test result from gaujor()\n");
	for(i = k = 0; i < m; i++)
	{
		printf(" x%1d = ", i + 1);
		for(j = m, k += m; j < l; j++)	printf("%22.15e  ", aw[k++]);
		
		putchar('\n');
	}

	printf("\n\n*** Solution linear equation of tridiagonal matrix\n");
	n = 5;
	for(i = 0; i <= n; i++)
	{
		al[i] = i + 1;
		au[i] = i + 2;
		am[i] = (al[i] + au[i]) * 2.0;
		b[i] = al[i] + am[i] + au[i];
	}
	am[0] += al[0];
	am[n] += au[n];
	printf("  Coefficient   A\n");
	for(i = 0; i <= n; i++)
	{
		if(i == 0)
			printf("                 am[%2d]=%4.1f   au[%2d]=%4.1f    b[%2d]=%4.1f\n",
				i, am[i], i, au[i], i, b[i]);
		if(i == n)
			printf("   al[%2d]=%4.1f   am[%2d]=%4.1f                  b[%2d]=%4.1f\n",
				i, al[i], i, am[i], i, b[i]);
		if((i != 0) & (i != n))
			printf("   al[%2d]=%4.1f   am[%2d]=%4.1f   au[%2d]=%4.1f    b[%2d]=%4.1f\n",
				i, al[i], i, am[i], i, au[i], i, b[i]);
	}
	error = trdiam(al, am, au, b, n, xx);
	for(i = 0; i <= n; i++)
	{
		if(i % 3 == 0)	printf("\n");
		printf("  x[%2d]= %7.5f  ", i, xx[i]);
	}
	printf("\n   Error  = %5d\n", error);

	printf("\n\n*** Gauss-Jordan method (Complex) ***\n");
	l = n = 4;
	m = 3;
	eps = 1.e-6;
	for(i = k = 0; i < m; i++)	for (j = 0; j < l; j++, k++)	a[k] = tocomplex(ar[k], ai[k]);
	for(i = k = 0; i < m; i++)
	{
		printf(" [ ");
		for(j = 0; j < m; j++, k++)	printf("(%5.1f,%5.1f)  ", a[k].r, a[k].r);
		printf("] [ x%1d ] = ", i + 1);
		for(j = m; j < l; j++, k++)	printf("[ (%5.1f,%5.1f) ] ", a[k].r, a[k].i);
		putchar('\n');
	}
	cgauj(a, l, m, n, eps);
	true[0] = tocomplex(1876238. / 853450., -1020591. / 853450);
	true[1] = tocomplex(2112162. / 853450., -914459. / 853450);
	true[2] = tocomplex(60762. / 65650., 39091. / 65650.);
	printf("\n* test result from cgauj()\n");
	for(i = k = 0; i < m; i++)
	{
		printf(" x%1d = ", i + 1);
		for(j = m, k += m; j < l; j++, k++)
		{
			printf("(%10.7f,%10.7f)    ", a[k].r, a[k].i);
			cerr = csub(a[k], true[i]);
			printf("err=(%e,%e)   ", cerr.r, cerr.i);
		}
		putchar('\n');
	}

	return 1;
}

出力結果 1           top (先頭に戻る)
*** Gauss-Seidel method ***
    [   5.0    1.0    1.0  ] [ x1 ] = [  10.0 ]
    [   1.0   -7.0    2.0  ] [ x2 ] = [  -7.0 ]
    [   1.0    1.0    6.0  ] [ x3 ] = [  21.0 ]

* test result from gausei()
 x1 =  9.999999883172546e-01  (err=-1.168275e-08)
 x2 =  1.999999986525477e+00  (err=-1.347452e-08)
 x3 =  3.000000004192878e+00  (err=4.192878e-09)


*** Gauss method ***
   [   5.0   1.0   1.0 ] [ x1 ] = [  10.0 ] [  18.0 ] 
   [   1.0  -7.0   2.0 ] [ x2 ] = [  -7.0 ] [  -9.0 ] 
   [   1.0   1.0   6.0 ] [ x3 ] = [  21.0 ] [  11.0 ] 

* test result from gauss()
 x1 =  1.000000000000000e+00   3.000000000000000e+00  
 x2 =  2.000000000000000e+00   2.000000000000000e+00  
 x3 =  3.000000000000000e+00   1.000000000000000e+00  


*** Gauss-Jordan method ***
   [   5.0   1.0   1.0 ] [ x1 ] = [  10.0 ] [  18.0 ] 
   [   1.0  -7.0   2.0 ] [ x2 ] = [  -7.0 ] [  -9.0 ] 
   [   1.0   1.0   6.0 ] [ x3 ] = [  21.0 ] [  11.0 ] 

* test result from gaujor()
 x1 =  1.000000000000000e+00   3.000000000000000e+00  
 x2 =  2.000000000000000e+00   2.000000000000000e+00  
 x3 =  3.000000000000000e+00   1.000000000000000e+00  


*** Solution linear equation of tridiagonal matrix
  Coefficient   A
                 am[ 0]= 7.0   au[ 0]= 2.0    b[ 0]= 9.0
   al[ 1]= 2.0   am[ 1]=10.0   au[ 1]= 3.0    b[ 1]=15.0
   al[ 2]= 3.0   am[ 2]=14.0   au[ 2]= 4.0    b[ 2]=21.0
   al[ 3]= 4.0   am[ 3]=18.0   au[ 3]= 5.0    b[ 3]=27.0
   al[ 4]= 5.0   am[ 4]=22.0   au[ 4]= 6.0    b[ 4]=33.0
   al[ 5]= 6.0   am[ 5]=33.0                  b[ 5]=39.0

  x[ 0]= 1.00000    x[ 1]= 1.00000    x[ 2]= 1.00000  
  x[ 3]= 1.00000    x[ 4]= 1.00000    x[ 5]= 1.00000  
   Error  =     0


*** Gauss-Jordan method (Complex) ***
 [ (  2.0,  2.0)  (  1.0,  1.0)  (  3.0,  3.0)  ] [ x1 ] = [ ( 10.0,  2.0) ] 
 [ (  1.0,  1.0)  ( -2.0, -2.0)  (  5.0,  5.0)  ] [ x2 ] = [ ( -3.0, -1.0) ] 
 [ ( -4.0, -4.0)  (  5.0,  5.0)  (  1.0,  1.0)  ] [ x3 ] = [ (  2.0,  4.0) ] 

* test result from cgauj()
 x1 = ( 2.1984158,-1.1958416)    err=(0.000000e+00,-2.220446e-16)   
 x2 = ( 2.4748515,-1.0714851)    err=(0.000000e+00,0.000000e+00)   
 x3 = ( 0.9255446, 0.5954455)    err=(1.110223e-16,-1.110223e-16)   

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

int main(void)
{
	static double a1[9] = { 5, 1, 1, 1, -7, 2, 1, 1, 6 };
	static double c1[3] = { 10, -7, 21 };
	static double a2[36] = {20,  3,  4,  5,  6,  7,
							 3, 40,  5,  6,  7,  8,
							 4,  5, 60,  7,  8,  9,
							 5,  6,  7, 80,  9, 10,
							 6,  7,  8,  9,100, 11,
							 7,  8,  9, 10, 11,120 };
	static double c2[6] = {45, 69, 93, 117, 141, 165};
	static double x1[3], x2[6];
	double w;
	int i, j;

	for(i = 0; i < 6; i++)
	{
		for(j = 0; j < 6; j++)	printf("%5.1f  ", a2[i * 6 + j]);
		printf("%6.1f\n", c2[i]);
	}
	printf("ludcmp = %d\n", ludcmp(a2, b2, x2, 1.e-6, 5));
	for(i = 0; i < 6; i++)	printf("x(%d) = %8.5f\n", i, x2[i]);

	cgm(a1, c1, x1, 3);

	printf("\n\n* test result from cgm()\n");
	for(i = 0; i < 3; i++)
	{
		w = 0;
		for(j = 0; j < 3; j++)	w += a1[i * 3 + j] * x1[j];
		printf(" x%1d = %22.15e  (err=%e)\n", i + 1, x1[i], c1[i] - w);
	}

	cgm(a2, c2, x2, 6);

	printf("\n* test result from cgm()\n");
	for(i = 0; i < 6; i++)
	{
		w = 0;
		for(j = 0; j < 6; j++)	w += a2[i * 6 + j] * x2[j];
		printf(" x%1d = %22.15e  (err=%e)\n", i + 1, x2[i], c2[i] - w);
	}

	return 1;
}

出力結果 2           top (先頭に戻る)
 20.0    3.0    4.0    5.0    6.0    7.0    45.0
  3.0   40.0    5.0    6.0    7.0    8.0    69.0
  4.0    5.0   60.0    7.0    8.0    9.0    93.0
  5.0    6.0    7.0   80.0    9.0   10.0   117.0
  6.0    7.0    8.0    9.0  100.0   11.0   141.0
  7.0    8.0    9.0   10.0   11.0  120.0   165.0
ludcmp = 0
x(0) =  1.00000
x(1) =  1.00000
x(2) =  1.00000
x(3) =  1.00000
x(4) =  1.00000
x(5) =  1.00000


* test result from cgm()
 x1 =  1.000000000000000e+00  (err=0.000000e+00)
 x2 =  2.000000000000000e+00  (err=-1.776357e-15)
 x3 =  3.000000000000000e+00  (err=0.000000e+00)

* test result from cgm()
 x1 =  9.999999999999631e-01  (err=4.952483e-12)
 x2 =  9.999999999999485e-01  (err=6.892265e-12)
 x3 =  9.999999999999273e-01  (err=9.706014e-12)
 x4 =  9.999999999998936e-01  (err=1.419664e-11)
 x5 =  9.999999999998344e-01  (err=2.219736e-11)
 x6 =  9.999999999996806e-01  (err=4.254730e-11)