Numerical Methods Practical Flle_MDU

download Numerical Methods Practical Flle_MDU

of 29

Transcript of Numerical Methods Practical Flle_MDU

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    1/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    NNAARRNNAAUULL ((HHAARRYYAANNAA))

    PRACTICAL FILE

    OF

    Numerical Methods

    USING C LAB

    Submitted in Partial Fullfillment of the Requirment in

    Electronics & Communication Engineering

    (Session 2012-2015)

    Submitted to : Submitted by:

    Mr. Yogesh Gupta Subhash Kumar Yadav

    ( Lectt. ) ECE 4th

    Sem.

    Roll No:12ECEL26

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    2/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    NN

    AA

    RR

    NN

    AA

    UU

    LL

    ((HH

    AA

    RR

    YY

    AA

    NN

    AA

    ))

    CCEERRTTIIFFIICCAATTEE

    This to certify that Subhash Kumar Yadav has completed her practical lab file of NUMERICAL

    METHODS OF COMPUTATIONAL PROGRAMMING LAB under my guidance and completed it to

    my total satisfaction submitted in partial fullfillment of the requirment in

    EElleeccttrroonniiccss &&CCoommmmuunniiccaattiioonn EEnnggiinneeeerriinngg

    (Session 2012-2015)

    Submitted to : Submitted by:

    Mr. Yogesh Gupta Subhash Kumar Yadav

    ( Lectt. ) ECE 4th

    Sem.

    Roll No:12ECEL26

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    3/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    NUMERICAL METHODS LAB.

    INDEX

    Sr.NO Program Name Page No. Date Lect. Sign

    1.Program to implement Bisection method to findthe root of given equation. 1-2

    2.

    Program to implement Newton-Rapson method to

    find root of given equation.

    3-4

    3.

    Program to find solution of system of linear

    equation using Gauss elimination technique with

    pivoting.

    5-7

    4.

    Program to find solution of system of linear

    equation using Gauss-Jordon technique. 8-9

    5.

    Program to find solution of system of linear

    equation using Gauss-Seidel method. 10-12

    6.

    Program to find the largest Eigen value using

    Power method.

    13-15

    7.

    Program to fit a polynomial of degree m through

    a set of points using method of least squares.

    16-19

    8.

    Program to implement Trapezoidal rule for

    tabulated function.

    20

    9.

    Program to implement Simpsons 1/3 rule for

    tabulated function.

    21-22

    10.

    Program to solve ODE of type dy/dx=xy using

    Eulers method.

    23-24

    11.

    Program to solve ODE of type dy/dx=xy usingsecond order Runge-Kutta method(Heuns

    method)

    25-26

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    4/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 1

    Aim: Program to implement Bisection method to find the root of given

    equation.

    #include

    #include

    #include

    #include

    double f(double x)

    {

    return pow((double)x,(double)3.0)-4*x-9;

    }

    void main()

    {

    float x1,x2,epsilon,x3;

    clrscr();

    printf("Enter first point of the search interval:");

    scanf("%f",&x1);

    printf("Enter second point of the search interval:");

    scanf("%f",&x2);

    if((f(x1)*f(x2))>0)

    {

    printf("\n Initial approximations are unsuitable\n");

    exit(1);

    }

    printf("Enter prescribed tolerance:");scanf("%f",&epsilon);

    do

    {

    x3=(x1+x2)/2;

    if(f(x1)*f(x3)

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    5/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    }

    while( fabs((double)(x1-x2)) > epsilon);

    printf("\nApproximate root=%8.4f\n",x3);

    getch();

    }

    Output:

    Enter first point of the search interval:2

    Enter second point of the search interval:3

    Enter prescribed tolerance:.001

    Approximate root= 2.7061

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    6/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 2

    Aim: Program to implement Newton-Rapson method to find root of given

    equation.

    #include

    #include

    #include

    #include

    double f(double x)

    {

    return x*x*x-4.0*x-9.0;

    }

    double df(double x)

    {

    return 3.0*x*x-4.0;

    }

    void main()

    {

    float x0,x1,epsilon,delta,relativeerror;int i,n;

    clrscr();

    printf("Enter initial approximation:");

    scanf("%f",&x0);

    printf("Enter number of iterations permitted:");

    scanf("%d",&n);

    printf("Enter prescribed tolerance:");

    scanf("%f",epsilon);

    printf("Enter lower bound on slope:");

    scanf("%f",&delta);

    for(i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    7/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    }

    x1=x0-f(x0)/df(x0);

    relativeerror=fabs((x1-x0)/x1);

    x0=x1;

    if (relativeerror

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    8/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 3

    Aim: Program to find solution of system of linear equation using Gauss

    elimination technique with pivoting.

    #include

    #include

    #include

    #define MAX 20

    void main()

    {

    float a[MAX][MAX],x[MAX],max,temp,sum;int i,j,k,m,n,p,q;

    clrscr();

    printf("Enter number of equations:");

    scanf("%d",&n);

    printf("\nEnter coefficients equation wise\n");

    for(i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    9/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    if(p!=k)

    {

    for(q=k;q

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    10/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Output:

    Enter number of equations:2

    Enter coefficients equation wise

    3

    2

    5

    8

    6

    9

    Required solution is

    x(1)=56403.301

    x(2)=-6.500

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    11/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 4

    Aim: Program to find solution of system of linear equation using Gauss-

    Jordon technique

    #include

    #include

    #include

    #define MAX 20

    void main()

    {

    float a[MAX][MAX],temp,comm;

    int i,j,k,m,n,p,q;

    clrscr();

    printf("Enter number of equations:");

    scanf("%d",&n);

    printf("\nEnter coefficients equation wise\n");

    for(i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    12/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    a[i][j]=a[i][j]-comm*a[k][j];

    }

    }

    }

    printf("\n\nRequired solution is\n");

    for(i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    13/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 5

    Aim: Program to find solution of system of linear equation using Gauss-Seidel

    method.

    #include

    #include

    #include

    #define MAX 20

    void main(){

    float a[MAX][MAX],x[MAX],max,temp,sum;

    int i,j,k,m,n,p,q;

    clrscr();

    printf("Enter number of equations:");

    scanf("%d",&n);

    printf("\nEnter coefficients equation wise\n");

    for(i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    14/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    }

    }

    if(p!=k)

    {

    for(q=k;q

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    15/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Output:

    Enter number of equations:2

    Enter coefficients equation wise

    4

    9

    6

    7

    8

    2

    Required solution is

    x(1)=56403.301

    x(2)=1.097

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    16/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 6

    Aim: Program to find the largest Eigen value using Power method.

    #include

    #include

    #include

    #define MAX 20

    void main()

    {

    float a[MAX][MAX],x[MAX],s[MAX],epsilon;

    float error,big_error,temp,lamda;int i,j,k,n;

    clrscr();

    printf("Enter number of eqautions:");

    scanf("%d",&n);

    printf("\nEnter coefficients equation wise\n");

    for (i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    17/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    s[i]=0.0;

    for(j=1;jepsilon);

    printf("\n\nLargest eigen value=%.4f\n",lamda);

    printf("\nAssociated eigen vector is\n");

    for(i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    18/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    9

    6

    8

    Enter required precision:2

    Largest eigen value=12.0000

    Associated eigen vector is

    x(1)=1.0000

    x(2)=1.1667

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    19/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 7

    Aim: Program to fit a polynomial of degree m through a set of points using

    method of least squares.

    #include

    #include

    #include

    void main()

    {

    float x[20],y[20],a[20],c[20][20];

    int i,j,n,m,k,m1,m2,l;

    float p,q,max,temp,sum;

    clrscr();

    printf("Enter degree of polynomial:");

    scanf("%d",&m);

    printf("Enter number of points:");

    scanf("%d",&n);

    printf("Enter %d pair of values as x,y\n",n);for(i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    20/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    {

    c[i][j]=c[i][j]+pow((double)x[k],(double)l);

    }

    }

    l=i-1;

    c[i][m2]=0.0;

    for(k=1;k

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    21/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    for(j=k;j=1;i--)

    {

    sum=0.0;

    for(j=(i+1);j

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    22/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Output:

    Enter degree of polynomial:3

    Enter number of points:2

    Enter 2 pair of values as x,y

    Enter value for x[2154]:23

    Enter value for y[2154]:43

    Enter value for x[2154]:23

    Enter value for y[2154]:65

    Regression coefficients

    a[1]= 0.0000a[2]= 0.0000

    a[3]= 0.1021

    a[4]= 0.0000

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    23/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 8

    Aim: Program to implement Trapezoidal rule for tabulated function.

    #include

    #include

    #include

    #include

    void main(){

    int i,n;

    float x[20],y[20],h,sum,integral;

    clrscr();

    printf("Enter number of intervals:");scanf("2%d",&n);

    printf("Enter size of interval:");

    scanf("%f",&h);

    printf("Enter %d pair of (x,y)\n",n+1);

    for(i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    24/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 9

    Aim: Program to implement Simpsons 1/3 rule for tabulated function

    #include

    #include

    #include

    #include

    void main(){

    int i,n;

    float x[20],y[20],h,sum,integral,s2,s4;

    clrscr();

    printf("Enter number of intervals:");scanf("%d",&n);

    printf("Enter size of interval:");

    scanf("%f",&h);

    printf("Enter %d pair of (x,y)\n",n+1);

    for (i=1;i

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    25/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Output:

    Enter number of intervals:3

    Enter size of interval:2

    Enter 4 pair of (x,y)

    4,3

    3,4

    2,7

    9,5

    Value of the integral= 16.0000

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    26/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 10

    Aim: Program to solve ODEof type dy/dx=xy using Eulers method.

    #include

    #include

    float f(float x,float y)

    {

    return(x*y);

    }

    void main()

    {int i;

    float x,y,x1,y1,xf,h,s,s1,s2,s3,s4;

    clrscr();

    printf("Enter value of x1,y1,xf:");

    scanf("%f,%f,%f",&x1,&y1,&xf);

    printf("Enter size of interval:");

    scanf("%f",&h);

    x=x1;

    y=y1;

    i=1;

    printf("\n\nPoints constituting solution curve are\n\n");

    printf("\nPoint x y");

    printf("\n=================================");

    printf("\n%3d\t%8.3f\t%8.3f",i,x,y);

    while(x

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    27/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    }

    Output:

    Enter value of x1,y1,xf:2,3,4

    Enter size of interval:2

    Points constituting solution curve are

    Point x y

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

    1 2.000 3.000

    2 4.000 15.000

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

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    28/29Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

    Program No: 11

    Aim: Program to solve ODE of type dy/dx=xy using second order Runge-Kutta

    method(Heuns method).

    #include

    #include

    float f(float x,float y)

    {

    return(x*y);

    }

    void main(){

    int i;

    float x,y,x1,y1,xf,h,s,s1,s2,s3,s4;

    clrscr();

    printf("Enter value of x1,y1,xf:");

    scanf("%f,%f,%f",&x1,&y1,&xf);

    printf("Enter size of interval:");

    scanf("%f",&h);

    x=x1;

    y=y1;

    i=1;

    printf("\n\nPoints constituting solution curve are\n\n");

    printf("\nPoint x y");

    printf("\n=================================");

    printf("\n%3d\t%8.3f\t%8.3f",i,x,y);while(x

  • 7/27/2019 Numerical Methods Practical Flle_MDU

    29/29

    }

    printf("\n===============================\n");

    getch();

    }

    Output:

    Enter value of x1,y1,xf:4,5,3

    Enter size of interval:3

    Points constituting solution curve are

    Point x y

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

    1 4.000 5.000

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