[ 簡単な説明 ] 代数方程式ライブラリの使用例です。
|
/* test10.c */ #include <stdio.h> #include "sslib.h" /* _f(x) should be defined by user */ double _f(double x) { return exp(x) - 2.; } int main(void) { Complex a, b, c, x[20]; double d[20], eps, xe, xs, h, xr; int i, iter, n; a = tocomplex( 1., 0.); b = tocomplex(-3., 0.); c = tocomplex( 2., 0.); qurt(a, b, c, x); printf(" (%4.1f , %4.1f) * x ^ 2\n", a.r, a.i); printf("+ (%4.1f , %4.1f) * x\n", b.r, b.i); printf("+ (%4.1f , %4.1f) = 0\n", c.r, c.i); printf(" x1 = ( %22.15e , %22.15e )\n", x[0].r, x[0].i); printf(" x2 = ( %22.15e , %22.15e )\n", x[1].r, x[1].i); a = tocomplex( 1., 0.); b = tocomplex(-2., 0.); c = tocomplex( 1., 0.); qurt(a, b, c, x); printf(" (%4.1f , %4.1f) * x ^ 2\n", a.r, a.i); printf("+ (%4.1f , %4.1f) * x\n", b.r, b.i); printf("+ (%4.1f , %4.1f) = 0\n", c.r, c.i); printf(" x1 = ( %22.15e , %22.15e )\n", x[0].r, x[0].i); printf(" x2 = ( %22.15e , %22.15e )\n", x[1].r, x[1].i); a = tocomplex( 1., 0.); b = tocomplex(-4., 0.); c = tocomplex( 8., 0.); qurt(a, b, c, x); printf(" (%4.1f , %4.1f) * x ^ 2\n", a.r, a.i); printf("+ (%4.1f , %4.1f) * x\n", b.r, b.i); printf("+ (%4.1f , %4.1f) = 0\n", c.r, c.i); printf(" x1 = ( %22.15e , %22.15e )\n", x[0].r, x[0].i); printf(" x2 = ( %22.15e , %22.15e )\n", x[1].r, x[1].i); d[0] = 0.1; d[1] = -0.5; d[2] = 0.8; d[3] = 1.4; carda(d, x); for(i = 0; i <= 3; i++) printf(" %4.1f * x ^ %d\n", d[i], 3 - i); for (i = 0; i < 3; i++) printf(" x%d = ( %22.15e, %22.15e )\n", i + 1, x[i].r, x[i].i); d[0] = 0.1; d[1] = -0.5; d[2] = 0.8; d[3] = 1.4; n = 3; eps = 1.e-6; iter = 100; xr = newton(d, n, eps, iter); for(i = 0; i <= n; i++) printf(" %4.1f * x ^ %d\n", d[i], n - i); printf(" x = %22.15e\n", xr); d[0] = 1.; d[1] = -10.; d[2] = 55.; d[3] = -140.; d[4] = 174.; d[5] = -100.; n = 5; eps = 1.e-6; iter = 100; bairs(d, n, eps, iter, x); for(i = 0; i <= n; i++) printf(" %6.1f * x ^ %d\n", d[i], n - i); for (i = 0; i < n; i++) printf(" x%d = ( %22.15e, %22.15e )\n", i + 1, x[i].r, x[i].i); xs = 0.; xe = 1.; h = 0.1; eps = 1.e-6; iter = 1000; printf(" exp(x) - 2 = 0\n"); printf(" x = %22.15e\n", regula(xs, xe, h, eps, iter)); return 1; } |
( 1.0 , 0.0) * x ^ 2 + (-3.0 , 0.0) * x + ( 2.0 , 0.0) = 0 x1 = ( 2.000000000000000e+00 , 0.000000000000000e+00 ) x2 = ( 1.000000000000000e+00 , 0.000000000000000e+00 ) ( 1.0 , 0.0) * x ^ 2 + (-2.0 , 0.0) * x + ( 1.0 , 0.0) = 0 x1 = ( 1.000000000000000e+00 , 0.000000000000000e+00 ) x2 = ( 1.000000000000000e+00 , 0.000000000000000e+00 ) ( 1.0 , 0.0) * x ^ 2 + (-4.0 , 0.0) * x + ( 8.0 , 0.0) = 0 x1 = ( 2.000000000000000e+00 , -2.000000000000000e+00 ) x2 = ( 2.000000000000000e+00 , 2.000000000000000e+00 ) 0.1 * x ^ 3 -0.5 * x ^ 2 0.8 * x ^ 1 1.4 * x ^ 0 x1 = ( -9.999999999998614e-01, 0.000000000000000e+00 ) x2 = ( 2.999999999999930e+00, 2.236067977499910e+00 ) x3 = ( 2.999999999999930e+00, -2.236067977499910e+00 ) 0.1 * x ^ 3 -0.5 * x ^ 2 0.8 * x ^ 1 1.4 * x ^ 0 x = -9.999999999999999e-01 1.0 * x ^ 5 -6.0 * x ^ 4 25.0 * x ^ 3 -2.0 * x ^ 2 2.0 * x ^ 1 0.0 * x ^ 0 x1 = ( 3.000000000000081e+00, 4.000000000000217e+00 ) x2 = ( 3.000000000000081e+00, -4.000000000000217e+00 ) x3 = ( 9.999999999977374e-01, 1.000000000001475e+00 ) x4 = ( 9.999999999977374e-01, -1.000000000001475e+00 ) x5 = ( 2.000000000004364e+00, 0.000000000000000e+00 ) exp(x) - 2 = 0 x = 6.931470233921545e-01 |
/* test11.c */ #include <stdio.h> #include "sslib.h" double _f(double x){ return x; } int main(void) { static double ar[4] = { 4., 3., 2., 1.}; static double ai[4] = { 4., 3., 2., 1.}; Complex a[4], x[4], y; double eps; int i, iter, n; eps = 1.e-6; n = 3; iter = 100; for(i = 0; i <= n; i++) { a[i].r = ar[i]; a[i].i = ai[i]; printf("( %4.1f , %4.1f ) * x ^ %d\n", ar[i], ai[i], n - i); } cnewton(a, x, n, eps, iter); for(i = 0; i < n; i++) { y = cfunc(a, n, x[i]); printf("func( x(%d) = { %14.7e , %14.7e } ) = { %10.3e, %10.3e }\n", i, x[i].r, x[i].i, y.r, y.i); } return 1; } |
( 4.0 , 4.0 ) * x ^ 3 ( 3.0 , 3.0 ) * x ^ 2 ( 2.0 , 2.0 ) * x ^ 1 ( 1.0 , 1.0 ) * x ^ 0 func( x(0) = { -7.2085207e-02 , 6.3832674e-01 } ) = { 6.558e-11, -1.992e-09 } func( x(1) = { -6.0582959e-01 , -3.3478094e-10 } ) = { 8.680e-10, -9.863e-10 } func( x(2) = { -7.2085207e-02 , -6.3832674e-01 } ) = { 2.018e-11, -1.854e-11 } |
/* test12.c */ #include <stdio.h> #include "sslib.h" double _f(double x){ return x; } int main(void) { int i, iter, n; Complex c[4], rt[4], w; static double ar[4] = { 2., 3., 8., -5.}; double x, eps; n = 3; eps = 1.e-6; iter = 100; for(i = 0; i <= n; i++) { c[i].r = ar[i]; c[i].i = 0.; } dka(c, rt, n, eps, iter); printf("* Coefficient\n"); for(i = 0; i <= n; i++) printf(" x ^ %2d : %5.1f\n", n - i, ar[i]); printf("* Result\n"); for(i = 1; i <= n; i++) { printf(" x(%d) = { %22.15e , %22.15e }\n", i, rt[i].r, rt[i].i); w = cfunc(c, n, rt[i]); printf(" error = { %12.5e , %12.5e }\n", w.r, w.i); } return 1; } |
* Coefficient x ^ 3 : 2.0 x ^ 2 : 3.0 x ^ 1 : 8.0 x ^ 0 : -5.0 * Result x(1) = { 4.999999999999999e-01 , 2.904184808559377e-18 } error = { -8.88178e-16 , 3.63023e-17 } x(2) = { -1.000000000000000e+00 , 2.000000000000000e+00 } error = { 0.00000e+00 , 0.00000e+00 } x(3) = { -1.000000000000000e+00 , -2.000000000000000e+00 } error = { 0.00000e+00 , 0.00000e+00 } |