Académique Documents
Professionnel Documents
Culture Documents
Dalah Mohamed
ENPC-Constantine 2016
Chapitre 2:
Résolution d’Equations non-Linéaires
E-mail dalah.mohamed@yahoo.fr
Année 2016/2017
ENPC-Constantine 2016
Chapitre 2: Résolution d’Equations non-Linéaires
(Cours : 06h00, TD : 04h30)
Le Contenu: Partie 2:
2016
ENPC-Constantine
•
3 MD "Calcul Scientifique"
Partie 2.1:
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:
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:
Résultats Numériques:
%**************************************************************************
result =
%**************************************************************************
Programme en Matlab 2:
Programme Matlab 2:
Résultats Numériques:
%**************************************************************************
result =
%**************************************************************************
Programme en Matlab 3:
Programme Matlab 3:
Résultats Numériques:
%**************************************************************************
0 2 2
%**************************************************************************
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
%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
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
60