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 45 46 47 48 49 50
|
function [r, fr, compt]=Dichotomie(f, a, b, e, Nmax)
% [r, fr, compt]=Dichotomie(f, a, b, e, Nmax)
% Dichotomie function
if nargin < 5
Nmax = 100;
end
if nargin < 4
e = 1.0e-8; % standard
end
if nargin < 3
error('Nombre d''arguments insuffisant!')
end
if f(a)*f(b) > 0
error('Condition : f(a)*f(b) < 0 insatisfaite!!')
end
if a >= b
error('Condition : a < b insatisfaite!!')
end
compt=1;
fr = 2*e;
% Boucle principale
while compt < Nmax && fr > e
if f(a) == 0
r = a;
fr = b-a;
break
elseif f(b) == 0
r = b;
fr = b-a;
break
elseif f(a)*f((a+b)/2) <= 0
b = (a+b)/2;
else
a = (a+b)/2;
end
r = (a+b)/2;
fr = b-a;
compt=compt+1;
end
fprintf('La racine de l''équation est: %f obtenue aprés %f itérations\n ',r,compt); |
Partager