Advertisement
Guest User

Untitled

a guest
Nov 19th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <iomanip>
  5. long double func(long double x)
  6. {
  7. return sin(x)-0.5*x;
  8. }
  9.  
  10. long double dfunc(long double x)
  11. {
  12. return cos(x)-0.5;
  13. }
  14.  
  15. class RootFinder{
  16. private:
  17. long double (*drf)(long double ,long double ,long double ,long double );
  18. long double (*f)(long double );
  19. long double (*df)(long double );
  20. long double root2(long double min, long double max)
  21. {
  22.  
  23. long double x;
  24. x=(max+min)/2;
  25. long double y=x-f(x)/df(x);
  26. if (f(y)<pow(10,-20)&&f(y)>-1*pow(10,-20))
  27. return y;
  28. long double w=y-f(y)/df(y);
  29. return root2(w,w);
  30. }
  31. long double root1(long double min, long double max)
  32. {
  33. std::vector<long double> d(2);
  34. d.at(0)=min;
  35. d.at(1)=max;
  36. long double df=(max-min)/2;
  37. if(f(d.at(0)+df)==f(d.at(0)))
  38. return d.at(0);
  39. else if(f(d.at(1)-df)==f(d.at(1)))
  40. return d.at(1);
  41. if( (f(d.at(0)+df)>0&&f(d.at(0))<0) || (f(d.at(0)+df)<0&&f(d.at(0))>0) )
  42. {
  43. return root1(d.at(0),d.at(0)+df);
  44. }
  45. else
  46. {
  47. std::vector<long double>::iterator it;
  48. it=d.begin();
  49. d.erase(it);
  50. return root1(d.at(0)-df,d.at(0));
  51. }
  52. }
  53. public:
  54. RootFinder(){
  55. std::cout<<"shaviman\n";
  56. }
  57. RootFinder( long double (*function)(long double )){
  58. std::cout<<"tetriman\n";
  59. f=function;
  60. }
  61. RootFinder( long double (*function)(long double ), long double (*dfunction)(long double )){
  62. f=function, df=dfunction;
  63. }
  64. void setDf(long double (*dfunction)(long double )){
  65. df=dfunction;
  66. }
  67. long double callF(long double x){
  68. return f(x);
  69. }
  70. long double GetRoot2(long double min, long double max){
  71. return root2(min,max);
  72. }
  73. long double GetRoot1(long double min, long double max){
  74. return root1(min,max);
  75. }
  76.  
  77.  
  78. };
  79. class dRootFinder: public RootFinder{
  80. private:
  81. long double a, b, c;
  82. long double DrtFunc (long double x)
  83. {
  84. return a*pow(x,2)+b*x+c;
  85. }
  86. public:
  87. dRootFinder(long double A,long double B,long double C){
  88. a=A, b=B, c=C;
  89. RootFinder(DrtFunc);
  90. }
  91. std::vector<long double> droot(){
  92. std::vector<long double> x(2);
  93. if(pow(b,2)-4*a*c<0){
  94. std::cout<<"no roots\n";
  95. x.pop_back();
  96. x.pop_back();
  97. return x;
  98. }
  99. else if(pow(b,2)-4*a*c==0){
  100. x.pop_back();
  101. x.at(0)=-b/2/a;
  102. return x;
  103. }
  104. else{
  105. x.at(0)=(-b-pow((pow(b,2)-4*a*c),0.5))/2/a;
  106. x.at(1)=(-b+pow((pow(b,2)-4*a*c),0.5))/2/a;
  107. return x;
  108. }
  109. }
  110. std::vector<long double>GetDRoot(){
  111. return droot();
  112. }
  113. };
  114.  
  115. int main()
  116. {
  117. long double min=1, max=3;
  118. // long double x;
  119. // std:: cin>>x;
  120. // RootFinder func1(func,dfunc);
  121.  
  122. // std:: cout<<func1.callF(x)<<std::endl;
  123. // std:: cout<<std::setprecision(9)<<func1.GetRoot2(min,max)<<std::endl;
  124. // std:: cout<<std::setprecision(9)<<func1.GetRoot1(0,2)<<std::endl;
  125.  
  126. dRootFinder drt1(1,0,0);
  127. // std::vector<long double> a=drt1.GetDRoot();
  128. //for(int i=0;i<a.size();i++)
  129. // std::cout<<a.at(i)<<std::endl;
  130. std:: cout<<std::setprecision(9)<<drt1.GetRoot1(-5,-0.5)<<std::endl;
  131. // std:: cout<<drt1.callF(3)<<std::endl;
  132.  
  133. }
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement