Keyword: 最小二乗, チェビシェフ級数, フィット
概要
本サンプルは最小二乗チェビシェフ級数曲面フィットを行うC言語によるサンプルプログラムです。 本サンプルは以下に示されるデータについてフィットされた値とチェビシェフ係数を求めて出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_2d_cheb_fit_lines() のExampleコードです。本サンプル及び関数の詳細情報は nag_2d_cheb_fit_lines のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_2d_cheb_fit_lines のマニュアルページを参照)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
このデータをダウンロード |
nag_2d_cheb_fit_lines (e02cac) Example Program Data 4 3 2 0.0 8 0.0 5.0 1.0 7 0.1 4.5 2.0 7 0.4 4.0 4.0 6 1.6 3.5 0.1 1.01005 1.0 1.0 1.10517 1.0 1.6 1.17351 1.0 2.1 1.23368 1.0 3.3 1.39097 1.0 3.9 1.47698 1.0 4.2 1.52196 1.0 4.9 1.63232 1.0 0.1 2.02010 1.0 1.1 2.23256 1.0 1.9 2.41850 1.0 2.7 2.61993 1.0 3.2 2.75426 1.0 4.1 3.01364 1.0 4.5 3.13662 1.0 0.5 3.15381 1.0 1.1 3.34883 1.0 1.3 3.41649 1.0 2.2 3.73823 1.0 2.9 4.00928 1.0 3.5 4.25720 1.0 3.9 4.43094 1.0 1.7 5.92652 1.0 2.0 6.10701 1.0 2.4 6.35625 1.0 2.7 6.54982 1.0 3.1 6.81713 1.0 3.5 7.09534 1.0
- 1行目はタイトル行で読み飛ばされます。
- 2行目にデータが与えられる Y=Y(I)の行の数(n)、x方向のフィットの次数(k)、y方向のフィットの次数(l)を指定しています。
- 3~6行目にはYの値、データ点の数(mi)、xの範囲の下限(xmin)、上限(xmax)を指定しています。
- 7~34行目にはデータ点のx値、従属変数のデータ値(f)、データ点に割り当てられた重み(w)を指定しています。
出力結果
(本関数の詳細はnag_2d_cheb_fit_lines のマニュアルページを参照)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
この出力例をダウンロード |
nag_2d_cheb_fit_lines (e02cac) Example Program Results Data Y Data X Data F Fitted F Residual 0.0000 0.1000 1.0100 1.0175 7.40e-03 0.0000 1.0000 1.1052 1.1126 7.39e-03 0.0000 1.6000 1.1735 1.1809 7.43e-03 0.0000 2.1000 1.2337 1.2412 7.55e-03 0.0000 3.3000 1.3910 1.3992 8.19e-03 0.0000 3.9000 1.4770 1.4857 8.72e-03 0.0000 4.2000 1.5220 1.5310 9.03e-03 0.0000 4.9000 1.6323 1.6422 9.83e-03 1.0000 0.1000 2.0201 1.9987 -2.14e-02 1.0000 1.1000 2.2326 2.2110 -2.16e-02 1.0000 1.9000 2.4185 2.3962 -2.23e-02 1.0000 2.7000 2.6199 2.5966 -2.34e-02 1.0000 3.2000 2.7543 2.7299 -2.43e-02 1.0000 4.1000 3.0136 2.9869 -2.68e-02 1.0000 4.5000 3.1366 3.1084 -2.82e-02 2.0000 0.5000 3.1538 3.1700 1.62e-02 2.0000 1.1000 3.3488 3.3648 1.60e-02 2.0000 1.3000 3.4165 3.4325 1.60e-02 2.0000 2.2000 3.7382 3.7549 1.66e-02 2.0000 2.9000 4.0093 4.0272 1.79e-02 2.0000 3.5000 4.2572 4.2769 1.97e-02 2.0000 3.9000 4.4309 4.4521 2.12e-02 4.0000 1.7000 5.9265 5.9231 -3.42e-03 4.0000 2.0000 6.1070 6.1036 -3.41e-03 4.0000 2.4000 6.3563 6.3527 -3.50e-03 4.0000 2.7000 6.5498 6.5462 -3.64e-03 4.0000 3.1000 6.8171 6.8132 -3.98e-03 4.0000 3.5000 7.0953 7.0909 -4.49e-03 Chebyshev coefficients of the fit 15.3482 5.1507 0.1014 1.1472 0.1442 -0.1046 0.0490 -0.0031 -0.0070 0.0015 -0.0003 -0.0002
- 3~25行目にy値、x値、従属変数のデータ値、フィットされたデータ値及び残差が出力されています。
- 37~42行目にチェビシェフ係数が出力されています。
ソースコード
(本関数の詳細はnag_2d_cheb_fit_lines のマニュアルページを参照)
※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
このソースコードをダウンロード |
/* nag_2d_cheb_fit_lines (e02cac) Example Program. * * CLL6I261D/CLL6I261DL Version. * * Copyright 2017 Numerical Algorithms Group. * * Mark 26.1, 2017. */ #include <stdio.h> #include <nag.h> #include <nag_stdlib.h> #include <nage02.h> int main(void) { /* Scalars */ double ymax; Integer exit_status, i, j, k, l, mi, mj, n, r, t, na, one; NagError fail; /* Arrays */ double *a = 0, *f = 0, *ff = 0, *w = 0, *x = 0, *xmax = 0, *xmin = 0, *y = 0; Integer *m = 0; INIT_FAIL(fail); exit_status = 0; printf("nag_2d_cheb_fit_lines (e02cac) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n] "); /* Input the number of lines Y = Y(I) on which data is given, */ /* and the required degree of fit in the X and Y directions */ while (scanf("%ld%ld%ld%*[^\n] ", &n, &k, &l) != EOF) { printf("\n"); if (n > 0) { /* Allocate arrays m, y, xmin and xmax */ if (!(m = nAG_ALLOC(n, Integer)) || !(y = nAG_ALLOC(n, double)) || !(xmin = nAG_ALLOC(n, double)) || !(xmax = nAG_ALLOC(n, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } mj = 0; /* Input Y(I), the number of data points on Y = Y(I) and the */ /* range of X-values on this line, for I = 1,2,...N */ for (i = 0; i < n; ++i) { scanf("%lf%ld%lf%lf%*[^\n] ", &y[i], &mi, &xmin[i], &xmax[i]); m[i] = mi; mj += mi; } /* Allocate arrays x, f, ff, w and a */ na = (k + 1) * (l + 1); if (!(x = nAG_ALLOC(mj, double)) || !(f = nAG_ALLOC(mj, double)) || !(ff = nAG_ALLOC(mj, double)) || !(w = nAG_ALLOC(mj, double)) || !(a = nAG_ALLOC(na, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Input the X-values and function values, F, together with */ /* their weights, W. */ for (i = 0; i < mj; ++i) scanf("%lf%lf%lf", &x[i], &f[i], &w[i]); scanf("%*[^\n] "); /* Evaluate the coefficients, A, of the fit to this set of data */ one = 1; /* nag_2d_cheb_fit_lines (e02cac). * Least squares surface fit by polynomials, data on lines */ nag_2d_cheb_fit_lines(m, n, k, l, x, y, f, w, a, xmin, xmax, y, one, y, one, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_2d_cheb_fit_lines (e02cac).\n%s\n", fail.message); exit_status = 1; goto END; } printf(" Data Y Data X Data F Fitted F Residual\n"); printf("\n"); mi = 0; for (r = 1; r <= n; ++r) { t = mi + 1; mi += m[r - 1]; ymax = y[n - 1]; if (n == 1) ymax += 1.0; /* Evaluate the fitted polynomial at each of the data points */ /* on the line Y = Y(R) */ /* nag_2d_cheb_eval (e02cbc). * Evaluation of fitted polynomial in two variables */ nag_2d_cheb_eval(t, mi, k, l, x, xmin[r - 1], xmax[r - 1], y[r - 1], y[0], ymax, ff, a, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_2d_cheb_eval (e02cbc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Output the data and fitted values on the line Y = Y(R) */ for (i = t - 1; i < mi; ++i) { printf("%11.4f%11.4f%11.4f%11.4f", y[r - 1], x[i], f[i], ff[i]); printf("%11.2e\n", ff[i] - f[i]); } printf("\n"); } /* Output the Chebyshev coefficients of the fit */ printf("Chebyshev coefficients of the fit\n"); printf("\n"); for (j = 1; j <= k + 1; ++j) { for (i = (j - 1) * (l + 1); i < j * (l + 1); ++i) printf("%11.4f ", a[i]); printf("\n"); } } } END: nAG_FREE(a); nAG_FREE(f); nAG_FREE(ff); nAG_FREE(w); nAG_FREE(x); nAG_FREE(xmax); nAG_FREE(xmin); nAG_FREE(y); nAG_FREE(m); return exit_status; }