function wMin = NewtonsMethod(     initialWeight      )

%------------------------------------------------------------------------------
% This script will perform Newton's method to find the weight vector that minimizes
% the cost function given below.  The script returns the weight vector that 
% minimized the cost function.
%
% If the simulation does not converge, the weight vectors that were computed 
% will be returned.
%
%------------------------------------------------------------------------------
%   Inputs
%------------------------------------------------------------------------------
% initialWeight     the initial weight vector.  The script will begin iterating 
%                   at the cost function corresponding to this weight.
%
%------------------------------------------------------------------------------
%   Outputs
%------------------------------------------------------------------------------
% wMin              the weight vector that minimizes the cost function





% I'm going to do most of this symbolically since I can.  Define an arbitrary
% weight vector
syms w1 w2;
w = [   w1;                                                                 ...
        w2];



% Define the cost function in terms of the weight vector
costFunction = w(1)^2  +  30*w(2)^2  +  5*w(1)*w(2);



% Find the Hessian and the gradient of the cost function
d1 = diff( costFunction, w(1) );
d2 = diff( costFunction, w(2) );
hessian = [     diff( d1,w(1) )             diff( d2,w(1) );                ...
                diff( d1,w(2) )             diff( d2,w(2) )];
invHessian = inv(hessian);
grad = [        d1;                                                         ...
                d2 ];


    
   
% Find the weight change - since our cost function is quadratic, no iteration is 
% necessary
w1 = initialWeight(1);
w2 = initialWeight(2);
correction = - eval(invHessian)  *  eval(grad);
wMin = initialWeight + correction;