MENU

テイラー展開② - 三角関数

cos{x}テイラー展開してみる

cos {x} テイラー展開すると以下のようになります。

{ \displaystyle
cos{x} = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + ...
}

x の値は 0 ~ 2\pi の範囲に収まるように計算します。

プログラム

一例(C++)を示します。

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

double tailercos(double);

int main()
{
    double x,rd=3.14159/180;

    cout << setw(5) << "x" << setw(14) << "tailercos(x)"  <<  setw(14) << "exp(x)" << endl;
    
    for (x=0;x<=180;x=x+10){
        cout << setw(5) << x << setw(14) << tailercos(x) << setw(14) << exp(x) << endl;
    }
    
    return 0;
}
double tailercos(double x){
    double EPS=1e-09;
    double s=1.0,e=1.0,d=1.0;
    int k;

    x = fmod(x,2*3.14159265358979);
    for(k=1;k<=200;k=k+2){
        d=s;
        e=-e*x*x/(k*(k+1));
        s=s+e;
        if(fabs(s-d)<EPS*fabs(d))
            return (s);
    }
    return (9999.0);
}

実行結果

    x  tailercos(x)        cos(x)
  0.0             1             1
 10.0      0.984808      0.984808
 20.0      0.939693      0.939693
 30.0      0.866026      0.866026
 40.0      0.766045      0.766045
 50.0      0.642788      0.642788
 60.0      0.500001      0.500001
 70.0      0.342021      0.342021
 80.0      0.173649      0.173649
 90.0  1.32679e-006  1.32679e-006
100.0     -0.173647     -0.173647
110.0     -0.342019     -0.342019
120.0     -0.499998     -0.499998
130.0     -0.642786     -0.642786
140.0     -0.766043     -0.766043
150.0     -0.866024     -0.866024
160.0     -0.939692     -0.939692
170.0     -0.984807     -0.984807
180.0            -1            -1