ProgIng - Programación en Ingeniería
Loading...
Searching...
No Matches
Ejemplo016.c File Reference

Aproximación de sen(x) con Taylor usando recurrencia entre términos (O(n)). More...

#include <stdio.h>
Include dependency graph for Ejemplo016.c:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Detailed Description

Aproximación de sen(x) con Taylor usando recurrencia entre términos (O(n)).

Serie: sen(x) = sum_{i=0}^{∞} (-1)^i * x^{2i+1} / (2i+1)!

Definimos el término positivo: t_i = x^{2i+1} / (2i+1)! con recurrencia: t_0 = x t_{i+1} = t_i * (x^2) / [(2i+2)(2i+3)]

Luego: sen(x) ≈ sum_{i=0}^{n-1} (-1)^i * t_i

Entrada
  • Un entero n >= 1.
  • Un real x.
Salida
Imprime:
sen(x) = valor
Complejidad
Tiempo: O(n). Memoria: O(1).
Note
Es la versión más eficiente (respecto a Ejemplo014 y Ejemplo015) porque evita recalcular potencias/factoriales en cada término.

Definition in file Ejemplo016.c.

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 38 of file Ejemplo016.c.

39{
40 (void)argc;
41 (void)argv;
42
43 int n, i;
44 int signo; /* +1 o -1 alternando */
45 float x;
46 float sx; /* acumulador */
47 float fct; /* término positivo t_i = x^(2i+1)/(2i+1)! */
48
49 do {
50 printf("Ingrese el numero de terminos: ");
51 scanf("%d", &n);
52 } while (n < 1);
53
54 printf("Ingrese el valor de x: ");
55 scanf("%f", &x);
56
57 /*
58 Inicialización:
59 t0 = x
60 sx = 0
61 En cada iteración:
62 sx += (-1)^i * t_i
63 t_{i+1} = t_i * (x^2)/[(2i+2)(2i+3)]
64 */
65 for (i = 0, sx = 0, fct = x; i < n; i++)
66 {
67 signo = 1 - 2*(i % 2); /* +1 si i par; -1 si i impar */
68
69 sx += (signo * fct);
70
71 /* Actualizar al siguiente término positivo */
72 fct *= (x / (2*i + 2)) * (x / (2*i + 3));
73 }
74
75 printf("sen(%f) = %f\n", x, sx);
76 return 0;
77}
double fct(double x, int n)
Definition Ejemplo029.c:34
Here is the call graph for this function: