Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. ///MAIN.CPP///
  2.  
  3. ///ATTENTION ROOTFINDER.CPP MODIF POUR FCT 2 VARIABLES !!
  4.  
  5. #include <iostream>
  6. #include "extremumFinder.h"
  7. #include "rootFinder.h"
  8. #include <cmath>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. double f(double x,double y)
  14. {
  15.   double a = 0.54533;
  16.   return pow(pow(x,2)+pow(y+a,2)-1,3)-pow(x,2)*pow(y+a,3);
  17. }
  18.  
  19. int main()
  20. {
  21.     ofstream retour1 ("roots1.txt",ios::app);
  22.     ofstream retour2 ("roots2.txt",ios::app);
  23.     if (retour1)
  24.     {
  25.          for (double x=-1.139;x<1.139;x=x+0.004556)
  26.         retour1 << x << ";" << getRoot_bmethod(f,x,-2, 0, pow(10,-3))<< endl;
  27.     cout << "Fichier roots1.txt cree" << endl;
  28.     }
  29.     else
  30.         cout << "ERREUR FICHIER";
  31.     if (retour2)
  32.     {
  33.          for (double x=-1.139;x<1.139;x=x+0.004556)
  34.         retour2 << x << ";" << getRoot_bmethod(f,x,0, 1, pow(10,-3))<< endl;
  35.     cout << "Fichier roots2.txt cree" << endl;
  36.     }
  37.     else
  38.         cout << "ERREUR FICHIER";
  39. }
  40.  
  41. ///ROOTFINDER.CPP///
  42.  
  43.  
  44. #include <iostream>
  45. #include "rootFinder.h"
  46. #include <cmath>
  47.  
  48. using namespace std;
  49.  
  50. double getRoot_bmethod(double (*f)(double,double),double x, double a, double b, double eps)
  51. {
  52.     double c=(b+a)/2;
  53.     if(f(x,a)*f(x,b)>0) ///Si pas de zéro, renvoie "pas de zéro"
  54.         return NAN;
  55.     else if (fabs(f(x,a))<eps) ///Si a est le zéro
  56.         return a;
  57.     else if (fabs(f(x,b))<eps) ///Si b est le zéro
  58.         return b;
  59.     else if (fabs(f(x,c))<eps) ///Si c est direct le zéro
  60.         return c;
  61.     else ///Si au moins un zéro
  62.         do
  63.         {
  64.             c=(b+a)/2;
  65.              if (f(x,c)*f(x,b)>0)
  66.                 b=c;
  67.             else
  68.                 a=c;
  69.         }
  70.         while (fabs(a-b)>eps);
  71.     return c;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement