Vous êtes sur la page 1sur 60

Prof.

Dalah Mohamed
ENPC-Constantine 2016

Chapitre 2:
Résolution d’Equations non-Linéaires

Presented by: Dalah Mohamed


Department of Mathematics
Faculty of Exact Sciences
University of Constantine 1, Algeria

E-mail dalah.mohamed@yahoo.fr

Année 2016/2017

10 = X ‫الحرف‬ 5= V‫الحرف‬ 1 = I ‫الحرف‬


© Copyright 2016 Cour .TD.TP

ENPC-Constantine 2016
Chapitre 2: Résolution d’Equations non-Linéaires
(Cours : 06h00, TD : 04h30)

Le Contenu: Partie 2:

2.3. Méthodes Itératives :


Méthode de Point Fixe
Méthode de Newton
Ordre de Convergence
Critères d’Arrêts.
Partie 2:

2.3. Méthodes Itératives :


2.3.1.Méthode de Point Fixe
2.3.2.Méthode de Newton
2.3.3.Ordre de Convergence

2016
ENPC-Constantine

3 MD "Calcul Scientifique"
Partie 2.1:

La Méthode de Point Fixe

2016
ENPC-Constantine

4 MD "Calcul Scientifique"
2.3.1.
2.3.1.
2.3.1.
© Copyright 2016 Cour .TD.TP
Remarque:
2.3.1.
2.3.1.
2.3.1.
TP 1: Programme en Matlab 1:

Prgramme Matlab 1:

function fixed(g,x0,tol,n)
iter=0;
u=feval(g,x0);
err=abs(u-x0);
disp('---------------------------------------------------------------------')
disp('iter a g(x) |xn+1 - xn|')
disp('---------------------------------------------------------------------')
while(err>tol)&(iter<=n)
x1=u;
err=abs(x1-x0);
x0=x1;
u=feval(g,x0);
iter=iter+1;
fprintf('%2.0f %18.6f %19.6f %20.8f\n',iter,x0,u,err)
end;
if(iter>n)
disp('Method failed to converge')
end;
TP 1: Programme en Matlab 1:

Résultats Numériques:

>>**************************************************************
>> g=inline('5./x.^2+2')
g =
Inline function:
g(x) = 5./x.^2+2
>> x0=2.5;
>> tol=10e-04;
>> n=40;
>> fixed(g,x0,tol,n)
------------------------------------------------------
iter a g(x) |xn+1 - xn|
------------------------------------------------------
1 2.800000 2.637755 0.30000000
2 2.637755 2.718623 0.16224490
3 2.718623 2.676507 0.08086781
4 2.676507 2.697965 0.04211628
5 2.697965 2.686906 0.02145790
6 2.686906 2.692572 0.01105819
7 2.692572 2.689660 0.00566568
8 2.689660 2.691154 0.00291154
9 2.691154 2.690387 0.00149391
10 2.690387 2.690781 0.00076713
>>**************************************************************
TP 1: Programme en Matlab 1:

Résultats Numériques:

>>**************************************************************

>> x=1.5:0.001:2.7;
>> g=inline('5./x.^2+2')
g =
Inline function:
g(x) = 5./x.^2+2
>>plot(x,f(x),’k’)
>> hold on
>> y1=x;
>> plot(x,y1,’r’)
>> grid
>> xlabel('x')
>> ylabel('Les images')
>> legend('y=f(x)')
>> title('Graphe de la fonction y=f(x)')

>>**************************************************************
TP 2: A la Maison:
TP :
ENPC-Constantine 2016
2.3.1.
2.3.1.
Partie 2.2:

La Méthode de NEWTON

2016
ENPC-Constantine

20 MD "Calcul Scientifique"
2.3.2.Méthode de Newton
2.3.2.Méthode de Newton
ENPC-Constantine 2016
Ou
TP 1: Programme en Matlab 1-a:

Prgramme Matlab 1-a:

function x = mynewton (f,f1,x0,n)


% Solves f(x) = 0 by doing n steps of Newton ’s method starting at x0.
© Copyright 2016 Cour .TD.TP

% Inputs : f -- the function , input as an inline


% f1 -- it ’s derivative , input as an inline
% x0 -- starting guess , a number
% n -- the number of steps to do
% Output : x -- the approximate solution
format long % prints more digits
x = x0; % set x equal to the initial guess x0
for i = 1:n % Do n times
x = x - f(x)/ f1(x) % Newton ’s formula , prints x too
end
TP 1: Programme en Matlab 1-a:
TP 2: Programme en Matlab 2-b:

Prgramme Matlab 2-b:

function x = mynewton (f,f1 ,x0 ,n,tol )


% Solves f(x) = 0 by doing n steps of Newton ’s method starting at x0.
% Inputs : f -- the function , input as an inline
% f1 -- it ’s derivative , input as an inline
% x0 -- starting guess , a number
% tol -- desired tolerance , prints a warning if |f(x)|> tol
% Output : x -- the approximate solution
x = x0; % set x equal to the initial guess x0
for i = 1:n % Do n times
x = x - f(x)/ f1(x) % Newton ’s formula
end
r = abs (f(x))
if r > tol
warning (’The desired accuracy was not attained ’)
end
TP 2: Programme en Matlab 2-b:
TP 3: Programme en Matlab 3-c:

Prgramme Matlab 3-c:

function x = mynewtontol (f,f1 ,x0 , tol )


x = x0; % set x equal to the initial guess x0
y = f(x);
while abs (y) > tol % Do until the tolerence is reached .
x = x - y/f1(x) % Newton ’s formula
y = f(x)
end
TP 3: Programme en Matlab 3-c:
TP 4: Programme en Matlab 4-d:

Prgramme Matlab 4-d:

function x = mynewtontol (f,f1 ,x0 , tol )


x = x0; % set x equal to the initial guess x0.
i =0; % set counter to zero
© Copyright 2016 Cour .TD.TP

y = f(x);
while abs (y) > tol & i < 1000
% Do until the tolerence is reached or max iter .
x = x - y/f1(x) % Newton ’s formula
y = f(x)
i = i +1; % increment counter
end
TP 5: Programme en Matlab 5-e:********************************
Prgramme Matlab 5-d:

function newton(f,df,x0,tol,n)
iter=0;
u=feval(f,x0);
v=feval(df,x0);
err=abs(u/v);
disp('--------------------------------------------------------------------------')
disp('iter xn f(xn) df(xn) |xn+1 - xn|')
disp('--------------------------------------------------------------------------')
fprintf('%2.0f %12.6f %12.6f %12.6f %12.6f\n',iter,x0,u,v,err)
while(err>tol)&(iter<=n)&((v-u)~=0)
x1=x0-u/v;
err=abs(x1-x0);
x0=x1;
u=feval(f,x0);
v=feval(df,x0);
iter=iter+1;
fprintf('%2.0f %12.6f %12.6f %12.6f %12.6f\n',iter,x0,u,v,err)
end;
if(v==0);
disp('Division by Zero')
end;
if(iter>n)
disp('Method failed to Converge')
end
TP 5: Programme en Matlab 5:********************************
Résultats Numériques:
%**************************************************************************
>>x0=1;
>>n=40;
>>tol=10e-04;
>>f=inline('x.^3-x.^2-1')

f =

Inline function:
f(x) = x.^3-x.^2-1

>>df=inline('3*x.^2-2*x')

df =

Inline function:
df(x) = 3*x.^2-2*x

>>newton(f,df,x0,tol,n)
--------------------------------------------------------------------------
iter xn f(xn) df(xn) |xn+1 - xn|
--------------------------------------------------------------------------
0 1.000000 -1.000000 1.000000 1.000000
1 2.000000 3.000000 8.000000 1.000000
2 1.625000 0.650391 4.671875 0.375000
3 1.485786 0.072402 3.651108 0.139214
4 1.465956 0.001352 3.515168 0.019830
5 1.465571 0.000001 3.512556 0.000385
%**************************************************************************
TP 5: Programme en Matlab 5:********************************
>>roots([1 -1 0 -1])

ans =

1.4656
-0.2328 + 0.7926i
-0.2328 - 0.7926i

>>fzero('x.^3-x.^2-1',1)

ans =

1.4656
>>p=[1 -1 0 -1]

p =

1 -1 0 -1

>>polyval(p,0)

ans =

-1
TP :
TP :
Annexes de Quelques
Programmes Supplémentaires
Programme en Matlab 1:
Programme Matlab 1:

>> % Newton iteration (quadratic convergence)


% See example 5.10 on p.230 of Heath
% K. Ming Leung, 02/13/03

clear all; format long;


tol=eps; % set tolerance to machine epsilon
x=3; % initial guess for the root
dx=1; % change in x
result=[]; % to store all intermediate results
loop=1; % loop count
while abs(dx)> tol;
fx=x*x-4*sin(x); % function of interest
fp=2*x-4*cos(x); % derivative of the function
dx=-fx/fp; % change in x
result=[result; [loop x fx fp dx]];
x=x+dx; % the next value of x
loop=loop+1; % increment loop counter
end;
display(result); % display final result
Programme en Matlab 1:

Résultats Numériques:

%**************************************************************************
result =

1.000000000000000 3.000000000000000 8.435519967760531 9.959969986401781 -0.846942307986614


2.000000000000000 2.153057692013386 1.294772505286566 6.505771709980720 -0.199019050007582
3.000000000000000 1.954038642005804 0.108438553394624 5.403795435780019 -0.020067109253733
4.000000000000000 1.933971532752070 0.001151631523864 5.288919536106083 -0.000217744194443
5.000000000000000 1.933753788557627 0.000000136054946 5.287669847970434 -0.000000025730605
6.000000000000000 1.933753762827022 0.000000000000002 5.287669700292105 -0.000000000000000
7.000000000000000 1.933753762827021 -0.000000000000000 5.287669700292102 0.000000000000000

%**************************************************************************
Programme en Matlab 2:
Programme Matlab 2:

>> % Newton iteration (quadratic convergence)


% See example 5.10 on p.230 of Heath
% K. Ming Leung, 02/13/03

clear all; format long;


tol=eps; % set tolerance to machine epsilon
x=3; % initial guess for the root
dx=1; % change in x
result=[]; % to store all intermediate results
loop=0; % loop count
while abs(dx)> tol;
fx=x*x-4*sin(x); % function of interest
fp=2*x-4*cos(x); % derivative of the function
dx=-fx/fp; % change in x
result=[result; [loop x fx fp dx]];
x=x+dx; % the next value of x
loop=loop+1; % increment loop counter
end;
display(result); % display final result
Programme en Matlab 2:

Résultats Numériques:

%**************************************************************************
result =

0 3.000000000000000 8.435519967760531 9.959969986401781 -0.846942307986614


1.000000000000000 2.153057692013386 1.294772505286566 6.505771709980720 -0.199019050007582
2.000000000000000 1.954038642005804 0.108438553394624 5.403795435780019 -0.020067109253733
3.000000000000000 1.933971532752070 0.001151631523864 5.288919536106083 -0.000217744194443
4.000000000000000 1.933753788557627 0.000000136054946 5.287669847970434 -0.000000025730605
5.000000000000000 1.933753762827022 0.000000000000002 5.287669700292105 -0.000000000000000
6.000000000000000 1.933753762827021 -0.000000000000000 5.287669700292102 0.000000000000000

%**************************************************************************
Programme en Matlab 3:

Programme Matlab 3:

>> % Newton iteration near a double root


% newton2.m
% function 1: f1(x)=x^2-1 simple roots at x=1 and -1
% function 2: f2(x)=x^2-2*x+1 double root at x=1
% initial guess: x0=3
% tolerance: tol=1e-6

clear all; format long; tol=1e-6;


x1=2; x2=x1; loop=0;
disp([loop x1 x2]);
for loop=1:5
f1x=x1*x1-1; f2x=x2*x2-2*x2+1;
f1p=2*x1; f2p=2*x2-2;
x1=x1-f1x/f1p; x2=x2-f2x/f2p;
disp([loop x1 x2]);
end;
Programme en Matlab 3:

Résultats Numériques:

%**************************************************************************
0 2 2

1.000000000000000 1.250000000000000 1.500000000000000

2.000000000000000 1.025000000000000 1.250000000000000

3.000000000000000 1.000304878048780 1.125000000000000

4.000000000000000 1.000000046461147 1.062500000000000

5.000000000000000 1.000000000000001 1.031250000000000

%**************************************************************************
Programme en Matlab 4:
Programme Matlab 4:
function x = NewtonD(F,JF,x,tol,maxIt)
% D-dimensional Newton'n method to
% solve the system F(x) = 0
% F(x)=[F1(x); F2(x); ... ; Fn(x)]
% x = [ x1; x2; ... ; xn]
% JF is the Jacobian matrix of F at x
% x is the initial guess
% tol is the tolerance
% maxIt is the maximum number of iterations
% Usage: x = NewtonD('fcnEg5p15','fcnJEg5p15',[1;2],1e-6,20)
% K. Ming Leung, 01/03/03

disp([0 x']);
iter=1; dif=10;
while (dif > tol & iter <=maxIt)
dx = -feval(JF,x) \ feval(F,x);
dif = norm(dx);
x = x + dx;
disp([ iter x' dif]);
iter = iter + 1;
end;
if iter > maxIt
disp('Maximum iteration exceeded without convergence!');
else
disp('Newton method has converged.');
end

function f = fcnEg5p15(x)
f = [(x(1)+2*x(2)-2); (x(1)^2+4*x(2)^2-4)];

function Jf = fcnJEg5p15(x)
Jf = [1 2; 2*x(1) 8*x(2)];

function f = orbitCross(x)
f = [ (3*x(1)^2 + 4*x(2)^2 -3)
(x(1)^2 + x(2)^2 - sqrt(3)/2)];

function df = orbitCrossJ(x)
df = [6*x(1) 8*x(2); 2*x(1) 2*x(2)];
Programme en Matlab 5:
Programme Matlab 5:
>> function x = NewtonD(F,JF,x,tol,maxIt)
% D-dimensional Newton'n method to
% solve the system F(x) = 0
% F(x)=[F1(x); F2(x); ... ; Fn(x)]
% x = [ x1; x2; ... ; xn]
% JF is the Jacobian matrix of F at x
% x is the initial guess
% tol is the tolerance
% maxIt is the maximum number of iterations
% Usage: x = NewtonD('fcnEg5p15','fcnJEg5p15',[1;2],1e-6,20)
% K. Ming Leung, 01/03/03

%disp([0 x']);
iter=1; dif=10;
while (dif > tol & iter <=maxIt)
dx = -feval(JF,x) \ feval(F,x);
dif = norm(dx);
x = x + dx;
% disp([ iter x' dif]);
iter = iter + 1;
end;
if iter > maxIt
disp('Maximum iteration exceeded without convergence!');
else
disp('Newton method has converged.');
end
Programme en Matlab 6:
Programme Matlab 6:
>> function x = NewtonD04(F,JF,x,tol,maxIt,varargin)
% D-dimensional Newton'n method to
% solve the system F(x) = 0
% F(x)=[F1(x); F2(x); ... ; Fn(x)]
% x = [ x1; x2; ... ; xn]
% JF is the Jacobian matrix of F at x
% x is the initial guess
% tol is the tolerance
% maxIt is the maximum number of iterations
% Usage: x = NewtonD04('fcnEg5p15','fcnJEg5p15',[1;2],1e-6,20,...)
% where ... represents additional parameters for the function and the
% Jacobian matrix (assume the same set of parameters).
% K. Ming Leung, 02/08/04

%disp([0 x']);
iter=1; dif=10;
while (dif > tol & iter <=maxIt)
dx = -feval(JF,x,varargin{:}) \ feval(F,x,varargin{:});
dif = norm(dx);
x = x + dx;
% disp([ iter x' dif]);
iter = iter + 1;
end;
if iter > maxIt
disp('Maximum iteration exceeded without convergence!');
else
disp('Newton method has converged.');
end
Programme en Matlab 7:
Programme Matlab 7:
>> % Newton iteration near a simple root & a double root
% Convergence is quadratic for simple root
% Convergence is linear for double root
% See example 5.11 on p.231 of Heath
% NewtonDoubleRoot.m
% function 1 has simple roots at x=1 & -1: f1(x)=x^2-1
% function 2 has double root at x=1: f2(x)=x^2-2*x+1
% initial guess: x0=3
% tolerance: tol=1e-6
% K. Ming Leung, 02/13/03

clear all; format long; tol=1e-6;


x1=2; x2=x1; loop=0;
disp([loop x1 x2]);
for loop=1:5
f1x=x1*x1-1; f2x=x2*x2-2*x2+1;
f1p=2*x1; f2p=2*x2-2;
x1=x1-f1x/f1p; x2=x2-f2x/f2p;
disp([loop x1 x2]);
end;
Programme en Matlab 8:
Programme Matlab 8:
>> % Newton iteration near a simple root & a double root
% Convergence is quadratic for simple root
% Convergence is linear for double root
% See example 5.11 on p.231 of Heath
% NewtonDoubleRoot.m
% function 1 has simple roots at x=1 & -1: f1(x)=x^2-1
% function 2 has double root at x=1: f2(x)=x^2-2*x+1
% initial guess: x0=3
% tolerance: tol=1e-6
% K. Ming Leung, 02/13/03

clear all; format long; tol=1e-6;


x1=4.1; x2=0.4; loop=0;
disp([loop x1 x2]);
for loop=1:5
f1x=x1*x1-1; f2x=sin(x2*x2-2*x2+1);
f1p=2*x1; f2p=cos(x2*x2-2*x2+1)*(2*x2-2);
x1=x1-f1x/f1p; x2=x2-2*f2x/f2p;
disp([loop x1 x2]);
end;
Programme en Matlab 9:
Programme Matlab 9:
>> function x = NewtonDt(F,JF,x,tol,maxIt)
% D-dimensional Newton'n method to
% solve the system F(x) = 0
% F(x)=[F1(x); F2(x); ... ; Fn(x)]
% x = [ x1; x2; ... ; xn]
% JF is the Jacobian matrix of F at x
% x is the initial guess
% tol is the tolerance
% maxIt is the maximum number of iterations
% Usage: x = NewtonD('fcnEg5p15','fcnJEg5p15',[1;2],1e-6,20)
% K. Ming Leung, 01/03/03

%disp([0 x']);
iter=1; dif=10;
while (dif > tol & iter <=maxIt)
dx = -feval(JF,x) \ feval(F,x);
dif = norm(dx);
x = x + dx;
% disp([ iter x' dif]);
iter = iter + 1;
end;

function f = fcnEg5p15(x)
f = [(x(1)+2*x(2)-2); (x(1)^2+4*x(2)^2-4)];

function Jf = fcnJEg5p15(x)
Jf = [1 2; 2*x(1) 8*x(2)];

function f = orbitCross(x)
f = [ (3*x(1)^2 + 4*x(2)^2 -3)
(x(1)^2 + x(2)^2 - sqrt(3)/2)];

function df = orbitCrossJ(x)
df = [6*x(1) 8*x(2); 2*x(1) 2*x(2)];
Programme en Matlab 10:
Programme Matlab 10:
>> function NewtonF1(x,fcn,tol)
% Newton iteration in 1D

dx=1; result=[]; loop=0;


while abs(dx)> tol;
[fx, fp]=feval(fcn,x);
% fcn returns the function & it's derivative at x
% fx=x*x-4*sin(x);fp=2*x-4*cos(x);
dx=-fx/fp;
result=[result; [loop x fx fp dx]];
x=x+dx;
loop=loop+1;
end;
Programme en Matlab 11:
Programme Matlab 11:
>> % Compute the reciprocal of a given number w/o division
% Uses Newton-Raphson method
% NewtonReciprocation

a=6.7; pt5=0.5; h=1; one=1;


sgn=sign(a); a=abs(a);
if a < pt5
isSmall=1; h=1; y=1;
while a < h
h=pt5*h, y=2*y;
end
elseif a > one
isSmall=0; h=1; y=1;
while a > h
h=2*h, y=pt5*y,
end
else
h=1;
end
a=a*y, h,
x=pt5*a;
Programme en Matlab 12:
Programme Matlab 12:
>> function inverse = NewtonReciprocation1(a)
% Compute the reciprocal of a given nonzero number w/o division
% Uses Newton-Raphson method
% NewtonReciprocation1.m
% Usage: inverse = NewtonReciprocation1(a)
% K. Ming Leung, 02/24/03

pt5=0.5; one=1; tol=eps; maxIt=50; it=0;


% Take out the sign of a so that it is always positive
sgn=sign(a); a=abs(a);

% Scale a until it lies between 0.5 & 1


% Should really be done by bit shifting
s=1;
if a < pt5
while a < pt5
s=2*s; a=2*a;
end
elseif a > one
while a > one
s=pt5*s; a=pt5*a;
end
end
a,
% Start Newton-Raphson iteration with initial guess:
%x=1.5 % pick the arith. mean of the interval
%x=pt5*a, % Crandall's initial guess (10 iterations for the worst case
if a >= 0.66666
x=2-a; % My initial guess (6 iterations for the worst case)
else
x=4*(1-a)
end

x=1.333333
%x=(a-3)*a+3,
dx=x*(1-a*x);
while abs(dx) > tol & it < maxIt
x=x+dx;
dx=x*(1-a*x);
it=it+1; [it x],
end
% Put back the sign and the overall scale factor
inverse=sgn*s*x;
ENPC-Constantine 2016
Références
Prof. Dalah Mohamed

Thank You For Your Attention

60

Vous aimerez peut-être aussi