www.tirinhas.com

terça-feira, 21 de abril de 2009

Sequências Polinomiais

Abaixo estão algumas séries, utilizadas para determinar funções trigonométricas.










A seguir, implementei algumas dessas funções matemáticas em C++.

Cálculo de x elevado a n
double pot( double x, unsigned int n)
{
double p = 1; unsigned int i;
for (i = 1; i<=n; i++)
p = p*x;
return p;
}


Fatorial de n:
long double fat(unsigned int n)
{
if (n <= 1 ) return 1;
else{
long double f = 1; unsigned int j;
for ( j = 1; j <= n; j++)
f = f*j;
return f;
}
}

Seno de x
double sen(double x)
{

x = x *pi() / 180.0;
int i, k; double p, s , x2;
x2 = -x * x;
p = x;
s = p;
k = 3;
while ( mod(p) > 1E-20)
{
p = p * x2 / ( k*(k-1));
s = s+p;
k = k+2;
}
return s;
}


Módulo de x;
double mod( double x)
{
if (x <= 0)
x = -x;
return x;
}

Cosseno de x
double cosseno(double x)
{
x = x *pi() / 180.0;
int k=1;
double c = 0.0;
double p=1.0;
double q = -x * x;
while ( mod(p) > 1E-20)
{
c = c + p;
p = p* (q / ( k * ( k +1)));
k = k+2;
}
return c;
}

Cálculo do valor de pi
double pi()
{
int c=1; double s=0, i;
for (i=2;i<=1E7;i++)
{
c = -c;
s = s+4.0/(2*i-1)*c;
}
s = s+4;
return s;
}

Nenhum comentário: