1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <stdio.h>
#include <math.h>
float maFonc( float x) {
return 0.4*x*x*x - x*x + 0.7 ;
}
float deriv( float x)
{
return 1.2*x*x + 2*x ;
}
float newton ( float Fonc( float ), float Deriv(float ), float x0, float eps, float eps2, int MaxIter )
{
float d, y0, x1,y ;
int nbIter=0;
y0=Fonc(x0);
d=Deriv(x0);
while( fabs(y0) > eps && nbIter < MaxIter )
{
if (fabs(d)<eps2)
{
printf("\n derive proche de zero: %10.7f \n" , d );
exit (-1);
}
if (y==0) return x1;
x1 -= y0/d;
y0= Fonc(x1);
d=Deriv(x1);
nbIter++;
} return 1;
}
float dichotomie( float Fonc( float ), float a, float b, float eps, int MaxIter )
{
float x, y;
int nbIter=0;
x=(a+b)/2;
y=Fonc(x);
while( fabs(y) > eps && nbIter < MaxIter )
{
if (y==0) return x;
if (y * Fonc(a)< 0) b= x;
else a=x; x=(a+b)/2;
y=Fonc(x); nbIter++;
}
return x;
} |
Partager