Advertisement
Guest User

sieczneRysia

a guest
Jun 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<math.h>
  5.  
  6. //wyliczanie wartości funkcji w zadanym x
  7. float funcArg(int r, float arg, float tab[20])
  8. {
  9.     float result = 0;
  10.     int i;
  11.  
  12.     for(i=0; i<=r; i++)
  13.         result += pow(arg,i)*tab[i];
  14.    
  15.     return result;
  16. }
  17.  
  18. //wartość pierwszej pochodnej funkcji w x
  19. float deriv(int r, float arg, float tab[20])
  20. {
  21.     float result = 0;
  22.     int i;
  23.  
  24.     for(i=1; i<=r; i++)
  25.         result += pow(arg,i-1)*tab[i]*i;
  26.    
  27.     return result;
  28. }
  29.  
  30. //wartość drugiej pochodnej funkcji w x
  31. float deriv2(int r, float arg, float tab[20])
  32. {
  33.     float result = 0;
  34.     int i;
  35.  
  36.     for(i=2; i<=r; i++)
  37.         result += pow(arg,i-2)*tab[i]*i*(i-1);
  38.    
  39.     return result;
  40. }
  41.  
  42.  
  43. int main(int argc,char **argv)
  44. {
  45.     float a, b, E, wsp[20], x, y, yk, y1,y2,z;
  46.     int i, n;
  47.  
  48.     printf("Podaj rzad wielomianu: ");
  49.     scanf("%d",&n);
  50.  
  51.     for(i=0; i<=n; i++)
  52.     {
  53.         printf("Podaj wspolczynnik znajdujacy sie przy %d potedze: ",i);
  54.         scanf("%f",&wsp[i]);
  55.     }
  56.  
  57.  
  58.     printf("Podaj granice przedzialu:\n");
  59.     printf("a = ");
  60.     scanf("%f",&a);
  61.     printf("b = ");
  62.     scanf("%f",&b);
  63.     printf("Podaj dokladnosc :");
  64.     scanf("%f",&E);
  65.  
  66.    
  67.     z = (a + b)/2;
  68.    
  69.     //obliczenie wartości pierwszej i drugiej pochodnej w punkcie z
  70.     y1 = deriv(n,z,wsp);
  71.     y2 = deriv2(n,z,wsp);
  72.  
  73.     //algorytm z instrukcji
  74.     y = funcArg(n,b,wsp);
  75.     yk = funcArg(n,a,wsp);
  76.  
  77.     x = b;
  78.     i = 0;
  79.     if(y1*y2 < 0)
  80.     {
  81.         while(fabs(y) > E)
  82.         {      
  83.             x -= y*(a-x)/(yk-y);
  84.             y = funcArg(n,x,wsp);
  85.             i++;
  86.         }
  87.     printf("Otrzymany pierwiastek x = %0.3f\n", x);
  88.     } else printf("Funkcja nie ma pierwiastkow w tym przedziale");
  89.        
  90.    
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement