Advertisement
Guest User

interpo.c

a guest
Nov 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. float interpol_Lagrange(int n, float abs[n], float ord[n], float x);
  4. float lix (int n, float abs[n], float x, int ind);
  5. float interpol_Newton(int n, float abs[n], float ord[n], float x, int deg, float *b);
  6. float difdiv(float* abs, float* ord, int deg, int ind);
  7. void remplir_b(float* abs, float* ord, int n, float b[n]);
  8. void remplir_dens(float* abs,float* ord);
  9. void remplir_revenu(float* abs,float*  ord);
  10.  
  11. int main()
  12. {
  13.     float i = 0;
  14.     float x,res;
  15.     float abs[20];
  16.     float ord[20];
  17.     float b[20];
  18.     remplir_dens(abs,ord);
  19.     remplir_b(abs,ord,20,b);
  20.    
  21.     while(i < 40)
  22.     {
  23.         res=interpol_Newton(20,abs,ord,i,19,b);
  24.         printf("%f : %f\n",i ,res);
  25.         i += 0.5;  
  26.     }
  27.    
  28.    
  29.    
  30.     return (0);
  31. }
  32.  
  33. float interpol_Lagrange(int n, float abs[n], float ord[n], float x)
  34. {
  35.     int i = 0;
  36.     float poly = 0;
  37.    
  38.     for(i = 0; i < n; i++)
  39.     {
  40.         poly += ord[i] * lix(n, abs, x, i);
  41.     }
  42.    
  43.     return poly;
  44. }
  45.  
  46. float lix (int n, float abs[n], float x, int ind)
  47. {
  48.     int j;
  49.     float pi = 1;
  50.    
  51.     for(j = 0; j < n; j++)
  52.     {
  53.         if (j != ind)
  54.         {
  55.             pi = pi*((x-abs[j])/(abs[ind]-abs[j]));
  56.         }
  57.  
  58.     }
  59.    
  60.     return (pi);
  61. }
  62.  
  63. float interpol_Newton(int n, float abs[n], float ord[n], float x, int deg, float *b)
  64. {
  65.     float res;
  66.    
  67.     if (deg == 0)
  68.         return b[n - 1];
  69.  
  70.     else
  71.     {  
  72.         res = b[n - deg - 1] + (x - abs[n - deg - 1]) * interpol_Newton(n, abs, ord, x, deg - 1, b);
  73.         return res;
  74.     }
  75. }
  76.  
  77. float difdiv(float* abs, float* ord, int deg, int ind)
  78. {
  79.     float dif;
  80.     if (ind<deg)
  81.         return 0;
  82.     if (deg==0)
  83.     {
  84.         dif=ord[ind];
  85.     }
  86.     else if (deg==1)
  87.         dif=(ord[ind]-ord[0])/(abs[ind]-abs[0]);
  88.    
  89.         else
  90.         {
  91.             dif =(difdiv(abs,ord,deg-1,ind) - difdiv(abs,ord,deg-1,deg-1))/(abs[ind]-abs[deg-1]);
  92.         }
  93.     return (dif);
  94. }
  95.  
  96. void remplir_b(float* abs, float* ord, int n, float b[n])
  97. {
  98.     int i;
  99.     for (i=0;i<n;i++)
  100.     {
  101.         b[i]=difdiv(abs,ord,i,i);
  102.         //printf("%f",b[i]);
  103.     }
  104.        
  105. }
  106.  
  107. void remplir_dens(float* abs,float*  ord)
  108. {
  109.     float abs1[20]={0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38};
  110.     float ord1[20]={0.99987,0.99997,1,0.99997,0.99988,0.99973,0.99953,0.99927,0.99289,0.99846,0.99805,0.999751,0.99705,0.99650,0.99664,0.99533,0.99472,0.99472,0.99333,0.99326};
  111.     int i;
  112.     for (i=0;i<20;i++)
  113.     {
  114.         abs[i]=abs1[i];
  115.         ord[i]=ord1[i];
  116.     }
  117. }
  118.  
  119.  
  120. void remplir_revenu(float* abs,float*  ord)
  121. {
  122.     float abs1[21]={752,855,871,734,610,582,221,492,569,462,907,643,862,524,679,902,918,828,875,809,894};
  123.     float ord1[21]={85,83,162,79,81,83,281,81,81,80,243,84,84,82,80,226,260,82,186,77,223};
  124.     int i;
  125.     for (i=0;i<21;i++)
  126.     {
  127.         abs[i]=abs1[i];
  128.         ord[i]=ord1[i];
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement