a = 1 b = 2 printf ("%12.10f\n" , bisection(a, b)) def bisection(a, b) { while (true) { // 区間 (a, b) の中点 c = (a + b) / 2 c = (a + b) / 2 printf("%12.10f\t%13.10f\n", c, c - Math.sqrt(2)) fc = f(c) if (Math.abs(fc) < 0.0000000001) break if (fc < 0) { // f(c) < 0 であれば, 解は区間 (c, b) の中に存在 a = c } else { // f(c) > 0 であれば, 解は区間 (a, c) の中に存在 b = c } } c } def f(x) { x * x - 2 } a = 1 b = 2 printf ("%12.10f\n" , falseposition(a, b)) def falseposition(a, b) { while (true) { // 点 (a,f(a)) と 点 (b,f(b)) を結ぶ直線と x軸の交点 c = (a * f(b) - b * f(a)) / (f(b) - f(a)) printf("%12.10f\t%13.10f\n", c, c - Math.sqrt(2)) fc = f(c) if (Math.abs(fc) < 0.0000000001) break if (fc < 0) { // f(c) < 0 であれば, 解は区間 (c, b) の中に存在 a = c } else { // f(c) > 0 であれば, 解は区間 (a, c) の中に存在 b = c } } c } def f(x) { x * x - 2 } x = 1 printf ("%12.10f\n" , iterative(x)) def iterative(x0) { while (true) { x1 = g(x0) printf("%12.10f\t%13.10f\n", x1, x1 - Math.sqrt(2)) if (Math.abs(x1 - x0) < 0.0000000001) break x0 = x1 } x1 } def g(x) { (x / 2) + (1 / x) } x = 2 printf ("%12.10f\n" , newton(x)) def newton(x0) { while (true) { x1 = x0 - (f0(x0) / f1(x0)) printf("%12.10f\t%13.10f\n", x1, x1 - Math.sqrt(2)) if (Math.abs(x1 - x0) < 0.0000000001) break x0 = x1 } x1 } def f0(x) { x * x - 2 } def f1(x) { 2 * x } x = 2 printf ("%12.10f\n" , bailey(x)) def bailey(x0) { while (true) { x1 = x0 - (f0(x0) / (f1(x0) - (f0(x0) * f2(x0) / (2 * f1(x0))))) printf("%12.10f\t%13.10f\n", x1, x1 - Math.sqrt(2)) if (Math.abs(x1 - x0) < 0.0000000001) break x0 = x1 } x1 } def f0(x) { x * x - 2 } def f1(x) { 2 * x } def f2(x) { 2 } x0 = 1 x1 = 2 printf ("%12.10f\n" , secant(x0, x1)) def secant(x0, x1) { while (true) { x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0)) printf("%12.10f\t%13.10f\n", x2, x2 - Math.sqrt(2)) if (Math.abs(x2 - x1) < 0.0000000001) break x0 = x1 x1 = x2 } x2 } def f(x) { x * x - 2 } N = 3 def a = [[ 9.0, 2.0, 1.0, 1.0], [2.0, 8.0, -2.0, 1.0], [-1.0, -2.0, 7.0, -2.0], [1.0, -1.0, -2.0, 6.0]] as double[][] def b = [20.0, 16.0, 8.0, 17.0] as double[] def c = [ 0.0, 0.0, 0.0, 0.0] as double[] // ヤコビの反復法 jacobi(a,b,c) println("解") for (i in 0..N ) { printf ("%14.10f\t" , c[i]) } println() // ヤコビの反復法 def jacobi(a, b, x0) { def x1 = [ 0.0, 0.0, 0.0, 0.0] as double[] while (true) { def finish = true for (i in 0..N ) { x1[i] = 0 for (j in 0..N ) { if (j != i) x1[i] += a[i][j] * x0[j] } x1[i] = (b[i] - x1[i]) / a[i][i] if (Math.abs(x1[i] - x0[i]) > 0.0000000001) finish = false } for (i in 0..N ) { x0[i] = x1[i] printf ("%14.10f\t" , x0[i]) } println() if (finish) return } } |