Aller au contenu

Mathc initiation/Fichiers c : c22cd3

Un livre de Wikilivres.


Sommaire


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


c3d3.c
/* --------------------------------- */
/* save as c3d3.c                    */
/* --------------------------------- */
#include "x_hfile.h"
#include      "fd.h"
/* --------------------------------- */
int main(void)
{
   int n = 8;

double FirstApproximation = -1.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 \n" 
        " the smallest real root of :\n\n" 
        " f : x-> %s\n\n\n" 
        " On a graph of f, you can see that, the smallest\n" 
        " real root of f is between -2.0 and -1.0.\n\n" 
        " Choose x = %.1f as a first approximation.\n\n", 
          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(" f(%.15f) = %.15f\n\n",  
              Newton_s_Method(FirstApproximation, n, f, Df)
           ,f(Newton_s_Method(FirstApproximation, n, f, Df)));
 stop();

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


Fichier de commande gnuplot :
# ---------------------
# Copy and past this file into the screen of gnuplot
#
#
set zeroaxis lt 3 lw 1
plot [-2.5:2.5] [-6.:6.]\
         x**3 - 3.0*x + 1.0
reset
# ---------------------


Nous pouvons observer que la plus petite racine est comprise entre -2 et -1. Il faut calculer la racine de cette équation x**3 - 3.0*x + 1.0 = 0. On va donc choisir comme première approximation -1.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 
 the smallest real root of :

 f : x-> x**3 - 3.0*x + 1.0


 On a graph of f, you can see that, the smallest
 real root of f is between -2.0 and -1.0.

 Choose x = -1.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 = -1.5 

 The Newton's method give :        

 x[1] = -1.500000000000000
 x[2] = -2.066666666666666
 x[3] = -1.900875603864734
 x[4] = -1.879719904423836
 x[5] = -1.879385324669270
 x[6] = -1.879385241571822
 x[7] = -1.879385241571817
 x[8] = -1.879385241571817


 f(-1.879385241571817) = -0.000000000000000

 Press return to continue.