Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- double f1(double x)
- {
- return (2*x);
- }
- double f2(double x)
- {
- return (x*x - 5*x + 4);
- }
- double f3(double x)
- {
- return (3*x*x*x - 5*x - 1);
- }
- double bisection(double (*f)(), double floor, double ceiling, double approximation)
- {
- while(abs(f((ceiling + floor)/2)) >= approximation)
- {
- double x = (floor + ceiling) / 2;
- if (f(floor) * f(x) <= 0)
- {
- ceiling = x;
- }
- else if ( f(ceiling) * f(x) <= 0 )
- {
- floor = x;
- }
- }
- return ((floor + ceiling)/2);
- }
- void menu()
- {
- double floor, ceiling;
- int choice;
- printf("1. f(x) = 2x\n2. f(x) = x^2 - 5x + 4\n3. f(x) = 3x^2 - 5x - 1\n");
- scanf("%i", &choice);
- printf("\n");
- printf("Podaj dolna granice przedzialu: ");
- scanf("%lf", &floor);
- printf("\n");
- printf("Podaj gorna granice przedzialu: ");
- scanf("%lf", &ceiling);
- switch (choice)
- {
- case 1:
- printf("%lf", bisection(&f1, floor, ceiling, 1.e-5));
- break;
- case 2:
- printf("%lf", bisection(&f2, floor, ceiling, 1.e-5));
- break;
- case 3:
- printf("%lf", bisection(&f3, floor, ceiling, 1.e-5));
- break;
- }
- }
- int main()
- {
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement