dポイントプレゼントキャンペーン実施中!

f(x)=sin(x)のx=0におけるテイラー展開を求めるプログラムをしたいんですけど、数値が合いません、どこを直せばよいでしょうか。
#include <stdio.h>
#include <math.h>

#define EPS 0.00000001
#define NMAX 100
#define DELTA 0.1

double an(int n)
{
double factorical = 1.0;

for (int i = 1; i <= n; i++)
factorical *= (double)(2*i-1);

return 1/factorical;
}

void sin_approx(double x)
{
double sn = 0.0;
int N = 1;

for (N=1; N < NMAX;N++ )
{
double tn = pow((-1), (double)N - 1)*pow(x, (double)2 * N - 1)*an(N);
sn += tn;

if (fabs(tn) < EPS)
break;
}

printf("%.1f\t%4.8f\t%d\t%4.8f\n", x, sn, N, sin(x));
}

int main(void)
{
printf("x=0におけるテイラー展開f(x)=sin x,区間[-5.0,5.0]\n");

double min = -5.0;
double max = 5.0;
double x = min;

printf("x\tTaylor\t\tN\tsin(x)\n");
while (x <= max)
{
sin_approx(x);
x += DELTA;
}

return 0;
}

A 回答 (2件)

関数 double an(int n) は、具体的に、何を返す関数なのですか?


1/n! (nの階乗分の1)
1/(2n-1)! ((2n-1)の階乗分の1 → N項の分母部分)

どちらにしても「階乗」を求めるプログラムになっていません。
    • good
    • 0

an

    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!