MATLAB files for test of Newton s method for 2 nonlinear equations with a solution at Page 1 of 7 MATLAB files for test of Newton s method for 2 nonlinear equations with a solution at 3 4 This program uses the routine reduced Newton m listed separately on the Lecture Material web page All code generated with Matlab Software File Newton 2D test2 m Newton 2D test2 m This m file investigates the use of a reduced Newton s step method to solve a set of two nonlinear equations K Beers MIT ChE 10 17 2001 First make a filled 2 D contour plot of the norm of the function vector x1 min 5 x1 max 5 Npts x1 50 x1 vect linspace x1 min x1 max Npts x1 delta x1 x1 vect 2 x1 vect 1 x2 min 5 x2 max 5 Npts x2 50 x2 vect linspace x2 min x2 max Npts x2 delta x2 x2 vect 2 x2 vect 1 Make 2D grid of mesh points X1 X2 meshgrid x1 vect x2 vect F1 3 X1 3 4 X2 2 145 F2 4 X1 2 X2 3 28 Fmax max abs F1 abs F2 Now make a coarse grid for the gradient plots num coarse 10 scale quiver 0 5 x1 c linspace x1 min x1 max num coarse delta x1 c x1 c 2 x1 c 1 x2 c linspace x2 min x2 max num coarse 7 16 2002 MATLAB files for test of Newton s method for 2 nonlinear equations with a solution at Page 2 of 7 delta x2 c x2 c 2 x2 c 1 X1 c X2 c meshgrid x1 c x2 c F1 c 3 X1 c 3 4 X2 c 2 145 F2 c 4 X1 c 2 X2 c 3 28 Fmax c max abs F1 c abs F2 c DF1 DX1 DF1 DX2 gradient F1 c delta x1 c delta x2 c DF2 DX1 DF2 DX2 gradient F2 c delta x1 c delta x2 c DFmax DX1 DFmax DX2 gradient Fmax c delta x1 c delta x2 c Now make the plots figure hold on contour X1 X2 F1 20 quiver X1 c X2 c DF1 DX1 DF1 DX2 scale quiver title f 1 x 1 x 2 3 x 1 2 4 x 2 2 145 xlabel x 1 ylabel x 2 figure hold on contour X1 X2 F2 20 quiver X1 c X2 c DF2 DX1 DF2 DX2 scale quiver title f 2 x 1 x 2 4 x 1 2 x 2 3 28 xlabel x 1 ylabel x 2 figure hold on contour X1 X2 Fmax 20 quiver X1 c X2 c DFmax DX1 DFmax DX1 scale quiver title f max f 1 f 2 xlabel x 1 ylabel x 2 We now plot a trajectory for the Newton s method iterations max iter 100 x guess input Input initial guess as column vector calc f Newton 2D test1b calc f calc Jac Newton 2D test1b calc Jac 7 16 2002 MATLAB files for test of Newton s method for 2 nonlinear equations with a solution at Page 3 of 7 First use full Newton s method x traj zeros max iter 1 2 x x guess k 1 x traj k x for j 1 max iter k k 1 calculate Jacobian matrix Jac zeros 2 2 Jac 1 1 9 x 1 2 Jac 1 2 8 x 2 Jac 2 1 8 x 1 Jac 2 2 3 x 2 2 calculate function vector f 0 0 f 1 3 x 1 3 4 x 2 2 145 f 2 4 x 1 2 x 2 3 28 check for convergence f norm max abs f if f norm 1 e 10 iter conv j break end solve for Newton step update dx Jac f update solution estimate x x dx store new result in trajectory vector x traj k x end Make plot of trajectory over that of norm of f figure hold on contour X1 X2 Fmax 20 quiver X1 c X2 c DFmax DX1 DFmax DX2 scale quiver title Newton s method f trajectory xlabel x 1 ylabel x 2 7 16 2002 MATLAB files for test of Newton s method for 2 nonlinear equations with a solution at Page 4 of 7 if iter conv plot x traj 1 iter conv 1 x traj 1 iter conv 2 o gtext Initial guess x 1 num2str x guess 1 x 2 num2str x guess 2 iter int2str iter conv else plot x traj 1 x traj 2 o gtext Initial guess x 1 num2str x guess 1 x 2 num2str x guess 2 unconverged end gtext f 1 3 x 1 2 4 x 2 2 145 f 2 4 x 1 2 x 2 3 28 xfull traj x traj iter full conv iter conv Next use the reduced Newton s step method with the same convergence criterion Options max iter max iter Options max iter LS 10000 Options rtol 1 Options atol 1 e 10 Options step tol 1 e 3 Options verbose 1 Options use range 1 Options range 10 10 Param 0 x0 x guess x iflag iter conv x traj f traj reduced Newton x0 calc f calc Jac Options Param Now we plot the reduced Newton method trajectory figure hold on contour X1 X2 Fmax 20 quiver X1 c X2 c DFmax DX1 DFmax DX2 scale quiver title Reduced step Newton s method f trajectory xlabel x 1 ylabel x 2 if iter conv plot x traj 1 x traj 2 o gtext Initial guess x 1 num2str x guess 1 7 16 2002 MATLAB files for test of Newton s method for 2 nonlinear equations with a solution at Page 5 of 7 x 2 num2str x guess 2 iter int2str iter conv else plot x traj 1 x traj 2 o gtext Initial guess x 1 num2str x guess 1 x 2 num2str x guess 2 unconverged end gtext f 1 3 x 1 2 4 x 2 2 145 f 2 4 x 1 2 x 2 3 28 7 16 2002 MATLAB files for test of Newton s method for 2 nonlinear equations with a solution at Page 6 of 7 File Newton 2D test1b calc f m Newton 2D test1b calc f m This MATLAB m file calculates the function value for a simple set of 2 non linear equations K Beers MIT ChE 10 17 2001 function f Newton 2D test1b calc f x Param f 0 0 f 1 3 x 1 3 4 x 2 2 145 f 2 4 x 1 2 x 2 3 28 return 7 16 2002 MATLAB files for test of Newton s method for 2 nonlinear equations with a solution at Page 7 of 7 File Newton 2D test1b calc Jac m Newton 2D test1b calc Jac m This MATLAB m file calculates the Jacobian for a simple set of 2 non linear equations K Beers MIT ChE 10 17 2001 function Jac Newton 2D test1b calc Jac x Param Jac zeros 2 2 Jac 1 1 …
View Full Document
Unlocking...