Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. /*--------------------------------------------------------*
  2. * Main Program *
  3. * This program evaluates root of a polynomial using *
  4. * Muller's method *
  5. *---------------------------------------------------------*
  6. * Functions invoked *
  7. * NIL *
  8. *---------------------------------------------------------*
  9. * Subroutines used *
  10. * F(x) *
  11. *---------------------------------------------------------*
  12. * Variables used *
  13. * x1,x2,x3 - initial values *
  14. * f1,f2,f3 - function values at x1,x2,x3 *
  15. * a0,a1,a2 - coefficients of quadratic polynomial *
  16. * hi - xi-x3 *
  17. * di - function difference fi-f3 *
  18. *---------------------------------------------------------*
  19. * Constants used *
  20. * EPD - Error bound *
  21. *---------------------------------------------------------*/
  22.  
  23. #include<math.h>
  24. #define EPS 0.000001
  25.  
  26. main()
  27. {
  28. float F(float x);
  29. float x1,x2,x3,x4,f1,f2,f3,f4,h1,h2,d1,d2,a0,a1,a2,h;
  30. printf("\nInput three initial points \n");
  31. scanf("%f %f %f",&x1,&x2,&x3);
  32.  
  33. f1 = F(x1);
  34. f2 = F(x2);
  35. f3 = F(x3);
  36.  
  37. begin:
  38. h1 = x1-x3;
  39. h2 = x2-x3;
  40.  
  41. d1 = f1-f3;
  42. d2 = f2-f3;
  43. /* Compute parameters a0,a1,a2 */
  44.  
  45. a0 = f3;
  46. a1 = (d2*h1*h1-d1*h2*h2)/(h1*h2*(h1-h2));
  47. a2 = (d1*h2-d2*h1)/(h*h2*(h1-h2));
  48.  
  49. /* Compute h */
  50.  
  51. if(a1>0.0)
  52. h=(-2.0*a0)/(a1+sqrt(a1*a1-4*a2*a0));
  53. else
  54. h=(-2.0*a0)/(a1-sqrt(a1*a1-4*a2*a0));
  55.  
  56. /* Compute x4 and f4 */
  57.  
  58. x4 = x3+h;
  59. f4 = F(x4);
  60.  
  61. /* Test for accuracy */
  62.  
  63. if(f4<=EPS) /* root obtained */
  64. {
  65. printf("\n\nROOT BY MULLER'S METHOD\n\n");
  66. printf("Root = %f\n",x4);
  67. printf("\n");
  68. }
  69. else
  70. {
  71. x1 = x2;
  72. x2 = x3;
  73. x3 = x4;
  74. f1 = f2;
  75. f2 = f3;
  76. f3 = f4;
  77. goto begin;
  78. }
  79. }
  80. /* End of main() program */
  81. /* -----------------------------------------------------------*/
  82. /* Defining the subroutine F(x) */
  83. float F(float x)
  84. {
  85. float f;
  86. f = x*x*x+2*x*x+10*x-20;
  87. return (f);
  88. }
  89. /* End of subroutine () */
  90. /* -----------------------------------------------------------*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement