3次スプラインと導関数の評価

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

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 3次スプラインと導関数の評価 (C言語/C++)

Keyword: 3次, スプライン, 導関数

概要

本サンプルは3次スプラインと導関数の評価を行うC言語によるサンプルプログラムです。 本サンプルは以下に示されるデータについてスプライン曲線フィットを行い、スプラインの値と導関数を求めて出力します。

3次スプラインのデータ 

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

入力データ

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

このデータをダウンロード
nag_1d_spline_deriv (e02bcc) Example Program Data
   7   7
  0.0      0.0      0.0      0.0      1.0      3.0      3.0      3.0
  4.0      4.0      6.0      6.0      6.0      6.0
 10.0     12.0     13.0     15.0     22.0     26.0     24.0     18.0
 14.0     12.0
  0.0
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目にスプライン区間の数(ncap)、xの数(m)を指定しています。
  • 3~4行目にノットの値(lamda)を指定しています。
  • 5~6行目にBスプラインの係数(c)を指定しています。
  • 7~13行目に3次スプラインと導関数が評価される引数xを指定しています。

出力結果

(本関数の詳細はnag_1d_spline_deriv のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

この出力例をダウンロード
nag_1d_spline_deriv (e02bcc) Example Program Results
        x             Spline    1st deriv  2nd deriv   3rd deriv

     0.0000   Left    10.0000     6.0000   -10.0000    10.6667
     0.0000  Right    10.0000     6.0000   -10.0000    10.6667

     1.0000   Left    12.7778     1.3333     0.6667    10.6667
     1.0000  Right    12.7778     1.3333     0.6667     3.9167

     2.0000   Left    15.0972     3.9583     4.5833     3.9167
     2.0000  Right    15.0972     3.9583     4.5833     3.9167

     3.0000   Left    22.0000    10.5000     8.5000     3.9167
     3.0000  Right    22.0000    12.0000   -36.0000    36.0000

     4.0000   Left    22.0000    -6.0000     0.0000    36.0000
     4.0000  Right    22.0000    -6.0000     0.0000     1.5000

     5.0000   Left    16.2500    -5.2500     1.5000     1.5000
     5.0000  Right    16.2500    -5.2500     1.5000     1.5000

     6.0000   Left    12.0000    -3.0000     3.0000     1.5000
     6.0000  Right    12.0000    -3.0000     3.0000     1.5000

  • 2~23行目にxの値、左辺値か右辺値か、スプライン、1階導関数、2階導関数、3階導関数が出力されています。

ソースコード

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

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

このソースコードをダウンロード
/* nag_1d_spline_deriv (e02bcc) Example Program.
 *
 * CLL6I261D/CLL6I261DL Version.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 *
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nage02.h>

int main(void)
{
  Integer exit_status = 0, i, j, l, m, ncap, ncap7;
  NagError fail;
  Nag_DerivType derivs;
  Nag_Spline spline;
  double s[4], x;

  INIT_FAIL(fail);

  /* Initialize spline */
  spline.lamda = 0;
  spline.c = 0;

  printf("nag_1d_spline_deriv (e02bcc) Example Program Results\n");
  scanf("%*[^\n]"); /* Skip heading in data file */
  while (scanf("%ld%ld", &ncap, &m) != EOF)
  {
    if (m <= 0) {
      printf("Invalid m.\n");
      exit_status = 1;
      return exit_status;
    }
    if (ncap > 0) {
      ncap7 = ncap + 7;
      spline.n = ncap7;
      if (!(spline.c = nAG_ALLOC(ncap7, double)) ||
          !(spline.lamda = nAG_ALLOC(ncap7, double)))
      {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }
    }
    else {
      printf("Invalid ncap.\n");
      exit_status = 1;
      return exit_status;
    }
    for (j = 0; j < ncap7; j++)
      scanf("%lf", &(spline.lamda[j]));
    for (j = 0; j < ncap + 3; j++)
      scanf("%lf", &(spline.c[j]));
    printf("        x             Spline    1st deriv  "
           "2nd deriv   3rd deriv");
    for (i = 1; i <= m; i++) {
      scanf("%lf", &x);
      derivs = Nag_LeftDerivs;
      for (j = 1; j <= 2; j++) {
        /* nag_1d_spline_deriv (e02bcc).
         * Evaluation of fitted cubic spline, function and
         * derivatives
         */
        nag_1d_spline_deriv(derivs, x, s, &spline, &fail);
        if (fail.code != NE_NOERROR) {
          printf("Error from nag_1d_spline_deriv (e02bcc).\n%s\n",
                 fail.message);
          exit_status = 1;
          goto END;
        }

        if (derivs == Nag_LeftDerivs) {
          printf("\n\n%11.4f   Left", x);
          for (l = 0; l < 4; l++)
            printf("%11.4f", s[l]);
        }
        else {
          printf("\n%11.4f  Right", x);
          for (l = 0; l < 4; l++)
            printf("%11.4f", s[l]);
        }
        derivs = Nag_RightDerivs;
      }
    }
    printf("\n");
  END:
    nAG_FREE(spline.c);
    nAG_FREE(spline.lamda);
  }
  return exit_status;
}


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