EGR 511 NUMERICAL METHODS _______________________ LAST NAME, FIRST Problem set #2 1. Use Newton’s method with x(0) = 0 to compute x(2) for each of the following nonlinear systems: 1 a. 4 x12 - 20x1 + x 22 + 8 = 0 b. sin(4x1x2) – 2x2 – x1 = 0 4 1 4 1 2 x 1 x1 x 22 + 2x1 – 5x2 + 8 = 0 (e - e) + 4e x 22 - 2ex1 = 0 2 4 >> s2p1a x = 0.4958936 1.9834235 >> s2p1b x = -0.5131616 -0.0183762 2. Use the method of Steepest Descent with x(0) = 0 to approximate the solutions for 4 x12 - 20x1 + 1 2 x +8=0 4 2 1 x1 x 22 + 2x1 – 5x2 + 8 = 0 2 n Iterating until g = f i2 < 0.05 i 1 % Steepest Descent Method % Set 2, problem 2 % f1='4*x(1)^2-20*x(1)+.25*x(2)^2+8'; f2='.5*x(1)*x(2)^2+2*x(1)-5*x(2)+8'; % Initial guess % x=[0 0]; f=[eval(f1) eval(f2)]; g1=f*f'; for i=1:50 Jt=[8*x(1)-20 .5*x(2) .5*x(2)^2+2 x(1)*x(2)-5]; % Gradient vector % delg=2*Jt*f'; zo=sqrt(delg'*delg); % Unit vector in the steepest descent % z=delg/zo; xs=x;a3=1;x=x-a3*z'; f=[eval(f1) eval(f2)]; g3=f*f'; while g3>g1 a3=a3/2; x=xs-a3*z'; f=[eval(f1) eval(f2)]; g3=f*f'; end a2=a3/2;x=xs-a2*z'; fsave3=f; f=[eval(f1) eval(f2)]; g2=f*f';h1=(g2-g1)/a2;h2=(g3-g2)/(a3-a2);h3=(h2-h1)/a3; % Choose a0 so that g will be minimum in z direction % a0=.5*(a2-h1/h3); x=xs-a0*z'; f=[eval(f1) eval(f2)]; g0=f*f';fsave0=f; if g0<g3 alfa=a0;g=g0;f=fsave0; else alfa=a3;g=g3;f=fsave3; end x=xs-alfa*z'; fprintf('x = %9.6f %9.6f',x(1),x(2)) fprintf(' , g = %9.6f\n',g) if abs(g)<0.05, break, end g1=g; end >> s2p2 x = 0.398351 x = 0.688581 x = 0.423584 x = 0.490600 x = 0.409879 x = 0.459636 x = 0.545482 x = 0.481841 x = 0.490227 x = 0.487611 0.059753 , g = 72.674085 1.141483 , g = 29.558308 1.273358 , g = 8.398065 1.353318 , g = 7.249389 1.479892 , g = 4.538019 1.495889 , g = 3.863304 1.830068 , g = 1.507642 1.859146 , g = 0.275003 1.865165 , g = 0.258094 1.973402 , g = 0.033052 2.b Use the method of Steepest Descent with x(0) = 0 to approximate the solutions for sin(4x1x2) – 2x2 – x1 = 0 4 1 2 x 1 (e - e) + 4e x 22 - 2ex1 = 0 4 n Iterating until g = f i2 < 0.05 i 1 % Steepest Descent Method % Set 2, problem 2b % f1='sin(4*pi*x(1)*x(2))-2*x(2)-x(1)'; f2='(1-.25/pi)*(exp(2*x(1))-e)+4*e*x(2)^2-2*e*x(1)'; e=exp(1); % Initial guess % x=[0 0]; f=[eval(f1) eval(f2)]; g1=f*f'; for i=1:50 con=4*pi*cos(4*pi*x(1)*x(2)); Jt=[x(2)*con-1 x(1)*con-2 (2-.5/pi)*exp(2*x(1))-2*e 8*e*x(2)]'; % Gradient vector % delg=2*Jt*f'; zo=sqrt(delg'*delg); % Unit vector in the steepest descent % z=delg/zo; xs=x;a3=1;x=x-a3*z'; f=[eval(f1) eval(f2)]; g3=f*f'; while g3>g1 a3=a3/2; x=xs-a3*z'; f=[eval(f1) eval(f2)]; g3=f*f'; end a2=a3/2;x=xs-a2*z'; fsave3=f; f=[eval(f1) eval(f2)]; g2=f*f';h1=(g2-g1)/a2;h2=(g3-g2)/(a3-a2);h3=(h2-h1)/a3; % Choose a0 so that g will be minimum in z direction % a0=.5*(a2-h1/h3); x=xs-a0*z'; f=[eval(f1) eval(f2)]; g0=f*f';fsave0=f; if g0<g3 alfa=a0;g=g0;f=fsave0; else alfa=a3;g=g3;f=fsave3; end x=xs-alfa*z'; fprintf('x = %9.6f %9.6f',x(1),x(2)) fprintf(' , g = %9.6f\n',g) if abs(g)<0.05, break, end g1=g; end >> s2p2b x = -0.357926 0.000000 , g = 0.139390 x = -0.361009 0.057884 , g = 0.003316 3. Find the directional derivative of f(x, y) = 2x2 + y2 at x = 2 and y = 2 in the direction of h = 3i + 2j. Ans: 8.875 4. Find the gradient vector and Hessian matrix for each of the following functions: a) f(x, y) = 2xy2 + 3exy b) f(x, y, z) = x2 + y2 + 2z2 c) f(x, y) = ln(x2 + 2xy + 3y2) Ans: 2 y 2 3 ye xy (a) f = ,H= xy 4 xy 3xe 2 x (b) f = 2 y , H = 4 z (c) f = 3 y 2 e xy 2 xy xy 4 y 3 y e 3e 4 y 3 y 2 e xy 3e xy 4 x 3x 2 e xy 2 0 0 0 2 0 0 0 4 2 x 2 y 1 1 ,H= 2 2 x 2 xy 3 y 2 x 6 y x 2 2 xy 3 y 2 2 2 x 2 2 y 2 4 xy 2 x 2 6 y 2 12 xy 2 2 2 2 2 x 6 y 12 xy 2 x 18 y 12 xy 5. a) Find the first two iterations of the Gauss-Seidel method for the following linear system using x(0) = 0; i) 3x1 - x2 + x3 = 1, ii) 10x1 - x2 = 9, 3x1 + 6x2 + 2x3 = 0, - x1 + 10x2 - 2x3 = 7, 3x1 + 3x2 + 7x3 = 4. - 2x2 + 10x3 = 6. b) Repeat Exercise (a) using the SOR method with relaxation factor = 1.1. Gauss Seidel Gauss Seidel 0.1111111 0.9790000 -0.2222222 0.9495000 0.6190476 0.7899000 SOR SOR 0.0541008 0.9876790 -0.2115435 0.9784934 0.6477159 0.7899328 method method 6. Solve the system x2 + y2 + z2 = 9, xyz = 1, x + y - z2 = 0 by Newton's method to obtain the solution near (2.5, 0.2, 1.6). % Newton Method % f1='x(1)^2+x(2)^2+x(3)^2-9'; f2='x(1)*x(2)*x(3)-1'; f3='x(1)+x(2)-x(3)^2'; % Initial guess % x=[2.5 0.2 1.6]; for i=1:20 f=[eval(f1) eval(f2) eval(f3)]; Ja=[2*x(1) 2*x(2) 2*x(3) x(2)*x(3) x(1)*x(3) x(1)*x(2) 1 1 -2*x(3)]; % dx=Ja\f'; x=x-dx'; if max(abs(dx))<.00001, break, end end fprintf('Number of iteration = %g\n',i) fprintf('x = '); fprintf(' %10.7f %10.7f %10.7f\n',x(1),x(2),x(3)) Number of iteration = 3 x = 2.4913757 0.2427459 1.6535179 7. (P. 14.3 Chapra) Given: f(x, y) = 2xy + 1.5y – 1.25x2 – 2y2 Construct and solve a system of linear algebraic equations that maximizes f(x, y). Note that this is done be setting the partial derivatives of f with respect to both x and y to zero. Ans: x = 0.5, y = 0.625 8. (P. 14.4 Chapra) (a) Start with an initial guess of x = 1 and y = 1 and apply two applications of the steepest ascent method to the following function f(x, y) = 2xy + 1.5y – 1.25x2 – 2y2 (b) Construct a plot from the results of (a) showing the path of the search. Ans: x(2) = 0.543, y(2) = 0.657 9. (P. 14.7 Chapra) Perform one iteration of the optimal gradient steepest descent method to locate the minimum of f(x, y) = – 7x + 1.25x2 + 11y + 2y2 – 2xy using initial guesses x = 0 and y = 0. Ans: x(1) = 1.3013, y(1) = 2.0448