Demo entry 6684838
Dichotomie
Submitted by anonymous
on Dec 17, 2017 at 19:48
Language: C. Code size: 1.4 kB.
#include<stdio.h> #include<math.h> float f(float x) { return cos(x-5)+(x-5)*exp(0.1*x)+2 /*la fonction*/ } void dichot (float *x, float x0, float x1, int *itr) { *x = (x0+x1)/2; printf("L'iteration no. %1d X = %7.5f \n", *itr, *x); } void main () { int maxitr; float x0,x1,x2,x3,err,itr=0;; /* itr – le nombre de fois de l'iteration maxitr – le nomber maximal de l'iteration x0, x1 – le champ ou on peut trouver la racine x2 – la valeur de racine a n ieme iteration x3 – la valeur de racine a (n + 1) ieme iteration err – l'erreur acceptable x – la valeur de la racine à la n ième itération dans la fonction regula f(x0), f(x1) – la valeur de f(x0) et f(x1) */ printf("\nSaisir les valeur x0, x1, erreur acceptable et l'iterations maximals:\n"); scanf("%f %f %f %d", &x0, &x1, &err, &maxitr); itr++; dichot (&x2, x0, x1, &itr); do { /*reduire le champ*/ if (f(x0)*f(x2) < 0) x1=x2; else x0=x2; /*appiquer la methode dichotomie*/ itr++; dichot (&x3, x0, x1,&itr); if (fabs(x3-x2) <= err) //Trouver si |x3-x2|<erreur { printf("Apres %d iterations, on trouve la racine = %6.4f\n", itr, x3); return; } x2=x3; } while (itr<maxitr); printf("La solution n'est pas converge ou il y a trop d'iterations.\n"); }
This snippet took 0.01 seconds to highlight.
Back to the Entry List or Home.