Aller au contenu

Mathc initiation/Fichiers c : c22cb

Un livre de Wikilivres.


Sommaire


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

c3b.c
/* --------------------------------- */
/* save as c3b.c                     */
/* --------------------------------- */
#include "x_hfile.h"
#include      "fb.h"
/* --------------------------------- */
int main(void)
{
   int                  n = 6;
double                  a = 5.0;
double FirstApproximation = 2.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 2.0 and 3.0\n\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(" 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 [-4.:4] [-10.:10.]\
         x**2 - 5.0
reset
# ---------------------


Pour encadrer la racine de l'équation x**2 - 5.0 = 0, il suffit de voir que 2**2 = 4 < 5 et 3**2 = 9 > 5, 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 :

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





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

 This is equivalent to find the positive real root of :

 f : x-> x**2 - 5.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. 


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


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



 As a first approximation x = 2.5 

 The Newton's method give :        

 x[1] = 2.500000000000000
 x[2] = 2.250000000000000
 x[3] = 2.236111111111111
 x[4] = 2.236067977915804
 x[5] = 2.236067977499790
 x[6] = 2.236067977499790


          x = 2.236067977499790
  sqrt(5.0) = 2.236067977499790

 Press return to continue.