Numeric Method

download Numeric Method

of 20

Transcript of Numeric Method

  • 7/28/2019 Numeric Method

    1/20

    Assignment on Numeric MethodsPRASANNA KUMAR BARIK

    Roll_No=-:970126

    12/15/2009

    SCA , KIIT UNIVERSITY

  • 7/28/2019 Numeric Method

    2/20

    P a g e | 2

    Index:

    Sl.no Programs Page no.1 Bisection method 3

    2 Regula Falsi 4

    3 Newton Raphson 5

    4 Secant Method 6

    5 Solve problem using all methods 7-12

    6 Gauss Elimination 13

    7 Gauss Jordan 14-158 gauss Seidel 16

    9 Gauss Inverse 17-19

    10 Lagrange Interpolating 20

  • 7/28/2019 Numeric Method

    3/20

    P a g e | 3

    01.Solve the equation: x^3 - 5x + 1 using Bisection

    method.

    #include#include

    #includevoid main(){int i;float xl,xr,xu,fxr;clrscr();

    xl=2;xu=3;xr=(xl+xu)/2;printf("Bisection method\n");

    printf("xl xu xr f(xr)\n");

    printf("------------------------------------------------\n");for(i=0;i0)xl=xr;elsexu=xr;xr=(xl+xu)/2;}printf("The root is %f ",xr);getch();}

    OUTPUT

  • 7/28/2019 Numeric Method

    4/20

    P a g e | 4

    02.Solve the equation: x^3 - 5x + 1 using Regula Falsi

    method.

    #include

    #include#includevoid main(){int a;float xl,xr,xu,fxr,fxl,fxu;clrscr();

    xl=2;xu=3;printf("REGULA FALSI method\n");printf("interval xl xu xr

    f(xr)\n");printf("_____________________________________________________\n\n"); for(a=0;a0)xl=xr;elsexu=xr;

    }printf("\nThe root is %f",xr);getch();}OUTPUT:

  • 7/28/2019 Numeric Method

    5/20

    P a g e | 5

    03.Solve the equation: : x^3 - 5x + 1 using Newton

    Raphson method.

    #include#include

    #include

    void main(){

    float x=0,fx,fx1,xi;int a;clrscr();for(a=0;a

  • 7/28/2019 Numeric Method

    6/20

    P a g e | 6

    04.solve the equation: x^3 - 5x + 1 = 0 using secant

    method.

    #include#include

    #includevoid main(){

    int a;double xn,xn1,fxn,fxn1,xn2;clrscr();printf("\n SECANT METHOD\n");printf("\n Enter the value of x0:");scanf("%lf",&xn);printf("\n Enter the value of x1:");scanf("%lf",&xn1);

    printf("interval xn xn+1 f(xn)f'(xn)\n");printf("-------------------------------------------------------------------------\n");

    for (a=0;a

  • 7/28/2019 Numeric Method

    7/20

    P a g e | 7

    05.solve the equation f(x)=cos x - xe^x and f(x)=x^ex -

    1 using Bisection, Regula Falsi, Newton Raphson's and

    Secant method.

    /* Bisection Method */

    #include

    #include

    #include

    void main()

    {

    float xl=0,xu=1,xr,e=2.718281828;

    int i,n;

    float fx;

    clrscr();

    printf("how many iteration do u want?..");

    scanf("%d",&n);

    for(i=0;i0)

    xl=xr;

    elsexu=xr;

    printf("\n%f",xr);

    }

    getch();

    }

    OUTPUT

  • 7/28/2019 Numeric Method

    8/20

    P a g e | 8

    /*Regular Falsi */

    #include

    #include

    #include

    void main(){

    int i;

    float xl,xr,xu,fxr,fxl,fxu,e=2.718281828;

    clrscr();

    xl=2;

    xu=3;

    printf("Regular falsi method\n");

    printf("interval xl xu xr

    f(xr)\n");

    printf("___________________________________________________

    __________\n\n");

    for(i=0;i0)

    xl=xr;

    else

    xu=xr;

    }

    printf("\nThe root is %f",xr);

    getch();

    }

  • 7/28/2019 Numeric Method

    9/20

    P a g e | 9

    OUTPUT:-

    /* Newton Raphson's */

    #include

    #include

    #include

    void main()

    {int i;

    float xn,xn1,fxn,fxn1,e=2.718281828;;

    clrscr();

    printf("\n NEWTON RAPHSON METHOD\n");

    printf("\n Enter the value of x0:");

    scanf("%f",&xn);

    printf("\n interval xn xn+1 f(xn)

    f'(xn)\n");

    printf("--------------------------------------------------------

    -----------------\n");

    for(i=0;i

  • 7/28/2019 Numeric Method

    10/20

    P a g e | 10

    fxn=cos(xn)-((xn)*(pow(e,xn)));

    fxn1=(-sin(xn))-((xn)*(pow(e,xn)))-(pow(e,xn));

    xn1=xn-(fxn/fxn1);

    printf("%d %f %f %f%f\n",i,xn,xn1,fxn,fxn1);

    xn=xn1;

    }

    printf("\n The root is %f",xn);

    getch();}

    OUTPUT:

  • 7/28/2019 Numeric Method

    11/20

    P a g e | 11

    /*Secant Method*/

    #include

    #include

    #include

    void main()

    {

    int i;

    float xn,xn1,fxn,fxn1,e=2.718281828;

    clrscr();

    printf("\n NEWTON RAPHSON METHOD\n");

    printf("\n Enter the value of x0:");

    scanf("%f",&xn);

    printf("\n interval xn xn+1 f(xn)

    f'(xn)\n");

    printf("--------------------------------------------------------

    -----------------\n");

    for(i=0;i

  • 7/28/2019 Numeric Method

    12/20

    P a g e | 12

    getch();

    }

    OUTPUT

  • 7/28/2019 Numeric Method

    13/20

    P a g e | 13

    06. Solve the equations

    x1 + x2 + x3 = 6

    3x1 + 3x2 + 4x3 = 20

    2x1 + x2 + 3x3 = 13

    Using: Gauss Elimination Method

    #include#includevoid main(){int x3,x2,x1,m,n,a[3][4];

    clrscr();for(m=0;m

  • 7/28/2019 Numeric Method

    14/20

    P a g e | 14

    7)Gauss Jordan Method

    #include#includevoid main()

    {int m,n,k,x1,p,l;int ar[10][10];clrscr();printf("\n enter the number of unknowns to be found: ");scanf("%d",&l);printf("\n enter the value of equation : \n");for(m=0;m

  • 7/28/2019 Numeric Method

    15/20

    P a g e | 15

    }}}}gotoxy(45,20);

    printf("matrix after GAUSS JORDAN");for(m=0;m

  • 7/28/2019 Numeric Method

    16/20

    P a g e | 16

    8)Gauss Seidel Method

    #include

    #include

    #includevoid main()

    {

    float x1,x2=0,x3=0;

    int a,n;

    clrscr();

    printf("\n GAUSS SEIDEL METHOD \n");

    printf("\n Enter the no. of iterations:");

    scanf("%d",&n);

    for(a=0;a

  • 7/28/2019 Numeric Method

    17/20

    P a g e | 17

    9) Inverse using Gauss Jordan Method:

    1 1 1

    3 3 42 1 3

    #include

    #include

    #include

    void main()

    {

    int i,j,k,l,n,m,t;

    float a[8][8],p,g,d,x;

    clrscr();printf("enter the orderof the matrix\n");

    scanf("%d%d",&n,&m);

    printf("enter the matrix with identity matrix:\n");

    for(i=0;i

  • 7/28/2019 Numeric Method

    18/20

    P a g e | 18

    t=a[p][j];

    a[p][j]=a[i][j];

    a[i][j]=t;

    }

    }

    g=a[i][i];for(j=i;j

  • 7/28/2019 Numeric Method

    19/20

    P a g e | 19

    OUTPUT:

  • 7/28/2019 Numeric Method

    20/20

    P a g e | 20

    10.Find the value of f(2) using Langrange Interpolating:

    f(0)=1

    f(1)=3

    f(3)=55

    #include#includevoid main()

    {

    float x0=0.0,x1=1.0,x=2.0,x2=3.0,fx0=1.0,fx1=3.0,fx2=55.0;

    float fx,st1,st2,st3;

    clrscr();

    st1=(((x-x1)*(x-x2))/((x0-x1)*(x0-x2)))*(fx0);

    st2=(((x-x0)*(x-x2))/((x1-x0)*(x1-x2)))*(fx1);

    st3=(((x-x0)*(x-x1))/((x2-x0)*(x2-x1)))*(fx2);

    fx=st1+st2+st3;

    printf("result of lagrange ,f(2)=%f",fx);

    getch();

    }

    OUTPUT: