Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double szinusz(double x)
- {
- return sin(x - 0.6) - 0.4;
- }
- double gyok(void) //ez csak kiszamolja az elso gyokot pozitiv iranyban ellenorzesi cellal
- {
- double x;
- for (x = 0; fabs(szinusz(x)) > 0.0001; x += 0.0001);
- return x;
- }
- double gyokhely(double x, double y, double z)
- {
- double n = x, p = y; //n=also hatar ,p=felso hatar
- while (p - n > z)
- {
- double k;
- k = (n + p) / 2.0;
- if (szinusz(p) < 0 && szinusz(n) > 0)
- {
- if (szinusz(k) > 0.0)
- n = k;
- else
- p = k;
- }
- else if (szinusz(p) > 0 && szinusz(n) < 0)
- {
- if (szinusz(k) > 0.0)
- p = k;
- else
- n = k;
- }
- else
- {
- k = (n + p) / 2.0;
- p=k;
- }
- }
- return n;
- }
- int main(void)
- {
- printf("First root: %lf", gyok()); //ellenorzes | debugging
- double also, felso, pontossag, gyok;
- printf("\n\nPlease enter the lower-, upper-limit and the precision :");
- if (scanf("%lf %lf %lf", &also, &felso, &pontossag) != 3)
- {
- printf("\nWrong input!\n");
- return 0;
- }
- if (felso - also < 0)
- {
- printf("\nWrong input!\n");
- return 0;
- }
- gyok = gyokhely(also, felso, pontossag);
- if (gyok == also)
- printf("\nThere are no roots in the given interval.\n");
- else
- {
- printf("%lf\n", gyok);
- printf("Value of the function at the root: %lf\n\n", szinusz(gyokhely(also, felso, pontossag))); //ellenorzes | debugging
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement