Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void dif_dividides(double *x, double *y,int n){
  6. double *cs=malloc(sizeof(double)*n);
  7. int i;
  8. for(i=1;i<n;i++){
  9. int j;
  10. for(j=0;j<n-i;j++){
  11. y[j]=(y[j+1]-y[j])/(x[j+i]-x[j]);
  12. }
  13. cs[i]=y[0];
  14. }
  15.  
  16. for(i=0;i<n;i++){
  17. y[i]=cs[i];
  18. }
  19. free(cs);
  20.  
  21. }
  22.  
  23. double aval(double *x,double *c, int n, double t){
  24. double r=0;
  25. int i;
  26. for(i=0;i<n;i++){
  27. double p=c[i];
  28. int j;
  29. for(j=0;j<i;j++){
  30. p=p*(t-x[j]);
  31. }
  32. r=r+p;
  33. }
  34. return r;
  35. }
  36.  
  37. void imprimirArray(double *a, int n){
  38. int i;
  39. for(i=0;i<n;i++){
  40. printf("%e,",a[i]);
  41. }
  42. printf("\n");
  43. }
  44.  
  45. double f(double t){
  46. return exp(sin(t)+cos(t));
  47. }
  48.  
  49. int main(int argc,char *argv[]){
  50. if(argc>0){
  51. char *aux;
  52. int n=(int)strtol(argv[0],&aux,10);
  53. double *x=malloc(sizeof(double)*(n+1));
  54. double *y=malloc(sizeof(double)*(n+1));
  55.  
  56. double inc=2*M_PI/n;
  57. double d=0;
  58. int i=0;
  59. while(d<=2*M_PI){
  60. x[i]=d;
  61. y[i]=f(d);
  62. d=d+inc;
  63. i++;
  64. }
  65.  
  66.  
  67. dif_dividides(x,y,n+1);
  68.  
  69. char *nomArxiu="interp0";
  70. char cn='0'+n;
  71. aux=strcat(nomArxiu,&cn);
  72. aux=strcat(nomArxiu,".res");
  73. FILE *file=fopen(nomArxiu,"w");
  74. d=0.f;
  75. inc=2*M_PI/1000;
  76. double exacte=f(d);
  77. double aproximat=aval(x,y,n+1,d);
  78. double errorRelatiu=abs((exacte-aproximat)/exacte);
  79. for(i=0;i<1000;i++){
  80. fprintf(f,"%e %e %e %e",d,exacte,aproximat,errorRelatiu);
  81. d=d+inc;
  82. }
  83. fclose(file);
  84. }
  85. exit(0);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement