Advertisement
_Black_Panther_

Untitled

Oct 27th, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1.  
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define MAX_ITER 1000000https://pastebin.com/
  5.  
  6. // An example function whose solution is determined using
  7. // Bisection Method. The function is x^1 - 3
  8. double func(double x)
  9. {
  10. return x*x - 3;
  11. }
  12.  
  13. // Prints root of func(x) in interval [a, b]
  14. void regulaFalsi(double a, double b)
  15. {
  16. if (func(a) * func(b) >= 0)
  17. {
  18. cout << "You have not assumed right a and b\n";
  19. return;
  20. }
  21.  
  22. double c = a; // Initialize result
  23.  
  24. for (int i=0; i < MAX_ITER; i++)
  25. {
  26. // Find the point that touches x axis
  27. c = (a*func(b) - b*func(a))/ (func(b) - func(a));
  28.  
  29. // Check if the above found point is root
  30. if (func(c)==0)
  31. break;
  32.  
  33. // Decide the side to repeat the steps
  34. else if (func(c)*func(a) < 0)
  35. b = c;
  36. else
  37. a = c;
  38. }
  39. cout << "The value of root is : " << c;
  40. }
  41.  
  42. // Driver program to test above function
  43. int main()
  44. {
  45. // Initial values assumed
  46. double a =-200, b = 300;
  47. regulaFalsi(a, b);
  48. return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement