Question

4. (20 pts) In this problem, we combine the Steepest Descent method with Newtons method for solving the following nonlinear
0 0
Add a comment Improve this question Transcribed image text
Answer #1


SMatlab code for Gradient Descent method and Newton method clear all close all n-0; eps-1; a-0.06 initialize iteration countef1 xl(xl, x2, x3)-diff(f1, x1); f1 x2(xl, x2, x3)-diff (fl, x2); f1 x3(xl, x2, x3)-diff(E1, x3); f2_x1(x1,x2, x3 )-diff (f2,For initial condition x1=0.000000, Displaying the functions x2=0 .000000, x3=0.000000 C(x1,x2, x3)x1. *3+x1.*2. *x2-x1. *x3+6

%%Matlab code for Gradient Descent method and Newton method
clear all
close all
n=0;            %initialize iteration counter
eps=1;          %initialize error
a=0.06;         %set iteration parameter
xx=[0;0;0];     %set starting value
fprintf('For Gradient Descent method.\n')
fprintf('\tFor initial condition x1=%f, x2=%f, x3=%f \n',xx(1),xx(2),xx(3))
%functions for which root have to find

syms x1 x2 x3
f_x1=@(x1,x2,x3) x1.^3+x1.^2.*x2-x1.*x3+6;
f_x2=@(x1,x2,x3) exp(x1)+exp(x2)-x3;
f_x3=@(x1,x2,x3) x2-2.*x1.*x3-4;

%displaying the function

fprintf('Displaying the functions\n')
disp(f_x1)
disp(f_x2)
disp(f_x3)

fprintf('First three iterations using Steepest Descent\n')
%Computation loop
for i=1:3
    gradf=double([f_x1(xx(1),xx(2),xx(3));f_x2(xx(1),xx(2),xx(3));f_x3(xx(1),xx(2),xx(3))]); %gradf(x)
    yy=double(xx-a*gradf);                             %iterate
    n=n+1;
    eps=norm(xx-yy);                                   %counter+1
    error(n)=eps;
    xx=yy;                                             %update x
    fprintf('\tAfter %d iterations x1=%f; x2=%f x3=%f.\n',n,xx(1),xx(2),xx(3))
end

fprintf('The solution for nonlinear equation using Steepest Descent is\n\t x1= %f\n\t x2= %f\n\t x3= %f\n',xx(1),xx(2),xx(3));

fprintf('\n\nFor Newton method.\n')

%Matlab code for Newton method
%nonlinear function to be solved

syms x1 x2 x3
f1(x1,x2,x3)=x1.^3+x1.^2.*x2-x1.*x3+6;
f2(x1,x2,x3)=exp(x1)+exp(x2)-x3;
f3(x1,x2,x3)=x2-2.*x1.*x3-4;

%finding the Jacobian matrix
f1_x1(x1,x2,x3)=diff(f1,x1);
f1_x2(x1,x2,x3)=diff(f1,x2);
f1_x3(x1,x2,x3)=diff(f1,x3);

f2_x1(x1,x2,x3)=diff(f2,x1);
f2_x2(x1,x2,x3)=diff(f2,x2);
f2_x3(x1,x2,x3)=diff(f2,x3);

f3_x1(x1,x2,x3)=diff(f3,x1);
f3_x2(x1,x2,x3)=diff(f3,x2);
f3_x3(x1,x2,x3)=diff(f3,x3);

jac1=[f1_x1 f1_x2 f1_x3 ;...
            f2_x1 f2_x2 f2_x3 ;...
            f3_x1 f3_x2 f3_x3 ];
fprintf('The Jacobian matrix is\n')          
disp(jac1)

%All initial guess for x y z  
x11=1;x22=1;x33=1;

    fprintf('For initial condition x1=%f, x2=%f, x3=%f \n',x11,x22,x33)
    kmax=500; %maximum number of iterations
    %loop for Newton method
  
    fprintf('All iterations using Newton method\n')
    fprintf('\t\tx1\tx2\tx3\n')
    for i=1:kmax

        jac=jac1(x11,x22,x33);
        ijac=inv(jac);
        xx=double([x11;x22;x33]-ijac*[f1(x11,x22,x33);f2(x11,x22,x33);f3(x11,x22,x33)]);
        err=norm(xx-[x11;x22;x33]);
        x11=double(xx(1));
        x22=double(xx(2));
        x33=double(xx(3));

        if err<10^-9
            break
        end
        fprintf('\t %f; %f %f.\n',xx(1),xx(2),xx(3))
    end

fprintf('\nThe solution for nonlinear equation using Newton method is\n\t x1= %f\n\t x2= %f\n\t x3= %f\n',x11,x22,x33)


%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
4. (20 pts) In this problem, we combine the Steepest Descent method with Newton's method for solv...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT