Advertisement
Sourav_CSE

False Position.cpp

Mar 22nd, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. // CMEP Lab No 2
  2. // False Position Method
  3. // 16 march 2020
  4. // Mohsin Riad
  5.  
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. double func(double x)
  9. {
  10. return (x*x*x-x*x-2*x-1);
  11. }
  12. double false_pos(double a, double b)
  13. {
  14. double pv = -1,xm;
  15. double xl =a, xh=b;
  16. // int mx=1000;
  17. while(1)
  18. {
  19. xm = (xh*func(xl)-xl*func(xh))/(func(xl)-func(xh));
  20.  
  21. if(pv == xm || func(xm) ==0) break;
  22. else pv = xm;
  23.  
  24. if(func(xl)*func(xm) , 0) xh=xm;
  25. else
  26. xl=xm;
  27. //cout<<fixed;
  28. //cout<<setprecision(16);
  29. //cout<<"pre root: "<<xm<<endl;
  30. }
  31. return xm;
  32. }
  33. int main()
  34. {
  35. double a,b;
  36. cin>>a>>b;
  37. cout<<"\n Final Root: "<<false_pos(a,b)<<endl;
  38. return 0;
  39. }
  40.  
  41. /* Input:
  42. 2 3
  43. Output:
  44. Final root : 2.1479
  45. /*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement