Mathc initiation/Fichiers c : c22ca
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(" 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 - 7.0
reset
# ---------------------
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 :
The Newton's method :
f(x_n)
x_n+1 = x_n - -------
f'(x_n)
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.
*****************************
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.650000000000000
x[3] = 2.645754716981132
x[4] = 2.645751311066783
x[5] = 2.645751311064591
x[6] = 2.645751311064591
x = 2.645751311064591
sqrt(7.0) = 2.645751311064591
Press return to continue.