修正シェパード(Shepard)法を用いた三次補間

C言語によるサンプルソースコード : 使用関数名:nag_3d_shep_interp (e01tgc)

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 修正シェパード(Shepard)法を用いた三次補間 (C言語/C++)

Keyword: 修正シェパード, Shepard, 三次補間

概要

本サンプルは修正シェパード(Shepard)法を用いた三次補間の生成を行うC言語によるサンプルプログラムです。 本サンプルは以下に示されるデータ点から補間値を求めて出力します。

三次補間のデータ 

※本サンプルはnAG Cライブラリに含まれる関数 nag_3d_shep_interp() のExampleコードです。本サンプル及び関数の詳細情報は nag_3d_shep_interp のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

入力データ

(本関数の詳細はnag_3d_shep_interp のマニュアルページを参照)
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

このデータをダウンロード
nag_3d_shep_interp (e01tgc) Example Program Data
 30                      M, the number of data points
 0.80  0.23  0.37  0.51  X, Y, Z, F data point definition
 0.23  0.88  0.05  1.80
 0.18  0.43  0.04  0.11
 0.58  0.95  0.62  2.65
 0.64  0.69  0.20  0.93
 0.88  0.35  0.49  0.72
 0.30  0.10  0.78 -0.11
 0.87  0.09  0.05  0.67
 0.04  0.02  0.40  0.00
 0.62  0.90  0.43  2.20
 0.87  0.96  0.24  3.17
 0.62  0.64  0.45  0.74
 0.86  0.13  0.47  0.64
 0.87  0.60  0.46  1.07
 0.49  0.43  0.13  0.22
 0.12  0.61  0.00  0.41
 0.02  0.71  0.82  0.58
 0.62  0.93  0.44  2.48
 0.49  0.54  0.04  0.37
 0.36  0.56  0.39  0.35
 0.62  0.42  0.97 -0.20
 0.01  0.72  0.45  0.78
 0.41  0.36  0.52  0.11
 0.17  0.99  0.65  2.82
 0.51  0.29  0.59  0.14
 0.85  0.05  0.04  0.61
 0.20  0.20  0.87 -0.25
 0.04  0.67  0.04  0.59
 0.31  0.63  0.18  0.50
 0.88  0.27  0.07  0.71  End of data points
 6                       N, the number of evaluation points
 0.10  0.10  0.10        U, V, W evaluation point definition
 0.20  0.20  0.20
 0.30  0.30  0.30
 0.40  0.40  0.40
 0.50  0.50  0.50
 0.60  0.60  0.60        End of evaluation points

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目にデータ点の数(m)を指定しています。
  • 3~32行目にはデータ点の座標x、y、zとデータ値(f)を指定しています。
  • 33行目には評価点の数(n)を指定しています。
  • 34~39行目に評価点(u,v,w)を指定しています。

出力結果

(本関数の詳細はnag_3d_shep_interp のマニュアルページを参照)
1
2
3
4
5
6
7
8
9

この出力例をダウンロード
nag_3d_shep_interp (e01tgc) Example Program Results

     i     u(i)      v(i)      w(i)      Q(i)
     0    0.1000    0.1000    0.1000    0.2630
     1    0.2000    0.2000    0.2000    0.1182
     2    0.3000    0.3000    0.3000    0.0811
     3    0.4000    0.4000    0.4000    0.1552
     4    0.5000    0.5000    0.5000    0.3019
     5    0.6000    0.6000    0.6000    0.5712

  • 3~9行目に評価点と補間値が出力されています。

ソースコード

(本関数の詳細はnag_3d_shep_interp のマニュアルページを参照)

※本サンプルソースコードは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

このソースコードをダウンロード
/* nag_3d_shep_interp (e01tgc) 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 <nage01.h>

int main(void)
{
  /* Scalars */
  Integer exit_status, i, m, n, nq, nw, liq, lrq;
  NagError fail;

  /* Arrays */
  double *f = 0, *q = 0, *qx = 0, *qy = 0, *qz = 0, *rq = 0,
         *u = 0, *v = 0, *w = 0, *x = 0, *y = 0, *z = 0;
  Integer *iq = 0;

  exit_status = 0;

  INIT_FAIL(fail);

  printf("nag_3d_shep_interp (e01tgc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Input the number of nodes. */
  scanf("%ld%*[^\n] ", &m);

  if (m > 0) {
    /* Allocate memory */
    lrq = 10 * m + 7;
    liq = 2 * m + 1;
    if (!(f = nAG_ALLOC(m, double)) ||
        !(x = nAG_ALLOC(m, double)) ||
        !(y = nAG_ALLOC(m, double)) ||
        !(z = nAG_ALLOC(m, double)) ||
        !(rq = nAG_ALLOC(lrq, double)) || !(iq = nAG_ALLOC(liq, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    /* Input the data points X,Y,Z and F. */
    for (i = 0; i < m; ++i)
      scanf("%lf%lf%lf%lf%*[^\n] ", &x[i], &y[i], &z[i], &f[i]);

    /* Generate the interpolant. */
    nq = 0;
    nw = 0;

    /* nag_3d_shep_interp (e01tgc).
     * Interpolating functions, modified Shepard's method, three
     * variables
     */
    nag_3d_shep_interp(m, x, y, z, f, nw, nq, iq, rq, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_3d_shep_interp (e01tgc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

    /* Input the number of evaluation points. */
    scanf("%ld%*[^\n] ", &n);

    /* Allocate memory for nag_3d_shep_eval (e01thc) */
    if (!(q = nAG_ALLOC(n, double)) ||
        !(qx = nAG_ALLOC(n, double)) ||
        !(qy = nAG_ALLOC(n, double)) ||
        !(qz = nAG_ALLOC(n, double)) ||
        !(u = nAG_ALLOC(n, double)) ||
        !(v = nAG_ALLOC(n, double)) || !(w = nAG_ALLOC(n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    /* Input the evaluation points. */
    for (i = 0; i < n; ++i)
      scanf("%lf%lf%lf%*[^\n] ", &u[i], &v[i], &w[i]);

    /* Evaluate the interpolant using nag_3d_shep_eval (e01thc). */
    fail.print = Nag_TRUE;
    /* nag_3d_shep_eval (e01thc).
     * Interpolated values, evaluate interpolant computed by
     * nag_3d_shep_interp (e01tgc), function and first
     * derivatives, three variables
     */
    nag_3d_shep_eval(m, x, y, z, f, iq, rq, n, u, v, w, q, qx, qy, qz, &fail);

    printf("\n");
    printf("     i     u(i)      v(i)      w(i)      Q(i)\n");
    for (i = 0; i < n; ++i)
      printf("%6ld%10.4f%10.4f%10.4f%10.4f\n", i, u[i], v[i], w[i],
             q[i]);
  }

END:
  nAG_FREE(f);
  nAG_FREE(q);
  nAG_FREE(qx);
  nAG_FREE(qy);
  nAG_FREE(qz);
  nAG_FREE(rq);
  nAG_FREE(u);
  nAG_FREE(v);
  nAG_FREE(w);
  nAG_FREE(x);
  nAG_FREE(y);
  nAG_FREE(z);
  nAG_FREE(iq);

  return exit_status;
}


関連情報
© 日本ニューメリカルアルゴリズムズグループ株式会社 2025
Privacy Policy  /  Trademarks