Mathc initiation/Fichiers c : c22ce
Installer et compiler ces fichiers dans votre répertoire de travail.
c3e.c |
---|
/* --------------------------------- */
/* save as c3e.c */
/* --------------------------------- */
#include "x_hfile.h"
#include "fe.h"
/* --------------------------------- */
int main(void)
{
int n = 6;
double FirstApproximation = 0.8;
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 intersection point of :\n\n"
" g : x-> %s and\n"
" h : x-> %s\n\n\n"
" On a graph, you can see that, the intersection \n"
" point is between 0.0 and 1.0.\n\n"
" Choose x = %.1f as a first approximation.\n\n"
, geq, heq, FirstApproximation);
stop();
clrscrn();
printf(" In fact we want find x = cos(x) or x - cos(x) = 0.\n\n"
" We want find the root of\n\n"
" f : x-> %s\n\n", feq);
printf(" As a first approximation x = %.1f \n\n",FirstApproximation);
printf(" The Newton's method give : \n\n");
p_Newton_s_Method(FirstApproximation, n, f, Df);
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 [-3.:3.] [-1.5:1.5]\
cos(x),\
x
reset
# ---------------------
Nous pouvons observer que le point d'intersection est entre 0 et 1. Il faut calculer la racine de cette équation x - cos(x) = 0. On va donc choisir comme première approximation 0.8. 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 intersection point of :
g : x-> x and
h : x-> cos(x)
On a graph, you can see that, the intersection
point is between 0.0 and 1.0.
Choose x = 0.8 as a first approximation.
Press return to continue.
**************************
In fact we want find x = cos(x) or x - cos(x) = 0.
We want find the root of
f : x-> x- cos(x)
As a first approximation x = 0.8
The Newton's method give :
x[1] = 0.800000000000000
x[2] = 0.739853306370066
x[3] = 0.739085263405232
x[4] = 0.739085133215164
x[5] = 0.739085133215161
x[6] = 0.739085133215161
f(0.739085133215161) = 0.000000000000000
Press return to continue.