Advertisement
Guest User

taylor cos

a guest
Mar 21st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. // #############################################################
  2. // Este programa calcula o erro percentual da Série de Taylor
  3. // da função cos(bx), expandida no ponto x0
  4. #define DIM1 50
  5. #include <stdio.h>
  6. #include <math.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <iostream>
  10. main()
  11. {
  12. double a[DIM1];
  13. double x0,x,b,fxa,fxt,erro_p,Deltax,xa,xb,h,Pi;
  14. float fat(int n);
  15. FILE *ent;
  16. FILE *sai;
  17. int i,j,k,n,icont,nx,nt,aux,grau;
  18. ent = fopen("taylor150510v2.1","r");
  19. sai = fopen("taylor150510v2.s.2","w");
  20. fscanf(ent,"%lf",&x0); /* Ponto de expansão da série */
  21. fscanf(ent,"%i",&nt); /* grau + 1 */
  22. fscanf(ent,"%lf",&b); /* Coeficiente multiplicador b */
  23. fscanf(ent,"%i",&nx); /* Numero de abscissas */
  24. fscanf(ent,"%lf %lf",&xa,&xb); /* Dominio xa <= x <= xb */
  25. grau= nt-1;
  26. Deltax=xb-xa;
  27. h=Deltax/(nx-1);
  28. for ( n = 0 ; n <= grau ; n++)
  29. {
  30. i=1;
  31. if(n%2==0)
  32. { // Par
  33. j=0;
  34. k=1;
  35. if(n%4==0) i=2; // múltiplo de 4
  36. }
  37. else
  38. { // Ímpar
  39. j=1;
  40. k=0;
  41. if((n-1)%4==0) i=2; // múltiplo de 4 + 1
  42. }
  43. a[n]=pow(-1.,i)*pow(b,n)*pow(sin(b*x0),j)*pow(cos(b*x0),k);
  44. }
  45. x=xa;
  46. /* Cabeçalho */
  47. fprintf(sai," x f(x) F(x) Ep(%)\n");
  48. for (icont=1;icont<=nx;icont++)
  49. {
  50. fxt=0.;
  51. for (n=0;n<=grau;n++)
  52. {
  53. fxt=fxt+a[n]*pow(x-x0,n)/fat(n); /* Solução numérica F(x) */
  54. }
  55. fxa=cos(b*x); /* Solução analítica f(x) */
  56.  
  57. if(fxa != 0.)
  58. {
  59. erro_p=fabs((fxt-fxa)/fxa)*100.; /* Erro percentual */
  60. fprintf(sai,"%12.8f %16.8f %16.8f %16.8e \n",x,fxa,fxt,erro_p);
  61. }
  62. else
  63. {
  64. fprintf(sai,"%12.8f %16.8f %16.8f - \n",x,fxa,fxt);
  65. }
  66. x = x +h;
  67. }
  68. fclose(ent); /* Fechando os arquivos */
  69. fclose(sai);
  70. system ("pause");
  71. }
  72. // ***********************************************************************
  73. double fat(int n)
  74. {
  75. int i;
  76. double xi,fat;
  77. fat=n;
  78. for (i=n; i>=2; i--)
  79. {
  80. xi = i ;
  81. fat=fat*(xi-1.);
  82. }
  83. if(n==0) fat=1.;
  84. return(fat);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement