Guest User

Untitled

a guest
Nov 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4.  
  5. float f(float x)
  6. {
  7. return (pow(x,3)-4*x-9);
  8. }
  9.  
  10. void main( )
  11. {
  12. float a=-1.0, b=0.0, x=0.0, y=0.0, err;
  13. while(f(a)*f(b)>0)
  14. {
  15. a=b;
  16. b=b+1.0;
  17. }
  18. printf(β€œEnter the error: β€œ);
  19. scanf(β€œ%f”,&err);
  20. printf("\n\troot lies between: %f and %f",a,b);
  21. printf("\n\n\t a(-ve)\tb(+ve)\t\t x\t\t f(x)");
  22. printf("\n\t-------------------------------------------------------------\n");
  23. //*swapping the values*//
  24.  
  25. if(f(a)>0)
  26. {
  27. a=a+b;
  28. b=a-b;
  29. a=a-b;
  30. }
  31. //* Finding the approximate value of the root*//
  32.  
  33. do
  34. {
  35. y=x;
  36. x=(a+b)/2;
  37. if(f(x)<0)
  38. a=x;
  39. else
  40. b=x;
  41. printf("\n\t%f\t%f\t%f\t %f",a,b,x,f(x));
  42. }while(fabs(x-y) > err);
  43.  
  44. printf("\n\t-------------------------------------------------------------\n");
  45. printf("\n\tAnswer: %.4f (correct upto 4 decimal places)",x);
  46.  
  47. getch( );
  48. }
Add Comment
Please, Sign In to add comment