Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. double abs_double(double y)
  6. {
  7. y = abs(y) ;
  8. return y;
  9. }
  10.  
  11. double fx_val(double a, double b, double c, double d, double e, double x)
  12. {
  13. double a_x = a * x * x * x * x ;
  14. double b_x = b * x * x * x ;
  15. double c_x = c * x * x ;
  16. double d_x = d * x ;
  17. double e_x = e ;
  18. double sum_x = a_x + b_x + c_x + d_x + e_x ;
  19. return sum_x;
  20. }
  21.  
  22. double fx_dval(double a, double b, double c, double d, double e, double x)
  23. {
  24. double a_dx = 4 * a * x * x * x ;
  25. double b_dx = 3 * b * x * x ;
  26. double c_dx = 2 * c * x ;
  27. double d_dx = d ;
  28. double e_dx = 0 ;
  29. double sum_dx = a_dx + b_dx + c_dx + d_dx + e_dx ;
  30. return sum_dx;
  31. }
  32.  
  33. double fx_ddval(double a, double b, double c, double d, double e, double x)
  34. {
  35. double a_ddx = 4 * 3 * a * x * x ;
  36. double b_ddx = 3 * 2 * b * x ;
  37. double c_ddx = 2 * 1 * c ;
  38. double d_ddx = 0 ;
  39. double e_ddx = 0 ;
  40. double sum_ddx = a_ddx + b_ddx + c_ddx + d_ddx + e_ddx ;
  41. return sum_ddx;
  42. }
  43.  
  44.  
  45. double newrfind_halley(double a, double b, double c, double d, double e, double x)
  46. {
  47. double halley_counter = 0;
  48. double z_change;
  49. while (halley_counter < 10000)
  50. {
  51. double sum_h = fx_val(a,b,c,d,e,x);
  52. double sum_dh = fx_dval(a,b,c,d,e,x);
  53. double sum_ddh = fx_ddval(a,b,c,d,e,x);
  54. double abs_sum_dh = abs_double(sum_dh);
  55. z_change = (2 * sum_h * sum_dh)/((2 * abs_sum_dh * abs_sum_dh)-(sum_h * sum_ddh));
  56. x = x - z_change;
  57. if (z_change < .000001 || z_change > -.000001)
  58. {
  59. return x;
  60. }
  61. else
  62. {
  63. halley_counter = halley_counter + 1;
  64. }
  65. }
  66. return 500000;
  67. }
  68.  
  69. int rootbound(double a, double b, double c, double d, double e, double r, double l)
  70. {
  71. double v = 0;
  72. if (a*(4*a*l+b) < 0 )
  73. {
  74. v = v + 1 ;
  75. }
  76.  
  77. if (((4*a*l+b)*(6*a*l*l+3*b*l+c))<0)
  78. {
  79. v = v + 1;
  80. }
  81.  
  82. if ((6*a*l*l+3*b*l+c)*(4*a*l*l*l+3*b*l*l+2*c*l+d) < 0)
  83. {
  84. v = v + 1;
  85. }
  86.  
  87. if ((4*a*l*l*l+3*b*l*l+2*c*l+d)*(a*l*l*l*l+b*l*l*l+c*l*l+d*l+e) < 0)
  88. {
  89. v= v + 1 ;
  90. }
  91.  
  92. if (a*(4*a*r+b) < 0 )
  93. {
  94. v = v - 1 ;
  95. }
  96.  
  97. if (((4*a*r+b)*(6*a*r*r+3*b*r+c))<0)
  98. {
  99. v = v - 1;
  100. }
  101.  
  102. if ((6*a*r*r+3*b*r+c)*(4*a*r*r*r+3*b*r*r+2*c*r+d) < 0)
  103. {
  104. v = v - 1;
  105. }
  106.  
  107. if ((4*a*r*r*r+3*b*r*r+2*c*r+d)*(a*r*r*r*r+b*r*r*r+c*r*r+d*r+e) < 0)
  108. {
  109. v= v - 1 ;
  110. }
  111.  
  112. return v;
  113. }
  114.  
  115. int main(int argc, char **argv)
  116. {
  117. double a, b, c, d, e, l, r;
  118. FILE *in;
  119.  
  120. if (argv[1] == NULL) {
  121. printf("You need an input file.\n");
  122. return -1;
  123. }
  124. in = fopen(argv[1], "r");
  125. if (in == NULL)
  126. return -1;
  127. fscanf(in, "%lf", &a);
  128. fscanf(in, "%lf", &b);
  129. fscanf(in, "%lf", &c);
  130. fscanf(in, "%lf", &d);
  131. fscanf(in, "%lf", &e);
  132. fscanf(in, "%lf", &l);
  133. fscanf(in, "%lf", &r);
  134.  
  135. double og_l = l;
  136. double og_r = r;
  137. double x = 0;
  138. double counter ;
  139. double rootchecker ;
  140. r = l + .5 ;
  141. l = l - .5;
  142. double boudan_var = rootbound(a,b,c,d,e,l,r);
  143. {
  144. if (boudan_var == 0)
  145. printf("The polynomial has no roots in the given interval.");
  146. else
  147. {
  148. for(counter = og_l ; counter == og_r ; counter = counter + .5)
  149. {
  150. l = l + .5;
  151. printf("%f", l);
  152. x = l ;
  153. rootchecker = newrfind_halley(a,b,c,d,e,x);
  154. if (rootchecker != 500000)
  155. {
  156. printf("Root found: %f", x);
  157. }
  158. else
  159. {
  160. printf("No Root Found");
  161. }
  162. }
  163. }
  164. }
  165.  
  166. //The values are read into the variable a, b, c, d, e, l and r.
  167. //You have to find the bound on the number of roots using rootbound function.
  168. //If it is > 0 try to find the roots using newrfind function.
  169. //You may use the fval, fdval and fddval funtions accordingly in implementing the halleys's method.
  170.  
  171.  
  172. fclose(in);
  173.  
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement