Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // c program for implementation of bisection -- abhay's version
- #include<stdio.h>
- float fun(float);
- main()
- {
- float x1, x2, r;
- int i, j;
- for(i=0; i<10; i++)
- {
- printf("i = %d, fun(i) = %f\n", i, fun(i));
- if(fun(i) < 0)
- break;
- }
- printf("\n i = %d\n", i);
- x1 = i;
- printf("x1 = %f\n", x1);
- for(j=0; j<10; j++)
- {
- if(fun(j) > 0)
- break;
- }
- x2 = j;
- printf("x2 = %d", x2);
- while(1)
- {
- r = (x1 + x2)/2;
- if( fun(r) < 0)
- x1 = r;
- else x2 = r;
- if( abs(fun(x2) - fun(x1) <= 0.0001))
- break;
- }
- printf("\nYour root is : %f", r);
- return 0;
- }
- float fun(float x)
- {
- return (x*x) - (4*x) + 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment