多項式補間のチェビシェフ級数表現

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

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 多項式補間のチェビシェフ級数表現 (C言語/C++)

Keyword: 多項式補間, チェビシェフ

概要

本サンプルは多項式補間のチェビシェフ級数表現の構築を行うC言語によるサンプルプログラムです。 本サンプルは以下に示されるデータについて補間を構築しチェビシェフ級数表現の係数を出力します。

多項式補間のデータ 

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

入力データ

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

このデータをダウンロード
nag_1d_cheb_interp (e01aec) Example Program Data
    4       2.0       6.0
    0       2.0       1.0
    1       4.0       2.0      -1.0
    0       5.0       1.0
    2       6.0       2.0       4.0      -2.0

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に独立変数の数(m)、xの下限(xmin)、xの上限(xmax)を指定しています。
  • 3〜6行目に最高階導関数の階数(p)、xの値、yの値を指定しています。

出力結果

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

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

Total number of interpolating conditions = 7

Interpolating polynomial

   i    Chebyshev Coefficient a(i+1)
   0              9.1250
   1             -4.5781
   2              0.4609
   3              2.8516
   4             -2.8125
   5              2.2266
   6             -0.7109

  x    R   Rth derivative    Residual
 2.0   0         1.0         0.000000
 4.0   0         2.0         0.000000
       1        -1.0         0.000000
 5.0   0         1.0         0.000000
 6.0   0         2.0        -0.000000
       1         4.0        -0.000000
       2        -2.0         0.000000

  • 3行目に補間条件の合計数が出力されています。
  • 7〜14行目にチェビシェフ係数が出力されています。
  • 16〜23行目にxの値、導関数の階数、導関数と残差が出力されています。

ソースコード

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

※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法


このソースコードをダウンロード
/* nag_1d_cheb_interp (e01aec) 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 */
  double xmax, xmin;
  Integer exit_status, i, pmax, ires, iy, j, k, m, n, itmin, itmax, num_iter;
  NagError fail;

  /* Arrays */
  double *a = 0, *perf = 0, *x = 0, *y = 0;
  Integer *p = 0;

  exit_status = 0;

  INIT_FAIL(fail);

  printf("nag_1d_cheb_interp (e01aec) Example Program Results\n");

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

  while (scanf("%ld%lf%lf%*[^\n] ", &m, &xmin, &xmax) != EOF)
  {
    if (m > 0) {
      /* Allocate memory for p and x. */
      if (!(p = nAG_ALLOC(m, Integer)) || !(x = nAG_ALLOC(m, double)))
      {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }

      /* Read p, x and y arrays */
      n = 0;
      pmax = 0;
      for (i = 1; i <= m; ++i) {
        scanf("%ld%lf", &p[i - 1], &x[i - 1]);
        k = n + p[i - 1] + 1;
        /* We need to extend array y as we go along */
        if (!(y = nAG_REALLOC(y, k, double)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
        for (j = n + 1; j <= k; ++j)
          scanf("%lf", &y[j - 1]);
        scanf("%*[^\n] ");
        if (p[i - 1] > pmax)
          pmax = p[i - 1];
        n = k;
      }

      /* Allocate array a */
      if (!(a = nAG_ALLOC(n, double)) ||
          !(perf = nAG_ALLOC(pmax + n + 1, double)))
      {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }

      itmin = -1;
      itmax = -1;
      /* nag_1d_cheb_interp (e01aec).
       * Interpolating functions, polynomial interpolant, data may
       * include derivative values, one variable
       */
      nag_1d_cheb_interp(m, xmin, xmax, x, y, p, itmin, itmax, a, perf,
                         &num_iter, &fail);

      printf("\n");
      if (fail.code == NE_NOERROR) {
        printf("Total number of interpolating conditions ="
               " %ld\n", n);
        printf("\n");
        printf("Interpolating polynomial\n");
        printf("\n");
        printf("   i    Chebyshev Coefficient a(i+1)\n");

        for (i = 1; i <= n; ++i)
          printf("%4ld%20.4f\n", i - 1, a[i - 1]);

        printf("\n");

        printf("  x    R   Rth derivative    Residual\n");
        iy = 0;
        ires = pmax + 1;
        for (i = 1; i <= m; ++i) {
          for (j = 1; j <= p[i - 1] + 1; ++j) {
            ++iy;
            ++ires;
            if (j - 1 != 0)
              printf("    %4ld%12.1f%17.6f\n",
                     j - 1, y[iy - 1], perf[ires - 1]);
            else
              printf("%4.1f   0%12.1f%17.6f\n",
                     x[i - 1], y[iy - 1], perf[ires - 1]);
          }
        }
      }
      else {
        printf("Error from nag_1d_cheb_interp (e01aec).\n%s\n", fail.message);
        exit_status = 1;
      }
    }
  }

END:
  nAG_FREE(a);
  nAG_FREE(x);
  nAG_FREE(y);
  nAG_FREE(p);
  nAG_FREE(perf);

  return exit_status;
}


関連情報
Privacy Policy  /  Trademarks