[Optimization] Newton's method instance

0. Introduction

      The one already described some of the principles of the Newton method, for specific example In this section, the minimum value of the function by using Newton's method to solve.

1. Examples

  Minimum solving the following functions:

 

 

 

   Since this function is relatively simple, so the use of f x, y are partial derivatives find, then make partial derivatives equal 0, the extreme points can be obtained, should the function is convex (if not the analysis, visualization functions, as FIG. ), the minimum point is the extreme point, so minimum point (-1,1.5). here, then, the minimum value of the function for solving Newton iterative method.

 

 

step:

 

          

 

         

 2. Implementation Details

Reference [1] Part %% 
% Objective function : F (A, B) = 2 * A ^ 2 + B ^ 2 + 2 * A * B + A - B 
Clear; CLC; Close 
A = [ . 4 , 2 ; 2 , 2 ]; 
X = [ 2 ; - 2 ]; 
tmp = [ 0 ; 0 ]; 
B = [ 0 ; 0 ]; 
Delta = 1.0E-. 8 ; iterative difference between the two front% 
error = . 1 ; 
K = . 1;
max_iters = 10000;
history = zeros(max_iters,2);

while(k<=max_iters && error > delta)
b=[4*x(1,1)+2*x(2,1)+1;2*x(2,1)+2*x(1,1)-1];
tmp=x - inv(A)*b;
error=norm(x-tmp,2);
x=tmp;
History (K, . 1 ) = X ( . 1 ); 
History (K, 2 ) = X ( 2 ); 
K = K + . 1 ;
 End 
X1 = X ( . 1 , . 1 ); 
X2 = X ( 2 , . 1 ); 
F = 2 * X1 * X1 + X2 * X2 + 2 * X1 * X2 + X1- X2; 
fprintf ( ' A =% F, B =% F, F =% F, K =% F ' , X1, X2, F, - K- . 1 ); 

%% drawing 
[X, Y] = meshgrid now (- . 4 :. 2 : . 4 ); X% and Y to generate mesh data
len = size(X);
The Z = 2 * Power (X-,. 2 ) + Power (the Y, 2 ) + 2 * * the Y + X. X-- the Y; 
[the DX, the DY] = gradient (the Z, 0.2 , 0.2 on the surface of each computing%;) gradient at the point of 
the subplot ( 2 , 2 , . 1 ); 
surfc (X-, the Y, the Z) 
the xlabel ( ' X ' ); ylabel ( ' Y ' ); zlabel ( ' Z ' ); 
HOLD ON; 


the subplot ( 2 , 2 , 2 ); 
contour (X-, the Y, the Z, 50 ); drawing contour%
title ( ' contours ' ) 
 
the subplot ( 2 , 2 , . 3 ); 
Quiver (X-, the Y, the DX, the DY);% gradient field plotted 
title ( ' gradient field ' ) 
 

the subplot ( 2 , 2 , . 4 ); 
Contour ( X-, the Y, the Z, 50 ); drawing contour% 
HOLD ON; 
Quiver (X-, the Y, the DX, the DY);% gradient field plotted 
title ( ' gradient field contour + ' ) 
the xlabel ( ' X ' ); 
ylabel ( ' Y ' ); 
H = GET (GCA, ' Children' );% Get the current axes object handles all child objects
 SET (H, ' Color ' , ' K ' );% set the current object color axes all child objects black 

%% locus drawn iterative Newton method 
X0 = [ 2 , - 2 ]; 
Track = [X0; History]; 
HOLD ON; 
the subplot ( 2 , 2 , . 4 ); 
Plot (Track ( . 1 : . 3 , . 1 ), Track ( . 1 : . 3 , 2 ), ' RX ' , ' markersize ' ,5,'linewidth',1);
plot(track(1:3,1),track(1:3,2),'b--','markersize',1,'linewidth',1);

result:

 

 

a=-1.000000,b=1.500000,f=-1.250000,k=2.000000>> 

  

 

 

references

【1】 https://zhidao.baidu.com/question/198040354.html

Guess you like

Origin www.cnblogs.com/chen-hw/p/11832004.html