..................の間がプログラム本体です program 10 .................. #include #define NSIZ 4 /* 行列演算の応用 Gauss の消去法(ピボット選択なし) a x = b の解 x を求める */ main() { double a[NSIZ][NSIZ],b[NSIZ],x[NSIZ]; double awork[NSIZ][NSIZ],bwork[NSIZ],check[NSIZ]; double dd,xx; int i,j,k; /* ファイルから行列aに値を読み込む */ for( i=0; i=0; --i){ xx=0; for( j=i+1; j #include #define NMAX 30 /* 関数サブプログラムの利用 2分法による非線形方程式の解 */ double func(double); main() { double x1,x2,xtemp; int irepeat; /* 解探索の範囲を指定 */ scanf("%lf%lf",&x1,&x2); /* 2分法を最大NMAX回くりかえす */ for(irepeat=1; irepeat<=NMAX; ++irepeat){ /* x1とx2の差が与えた精度以内なら終了 */ if( fabs((x1-x2)/x1) <= 1e-10){ printf("solution is between %17.12f and %17.12f\n",x1,x2); return; } /* func(x1)とfunc(x2)の符号が逆なら範囲を二分する */ else if(func(x1)*func(x2) < 0){ xtemp = (x1+x2)/2; if(func(x1)*func(xtemp) < 0){ x2=xtemp; } else{ x1=xtemp; } printf("%d step x1= %17.12f x2= %17.12f\n",irepeat,x1,x2); } /* func(x1)とfunc(x2)が同符号なら、この範囲に解がないと判断して終了 */ else{ printf("no solution is found between %17.12f and %17.12f\n",x1,x2); return; } } /* NMAX回のくりかえしで解に収束しなかったときは終了 */ printf("not converged after %d steps\n",NMAX); printf("current values of x are %17.12f and %17.12f\n",x1,x2); } /* 解を得たい関数 */ double func(double x) { return pow(x,3) + 3*x*x + 5*x + 1; }