

%Algorithme bisection  Premiere version
function  [c,nit]= dicho(f,a,b)
% dicho calcule un z�ro de la fonction f dans l'intervalle [a,b]
% au moyen de la m�thode de dichotomie
% juqu'� la pr�cision machine
c=(a+b)/2;
nit=0;
while c>a & c<b,
  if feval(f,a)*feval(f,c) > 0,%alors la racine est dans ]c,b[
    a=c;
  else
    b=c;
  end;
c=(a+b)/2;
nit=nit+1;
end


% exemple 
f=inline('x/8*(63*x^4-70*x^2+15)')
f=inline('exp(x*tan(x))-4')
fplot(f,[-1 1])
[c,n]=dicho(f,0,1)

% exemple 
f=inline('10^(-7)*x^3+x^2-1')
fplot(f,[-1 2])
%Algorithme bisection � Deuxi�me version

function [x,y]=Bisection(f,a,b,tol)
% BISECTION computes a zero of a scalar function
% [x,y]=Bisection(f,a,b,tol) solves the nonlinear scalar equation
% f(x)=0 in the interval [a,b] using bisection, up to the given
% tolerance tol. The result x ist the approximate solution, and y
% the function value at x.
fa=f(a); v=1; if fa>0, v=-1; end;
if fa*f(b)>0
error('f(a) and f(b) have the same sign')
end
if (nargin<4), tol=0; end;
x=(a+b)/2;
while b-a>tol & (a<x & x<b)
if v*f(x)>0, b=x; else a=x; end;
x=(a+b)/2;
end
y=f(x);

[x,y]=Bisection(inline('x+exp(x)'),-1,0)
[x,y]=Bisection(f,0.6,1)

%point fixe pour xe^x=1
X=linspace(-1,10,1000);
Y=X.*exp(X)-1;
plot(X,Y)
% d'abord x=F(x)=e^(-x) 
x(1)=0.5
N=20
for j=1:N
    x(j+1)=exp(-x(j));
end
plot((1:N+1),x,'r')
hold on
% ensuite F(x)=x-xe^x+1
y(1)=0.5
N=20
for j=1:N
    y(j+1)=y(j)-y(j)*exp(y(j))+1;
end
plot((1:N+1),y,'b')
hold on
% ensuite F(x)=x+xe^x-1
z(1)=0.5
N=20
for j=1:N
    z(j+1)=z(j)+z(j)*exp(z(j))-1;
end
plot((1:N+1),z,'g')

% ensuite Newton
x(1)=0.5
N=20
for j=1:N
    x(j+1)=x(j)-(x(j)*exp(x(j))-1)/((x(j)+1)*exp(x(j)));
end
J=(1:N+1);
[J'-1 x']
% methode de la secante
x(1)=0.5; x(2)=1;
N=20
for j=2:N
    xj=x(j);xj1=x(j-1);
    fj=xj-exp(-xj);
    fj1=xj1-exp(-xj1);
    x(j+1)=xj-fj*(xj-xj1)/(fj-fj1);
end
J=(1:N+1);
[J'-1 x']


