Sommaire


Installer et compiler ces fichiers dans votre répertoire de travail.

c3a.c
'
/* --------------------------------- */
/* save as c3a.c                     */
/* --------------------------------- */
#include "x_hfile.h"
#include      "fa.h"
/* --------------------------------- */
int main(void)
{
   int                  n = 6;
double                  a = 7.0;
double FirstApproximation = 2.5;

 clrscrn();
 printf(" Use Newton's method to approximate sqrt(%.1f).        \n\n" 
        " This is equivalent to find the positive real root of :\n\n" 
        " f : x-> %s\n\n" 
        " You know that the positive real root of f is between 2.0 and 3.0\n" 
        "           (2.0**2 = 4)     (3.0**2 = 9)\n\n" 
        " Choose x = %.1f as a first approximation.\n\n",
         a, feq, FirstApproximation);
 stop();

 clrscrn();
 printf(" As a first approximation x = %.1f \n\n"
        " The Newton's method give :        \n\n",FirstApproximation);
 
 p_Newton_s_Method(FirstApproximation, n, f, Df);
 
 
 printf(" The Newton's method give : \n\n" 
        "          x = %.15f\n\n",
          Newton_s_Method(FirstApproximation, n, f, Df));
          
 printf("  sqrt(%.1f) = %.15f \n\n", a, sqrt(a));

 stop();

 return 0;
}


Pour encadrer la racine de l'équation x**2 - 7.0 = 0, il suffit de voir que 2**2 = 4 < 7 et 3**2 = 9 > 7, on va donc choisir comme première approximation 2.5. On arrêtera les calculs lorsque l'on aura deux valeurs identiques.


Exemple de sortie écran 1 :

 Use Newton's method to approximate sqrt(7.0).        

 This is equivalent to find the positive real root of :

 f : x-> x**2 - 7.0

 You know that the positive real root of f is between 2.0 and 3.0
           (2.0**2 = 4)     (3.0**2 = 9)

 Choose x = 2.5 as a first approximation.

 Press return to continue.


Exemple de sortie écran 2 :

 As a first approximation x = 2.5 

 The Newton's method give :        

 x[1] = 2.500000000000000
 x[2] = 2.650000000000000
 x[3] = 2.645754716981132
 x[4] = 2.645751311066783
 x[5] = 2.645751311064591
 x[6] = 2.645751311064591
 

 The Newton's method give : 

          x = 2.645751311064591

  sqrt(7.0) = 2.645751311064591 

 Press return to continue.
Cet article est issu de Wikibooks. Le texte est sous licence Creative Commons - Attribution - Partage dans les Mêmes. Des conditions supplémentaires peuvent s'appliquer aux fichiers multimédias.