Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <math.h>  
  4.  
  5. float horner(float *p, int n, float x);
  6. float bisection(float *p, int n, float a, float b);
  7.  
  8. float horner(float *p, int n, float x)
  9. {
  10. /*
  11.    float result = 0.0;
  12. for (int i=0; i<n; i++) {
  13.         result = result*x + p[i];
  14. }
  15.  
  16. return result;
  17. */
  18. // pointer implementation
  19. float holder;
  20. holder = 0.0;
  21. for (int i = 0;i < n;i++){
  22. holder = holder * x + *(p + i);
  23. }
  24. return holder;
  25. }
  26.  
  27. //your bisection method implementation
  28. float bisection(float *p, int n, float a, float b)
  29. {
  30. float result;
  31. float tolerance = 1e-6;
  32. float c = 0.0;
  33.  
  34. if ( horner(p,n,a) * horner(p,n,b) < 0.0){
  35.  
  36. while ( b - a >= tolerance){
  37.  
  38. c = (a+b)/2.0;
  39.  
  40. if (horner(p,n,c) == 0.0){
  41. result = c;
  42. break;
  43. }
  44.  
  45. else if ( horner(p,n,c) * horner(p,n,a) < 0.0 ){
  46. b = c;
  47. }
  48. else {
  49.  
  50. a = c;
  51. }
  52. }
  53.  
  54. if ( b - a < tolerance){
  55. result = c;
  56. }
  57.  
  58. }
  59.  
  60. return result;
  61.  
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement