#include #include #include /*********** 表2.1のスプライン補間の誤差を出力するプログラム ************** ・実行すると,スプライン補間の節点の数N+1に対応する N(textのNに対応)の入力を求めます. ・コマンドラインの第一引数にNを与えることでも 即時に誤差を出力することができます. 例) >spline 12 ・L_2ノルムの2乗の計算には積分の台形公式(式(3.4))を用います. **************************************************************************/ #define nn 100 //小区間中のきざみ数(L2nの精度に関わる) double f(double x){ double y; y=1./(1.+25.*pow(x,2)); return y; } int main(int argc, char **argv){ int i, j, jmax, N; double h, dx, temp, x, p, L2n; double *a, *b, *c; double *xi, *fi, *Y, *C; /********* 入力処理(始) ***********/ if(!(argc==1 || argc==2)){ printf("引数のエラーです\n"); return 1; } if(argc == 1){ printf("スプライン補間の節点の数N+1に対応する Nを入力してください.\n N = "); scanf("%d",&N); }else{ argv++; N = atoi(*argv); } /********* 入力処理(終) **********/ /*** 右側コメント文と同等のメモリ領域を確保 ***/ /*** a:α b:β c:γ ,求める連立一次方程式のXiはC[2][] ***/ a = malloc(sizeof(double)*(N+2)); //a[N+2] b = malloc(sizeof(double)*(N+2)); //b[N+2] c = malloc(sizeof(double)*(N+1)); //c[N+1] xi = malloc(sizeof(double)*(N+2)); //xi[N+2] fi = malloc(sizeof(double)*(N+2)); //fi[N+2] Y = malloc(sizeof(double)*(N+2)); //Y[N+2] C = malloc(sizeof(double)*5*(N+2)); //C[5][N+2] if(xi==NULL || fi==NULL || a==NULL || b==NULL || c==NULL || Y==NULL || C==NULL){ printf("can't malloc!!\n"); return 2; } dx = 2./N; //小区間の幅(節点間の幅) dx h = dx/nn; //小区間のきざみ幅 h(台形公式のh) for(i=0; ispline 12 > data.csv などとコマンドライン上でファイルにリダイレクトしてください. ***********************************************************/ // printf("%lf, %11.3e\n", x, p); } } printf("L2n=%11.3e\n",L2n); /***** メモリ領域の開放 *****/ free(xi);free(fi);free(a);free(b);free(c);free(Y);free(C); return 0; }