m.mehtab

Upload: ali-akhter

Post on 03-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 M.MEHTAB

    1/9

    ES-341

    NUMERICAL ANALYSIS

    MATLAB ASSIGNMENT

    NAME: MUHAMMAD MEHTAB

    REG NO.: 2009174

    FACULTY: ME

    DATE: 17-OCT-2011

    Submitted to : Dr. Siraj Ul Haq

  • 7/28/2019 M.MEHTAB

    2/9

    General Code For Newton Methodfunction [ output_args ] = Untitled7( input_args )

    %UNTITLED7 Summary of this function goes here

    % Detailed explanation goes here

    fprintf('Solution to Question 3b using newton Method\n\n');x101=input('enter lower limit:\n');

    x202=input('enter upper limit:\n');

    tolerence=input('enter tolerance:\n');

    p0=input('enter first guess:\n');

    n=2;u=0;pn=1000;

    disp('n p'); disp('----------------------');

    fprintf('%.0f %9.3f \n',u,p0);

    u=u+1;q=p0;

    while (abs(pn-q)>tol)

    a=funcsecant(p0) b=func2(p0); q=p0;

    pn=p0-(a./b);

    fprintf('%.0f %9.6f \n',n,pn);p0=pn; n=n+1;

    end

    fprintf('solution is %9.6f\n\n',pn);

    end

    General Code For Secant Method

    x101=input('enter lower limit:\n');

    x202=input('enter upper limit:\n');

    tollerence=input('enter tolerance:\n');

    p0=input('enter first guess:\n');

    p1=input('enter second guess:\n');

    n=2; u=0; pn=1000;disp('n p'); disp('----------------------');

    fprintf('%.0f %9.3f \n',u,p0);u=u+1; fprintf('%.0f %9.3f \n',u,p1);

    q=p0;

    while (abs(pn-q)>tol)

    a=funcsecant(p0); b=funcsecant(p1); q=p1;pn=p1-(b.*(p1-p0))./(b-a);

    fprintf('%.0f %9.6f \n',n,pn);

    p0=p1 p1=pn; n=n+1;

    end

    fprintf('solution is %9.6f\n\n',pn);

    General Code for Newton Modified Method

    x1=input('enter lower limit:\n');

    x2=input('enter upper limit:\n');

    tol=input('enter tolerance:\n');

    p0=input('enter first guess:\n');

    %%p1=input('enter second guess:\n');

    n=2; u=0; pn=1000;

    disp('n p'); disp('----------------------');

  • 7/28/2019 M.MEHTAB

    3/9

    u=u+1; q=p0;

    while (abs(pn-q)>tol)

    a=funcsecant(p0);

    b=func2(p0);

    c=func3(p0);q=p0;

    pn=p0-((a.*b)/((b.^2)-(a.*c)));fprintf('%.0f %9.6f \n',n,pn); p0=pn; n=n+1;

    end

    fprintf('solution is %9.6f\n\n',pn);

    Q1)y=(x.^x)-(3.*x)+(sin(3.*x))

    function [ y ] = funcsecant( x )

    %UNTITLED2 Summary of this function goes here

    % Detailed explanation goes here

    y=(x.^x)-(3.*x)+(sin(3.*x));

    end

    function [ z ] = func2( x )

    %UNTITLED4 Summary of this function goes here

    % Detailed explanation goes here

    z=(x.^x)*(1+log(x))-3+3.*cos(3.*x);end

    function [ y ] = func3( x )

    %UNTITLED3 Summary of this function goes here

    % Detailed explanation goes here

    y=((x.^x).*(1+log(x)).^2)-(9.*sin(3.*x));

    end

    RESULT

    Question 1 :

    y=ln(x-1)+cos(x-1)-sin(x.12);

    function [ y ] = funcsecant( x )

    y=ln(x-1)+cos(x-1);

    end

    function [ z ] = func2( x )z=(1/(x-1)-sin(x-1));

    end

    function [ y ] = func3( x )

    y=(-1/((x-1).^2)-cos(x-1));

    end

    Result:

    Newton Method

  • 7/28/2019 M.MEHTAB

    4/9

    enter lower limit:

    1.3

    enter upper limit:

    1.5enter tolerance:

    10^-5enter first guess:

    1.4

    n(no of iterations) p(Values)

    0 1.400

    2 -0.089115

    3 0.036332

    4 -0.362741

    5 -0.249266

    6 -0.154254

    7 -0.138628

    8 -0.139177

    9 -0.139176

    Solution= -0.139176

    NEWTON MODIFIED

    enter lower limit:

    1.3

    enter upper limit:

    1.5

    enter tolerance:10^-5

    enter first guess:

    1.4

    n(no of iterations) p(Values)

    0 1.400

    2 -0.089115

    3 0.036332

    4 -0.362741

    5 -0.249266

    6 -0.154254

    7 -0.138628

    8 -0.1391779 -0.139176

    10 -0.139176

    Solution= -0.139176

    Secant Method

    enter lower limit:

    1.3

  • 7/28/2019 M.MEHTAB

    5/9

    enter upper limit:

    1.5

    enter tolerance:

    10^-5

    enter first guess:1.4

    enter second guess:1.5

    n p

    ----------------------

    0 1.400

    1 1.500

    2 -0.634765

    3 -0.259479

    4 -0.515309

    5 -0.209034

    6 -0.136615

    7 -0.133114

    8 -0.1396579 -0.139174

    10 -0.139176

    solution is -0.139176

    QUSTION 2

    y=(x.^2)-(2.*x.*exp(-x))+exp(-2.*x) /34;

    function [ y ] = funcsecant( x )

    %UNTITLED2 Summary of this function goes here

    % Detailed explanation goes here

    y=(x.^2)-(2.*x.*exp(-x))+exp(-2.*x);end

    function [ z ] = func2( x )

    %UNTITLED4 Summary of this function goes here

    % Detailed explanation goes here

    z=(2.*x)-(2.*(exp(-x)-(x.*exp(-x))-(2.*exp(-2.*x))));

    end

    NEWTON METHOD

    enter lower limit:0

    enter upper limit:

    1

    enter tolerance:

    10^-5

    enter first guess:

    .5

    enter second guess:

  • 7/28/2019 M.MEHTAB

    6/9

    .4

    n p

    ----------------------

    0 0.500

    1 0.4002 0.518386

    3 0.5455324 0.558665

    5 0.561870

    6 0.563888

    7 0.565898

    8 0.566374

    9 0.566849

    10 0.567031

    11 0.567100

    12 0.567127

    13 0.567133

    solution is 0.567133

    QUESTION 3

    y=cos(x+(2).^(.5))+(x.*((x/2)+((2).^(.5))));

    function [ y ] = funcsecant( x )

    %UNTITLED2 Summary of this function goes here

    % Detailed explanation goes here

    y=cos(x+(2).^(.5))+(x.*((x/2)+((2).^(.5))));

    end

    function [ z ] = func2( x )

    %UNTITLED4 Summary of this function goes here

    % Detailed explanation goes here

    z=-sin(x+(2).^(.5))+x+(2).^(.5);

    end

    Newton Method

    enter lower limit:

    -2enter upper limit:

    -1

    enter tolerance:

    10^-5

    enter first guess:

    -1.5

    n p

    ----------------------

  • 7/28/2019 M.MEHTAB

    7/9

    0 -1.500

    2 -1.478551

    3 -1.462465

    4 -1.450402

    5 -1.4413556 -1.434569

    7 -1.4294808 -1.425664

    9 -1.422801

    10 -1.420654

    11 -1.419044

    12 -1.417836

    13 -1.416931

    14 -1.416251

    15 -1.415742

    16 -1.415360

    17 -1.415074

    18 -1.414860

    19 -1.41470320 -1.414589

    21 -1.414501

    22 -1.414473

    23 -1.414473solution is -1.414473

    ===========================================================================

    ==========

    QUESTION 4

    function [ y ] = funcsecant( x )

    %UNTITLED2 Summary of this function goes here

    % Detailed explanation goes herey=(x.^3)-(3.*(x.^2)).*(2.^(-x))+(3.*x).*(4.^(-x))-(8.^(-x));

    end

    function [ z ] = func2( x )

    %UNTITLED4 Summary of this function goes here

    % Detailed explanation goes here

    z=(3.*(x.^2))-3.*((2.*x).*(2.*(-x))-(x.^2).*(2.^(-x)).*(0.6931471806))+3.*(4.^(-x)-x.*(4.^(-

    x)).*(1.386294361))+(8.^(-x).*(2.079441542));end

    Newton Method

    enter lower limit:

    0

    enter upper limit:

    1

    enter tolerance:

  • 7/28/2019 M.MEHTAB

    8/9

    10^-5

    enter first guess:

    .5

    enter second guess:

    .4n p

    ----------------------0 0.500

    1 0.400

    2 0.524045

    3 0.568238

    4 0.584777

    5 0.598859

    6 0.609079

    7 0.616948

    8 0.622863

    9 0.630733

    10 0.635225

    11 0.63668512 0.637788

    13 0.639249

    14 0.640352

    15 0.64055716 0.640711

    17 0.640827

    18 0.640981

    19 0.641032

    20 0.641069

    21 0.641148

    22 0.641157

    solution is 0.641157

    ===========================================================================

    ======

    QUESTION 5

    function [ y ] = funcsecant( x )

    %UNTITLED2 Summary of this function goes here

    % Detailed explanation goes here

    y=exp(6.*x)+3.*((ln(2)).^2).*(exp(2.*x))-(ln(8)).*(exp(4.*x))-(ln(2)).^3;

    end

    function [ z ] = func2( x )%UNTITLED4 Summary of this function goes here

    % Detailed explanation goes here

    z=6.*(exp(6.*x))+6.*((ln(2)).^2).*(exp(2.*x))-4.*(ln(8)).*(exp(4.*x));

    end

    Newton Method

    enter lower limit:

    -1

    enter upper limit:

  • 7/28/2019 M.MEHTAB

    9/9

    0

    enter tolerance:

    10^-5

    enter first guess:

    -.5n p

    ----------------------0 -0.500

    2 -0.352638

    3 -0.285436

    4 -0.247646

    5 -0.224740

    6 -0.210322

    7 -0.201052

    8 -0.195013

    9 -0.191048

    10 -0.188430

    11 -0.186696

    12 -0.18554513 -0.184778

    14 -0.184265

    15 -0.183916

    16 -0.18366917 -0.183460

    18 -0.183103

    19 -0.182643

    20 -0.182815

    21 -0.182901

    22 -0.182924

    23 -0.182926

    solution is -0.182926