Advertisement
Guest User

FUNCION DE TAYLOR DE COS

a guest
Apr 19th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. /**
  2.  
  3.     Desarrollar el presente taller en grupo de, a lo más, 2 (dos) estudiantes, en un tiempo máximo de 30 minutos.
  4.     La fórmula de Taylor para la función trigonométrica cos x se define como:
  5.  
  6.             cos x = 1 – (x^2)/2! + (x^4)/4! – (x^6)/6! + … + (-1)^n x^2n/(2n)!
  7.  
  8.     Construir un programa en lenguaje C que lea valores para x y, incluyendo al menos una función, determine el valor de cos x.
  9.  
  10.     - INTEGRANTES:
  11.  
  12.     SEBASTIÁN MUÑOZ ORTIZ (19.839.581-1)
  13.     NICOLAS JARA CARVAJAL (19.649.455-3)
  14.  
  15.  
  16. **/
  17.  
  18. /*
  19.   OBSERVACIÓN DE NOSOTROS: X RECIBE EL VALOR EN GRADOS, PERO
  20.   LA FUNCIÓN ES UNA APROXIMACIÓN. EN ESTE CASO, EN LA FUNCION
  21.   NO VA EL X DIRECTO, SINO QUE DEBE TRANSFORMARSE A RADIANES
  22.   PARA QUE FUNCIONE BIEN.
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27.  
  28. #define PI 3.14159265358979323846 // se define PI
  29.  
  30. double taylor(double x, int n); // x recibirá decimales
  31. double potencia(double A, int B); // A puede recibir decimales
  32. double factorial(int A);
  33.  
  34. /*          */
  35. int main()
  36. {
  37.     int n = 7;
  38.     double x = 90, t; // x es el valor en grados
  39.     t = taylor(x, n); // taylor(rad, 3)
  40.  
  41.     printf("\nValor aproximado del cos %.0lf con n = %d es %.2lf\n", x, n, t);
  42.  
  43.     return 0;
  44. }
  45.  
  46. /*          */
  47. double potencia(double A, int B)
  48. {
  49.     if(B == 0) return 1;
  50.     return A * (potencia(A, B - 1));
  51. }
  52.  
  53. double factorial(int A)
  54. {
  55.     if(A == 0) return 1;
  56.     return A * factorial(A-1);
  57. }
  58.  
  59. double taylor(double x, int n)
  60. {
  61.     int i;
  62.     double rad;
  63.     rad = (x*PI/180);
  64.     //printf("\n\n%lf\n\n", rad); //Revisa el valor que entra
  65.  
  66.     double coseno = 0;
  67.  
  68.     for(i = 0; i < n; i++)
  69.     {
  70.         /* PARA REVISAR LOS VALORES */
  71.         /*printf("\n%lf ---> n: %d ---> (-1)^n", potencia(-1, i), i);
  72.         printf("\n%lf ---> x^2n", potencia(rad, 2*i));
  73.         printf("\n%lf ---> 2n!", factorial(2*i));
  74.         printf("\n\n");*/
  75.        
  76.         coseno = coseno + ((potencia(-1, i) * potencia(rad, 2*i))/(factorial(2*i)));
  77.     }
  78.     return coseno;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement