HOME > natural science Laboratory > コンピュータ・シミュレーション講座 > TIPS 集

ステップ関数のフーリエ級数を試してみる(C言語)

文責:遠藤 理平 (2009年9月 2日) カテゴリ:TIPS 集(107)

ステップ関数のフーリエ級数を試してみる。

ステップ関数

ステップ関数のフーリエ級数

計算結果

級数の項数を変えて図示する

級数の項数が10000の場合(計算時間20秒)

ソース(ステップ関数のフーリエ級数)

/*
フーリエ級数(ステップ関数)
*/
#include <math.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

double PI = acos(-1.0);
double e = 2.7182818284590452354;

double a = 1.0;
double L = 1.0;
double l = 0.5;
double x_max =4.0;

int main (void){

  for(int m_max = 2; m_max<=32; m_max *= 2){ // 級数の項数を指定
    ostringstream fname1;
    fname1 << "step-" << m_max  << ".data"; //出力ファイル名
    string fname1_s = fname1.str();
    ofstream fout;
    fout.open(fname1_s.c_str());

    for(double x=0.0; x<=x_max ; x+=0.001){ //実空間の刻み幅
      double sum =0.0;
      for(int m=1 ; m<=m_max ; m++ ){
        sum += ( sin(PI*double(m)*l/L) * cos(PI*double(m)*(2.0*x-l)/L) )/double(m) ;
      }
      double f = a * 0.5 - 2.0 *a * sum /PI;
      fout << x << " " << f << endl;
    }
    cout << m_max << endl;
  }

  return 0;
}


▲このページのトップNPO法人 natural science トップ

▲このページのトップNPO法人 natural science トップ




Warning: mysqli_connect(): (28000/1045): Access denied for user 'xsvx1015071_ri'@'sv102.xserver.jp' (using password: YES) in /home/xsvx1015071/include/natural-science/include_counter-d.php on line 8
MySQL DBとの接続に失敗しました