% NewtwonForRosenbrock.m % %------------------------------------------------------------------------ % Newton's Mehod for Minimization of the Rozebrock function % f:[x1; x2] |---> 100(x2-x1^2)^2 + (1-x1)^2. %------------------------------------------------------------------------ %clear all, close all % Choose initial guess and evaluate function and gradient. Store results. xvec = [0:1:11]'; yvec = [260 992 2717 5341 8224 13195 21355 32196 35230 43352 45524 47572]'; cost_fn = @logistic; x = [yvec(1) yvec(12) 1]'; %near to optimal value computed by nlinfit [f, g, GN]=feval(cost_fn,x,xvec,yvec); x_hist = x; num_hist = [f; norm(g); 0]; % Set stopping tolerances than implement Newton Iteration iter_flag = 1; iter = 1; iter_max = 10; grad_tol = 1e-10; while iter_flag iter = iter+1; p = -GN\g; x = x + p; [f, g, GN]=feval(cost_fn,x,xvec,yvec); % Record iterates and numerical performance values x_hist = [x_hist x]; num_hist = [num_hist [f; norm(g); norm(p)]]; fprintf('iter=%d f=%5.5e |g|=%5.5e\tx=[%f %f]\n',iter,f,norm(g),x(1),x(2)); % Check stopping criteria if norm(g) < grad_tol disp('Grandient tolerance met. Stop iterations') iter_flag = 0; elseif iter == iter_max disp('Maximum number of iterations reached.') iter_flag = 0; end end