ホームページ  >  記事  >  バックエンド開発  >  cos(x) 系列を求める C プログラム

cos(x) 系列を求める C プログラム

PHPz
PHPz転載
2023-08-30 12:17:05756ブラウズ

x と n の値を与えます。x は cos の角度、n は cos(x) の系列の項の数です。

Cos(x)の場合 h3>

Cos(x)は、x角度の値を計算するために使用される三角関数です。

cos(x) 系列を求める C プログラム

$$\cos (x) = \displaystyle\sum\limits_{k=0} ^\infty \frac{(-1 )^{k}}{(2k!)}x^{2k}$$

Cos(x) 系列の場合

Cos( x) = 1 – (x*2 / 2 !) (x*4 / 4!) – (x*6 / 6!) (x*8 / 8!)……

Input-: x = 10, n = 3
Output-: 0.984804
Input-: x = 8, n = 2
Output-: 0.990266

次のプログラムの場合使用する方法は次のとおりです -

  • xとnの値を入力します
  • cos(x)級数の計算式を適用します
  • 結果をすべてのレベルとして出力します。数値の合計

アルゴリズム

Start
Step 1 Declare and initialize const double PI = 3.142
Step 2 In function double series_sum(double x, int n)
   Set x = x * (PI / 180.0)
Set result = 1
Set s = 1, fact = 1, pow = 1
    Loop For i = 1 and i < 5 and i++
        Set s = s * -1
        Set fact = fact * (2 * i - 1) * (2 * i)
        Set pow = pow * x * x
        Set result = result + s * pow / fact
     End Loop
    Return result
Step 3 In function int main() s
    Declare and set x = 10
Declare and set n = 3
    Print series_sum(x, n)  
Stop

#include <stdio.h>
const double PI = 3.142;
//will return the sum of cos(x)
double series_sum(double x, int n) {
    x = x * (PI / 180.0);
    double result = 1;
    double s = 1, fact = 1, pow = 1;
    for (int i = 1; i < 5; i++) {
        s = s * -1;
        fact = fact * (2 * i - 1) * (2 * i);
        pow = pow * x * x;
        result = result + s * pow / fact;
    }
    return result;
}
//main function
int main() {
    float x = 10;
    int n = 3;
    printf("%lf</p><p>", series_sum(x, n));  
    return 0;
}

出力

X=10; n=30.984804
X=13; n=80.974363
X=8; n=2 0.990266

以上がcos(x) 系列を求める C プログラムの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。