Advertisement
Marini21

solve it

Dec 16th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. double EPS = 0.00001;
  8. int p,q,r,s,t,u;
  9.  
  10. double F(double);
  11. double derF(double x);
  12. double findRoot();
  13.  
  14. int main(){
  15.  
  16.     while(cin >> p >> q >> r >> s >> t >> u){
  17.  
  18.         if(F(0) * F(1) > 0) cout << "No Solution" << endl;
  19.         else cout << findRoot() << endl;
  20.     }
  21.  
  22. }
  23.  
  24.  
  25. double F(double x){
  26.     return p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) +t*x*x + u;
  27. }
  28.  
  29. double derF(double x){
  30.     return -p*exp(-x) + q*cos(x) - r*sin(x) + s/(cos(x)* cos(x)) + 2*t*x;
  31. }
  32.  
  33. double findRoot(){
  34.  
  35.     if (F(0)== 0) return 0;
  36.  
  37.     for(double x = 0.5; ;){
  38.         double x1 = x - F(x)/derF(x);
  39.         if(fabs(x1-x) < EPS) return x;
  40.         x = x1;
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement