Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. float l,u;
  4. int j,i=0;
  5. float arr[20];
  6. float f(float x)
  7. {
  8.  
  9. return (x*x*x-x-1);
  10. }
  11.  
  12. float itr(float a, float b)
  13. {
  14. float x=(a+b)/2;
  15. i++;
  16.  
  17. arr[i-1]=x;
  18. return x;
  19. }
  20. float limit()
  21. {
  22.  
  23. if(f(l)*f(arr[i-1])>0)
  24. {
  25. l=arr[i-1];
  26. }
  27.  
  28. else
  29. u=arr[i-1];
  30.  
  31. }
  32. void main()
  33. {
  34.  
  35. printf("Enter the lower value\n");
  36. scanf("%f",&l);
  37. printf("Enter the upper value\n");
  38. scanf("%f",&u);
  39. printf("Enter the Iteration\n");
  40. scanf("%d",&j);
  41. if(f(l)*f(u)>0)
  42. {
  43. printf("Here is no root");
  44. }
  45. else
  46. {
  47. while(i<=j)
  48. {
  49. printf("Iteration %d root: %0.3f\t",i,itr(l,u));
  50. if(i>=2)
  51. {
  52. int err=(fabs(arr[i-1]-arr[i-2])/arr[i-1])*100;
  53. printf("error %d\n",err);
  54.  
  55.  
  56. }
  57. else
  58. {
  59. printf("No Error\n");
  60. }
  61. limit();
  62. }
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement