Aller au contenu

Mathc initiation/Fichiers c : c22cc

Un livre de Wikilivres.


Sommaire


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

c3c.c
/* --------------------------------- */
/* save as c3c.c                     */
/* --------------------------------- */
#include "x_hfile.h"
#include      "fc.h"
/* --------------------------------- */
int main(void)
{
   int                  n =  6;
double                  a = 11.0;
double FirstApproximation =  3.5;

 clrscrn();
 
  printf(" The Newton's method :     \n"
        "                     f(x_n) \n"  
        "     x_n+1 = x_n  - ------- \n"
        "                    f'(x_n) \n"
        "\n\n\n\n\n");
        
 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 3.0 and 4.0\n" 
        "           (3.0**2 = 9)     (4.0**2 = 16)\n\n" 
        " Choose x = %.1f as a first approximation.\n\n",
         a, feq, FirstApproximation);
 stop();

 clrscrn();
 
  printf(" The Newton's method :     \n"
        "                     f(x_n) \n"  
        "     x_n+1 = x_n  - ------- \n"
        "                    f'(x_n) \n"
        "\n\n\n");
        
 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("           x = %.15f\n",
          Newton_s_Method(FirstApproximation, n, f, Df));
          
 printf("  sqrt(%.1f) = %.15f\n\n", a, sqrt(a));

 stop();

 return 0;
}
/* ---------------------------------- */
/* ---------------------------------- */


Fichier de commande gnuplot :
# ---------------------
# Copy and past this file into the screen of gnuplot
#
#
set zeroaxis lt 3 lw 1
plot [-0.:5] [-10.:10.]\
         x**2 - 11.0
reset
# ---------------------


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


Exemple de sortie écran :

 The Newton's method :     
                     f(x_n) 
     x_n+1 = x_n  - ------- 
                    f'(x_n) 





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

 This is equivalent to find the positive real root of :

 f : x-> x**2 - 11.0

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

 Choose x = 3.5 as a first approximation.

 Press return to continue. 


***********************


 The Newton's method :     
                     f(x_n) 
     x_n+1 = x_n  - ------- 
                    f'(x_n) 



 As a first approximation x = 3.5 

 The Newton's method give :        

 x[1] = 3.500000000000000
 x[2] = 3.321428571428572
 x[3] = 3.316628264208909
 x[4] = 3.316624790357219
 x[5] = 3.316624790355400
 x[6] = 3.316624790355400


           x = 3.316624790355400
  sqrt(11.0) = 3.316624790355400

 Press return to continue.