Advertisement
Guest User

5. labor 2. feladat

a guest
Oct 14th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. double szinusz(double x)
  2. {
  3.     return sin(x - 0.6) - 0.4;
  4. }
  5.  
  6. double gyok(void) //ez csak kiszamolja az elso gyokot pozitiv iranyban ellenorzesi cellal
  7. {  
  8.     double x;
  9.     for (x = 0; fabs(szinusz(x)) > 0.0001; x += 0.0001);
  10.     return x;
  11. }
  12.  
  13. double gyokhely(double x, double y, double z)
  14. {
  15.         double n = x, p = y; //n=also hatar ,p=felso hatar
  16.         while (p - n > z)
  17.         {
  18.             double k;
  19.             k = (n + p) / 2.0;
  20.  
  21.             if (szinusz(p) < 0 && szinusz(n) > 0)
  22.             {
  23.                 if (szinusz(k) > 0.0)
  24.                     n = k;
  25.                 else
  26.                     p = k;
  27.             }
  28.             else if (szinusz(p) > 0 && szinusz(n) < 0)
  29.             {
  30.                 if (szinusz(k) > 0.0)
  31.                     p = k;
  32.                 else
  33.                     n = k;
  34.             }
  35.             else
  36.             {
  37.                 k = (n + p) / 2.0;
  38.                 p=k;
  39.             }
  40.         }
  41.         return n;
  42.  
  43. }
  44.  
  45. int main(void)
  46. {
  47.     printf("First root: %lf", gyok()); //ellenorzes | debugging
  48.     double also, felso, pontossag, gyok;
  49.     printf("\n\nPlease enter the lower-, upper-limit and the precision :");
  50.     if (scanf("%lf %lf %lf", &also, &felso, &pontossag) != 3)
  51.     {
  52.         printf("\nWrong input!\n");
  53.         return 0;
  54.     }
  55.     if (felso - also < 0)
  56.     {
  57.         printf("\nWrong input!\n");
  58.         return 0;
  59.     }
  60.     gyok = gyokhely(also, felso, pontossag);
  61.     if (gyok == also)
  62.         printf("\nThere are no roots in the given interval.\n");
  63.     else
  64.     {
  65.         printf("%lf\n", gyok);
  66.         printf("Value of the function at the root:  %lf\n\n", szinusz(gyokhely(also, felso, pontossag))); //ellenorzes | debugging
  67.     }
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement