Numerical Methods File

download Numerical Methods File

of 25

Transcript of Numerical Methods File

  • 7/31/2019 Numerical Methods File

    1/25

    [Type the document titl

    e]INDEX

    KIIT COLLEGE OF ENGINEERING

  • 7/31/2019 Numerical Methods File

    2/25

    S.no

    Topic Date Signature

    1. Bisection Method

    2. Newton Raphson Method

    3. Gauss Elimination Method

    4. Gauss Jordan Method

    5. Gauss Seidal Method

    6. Trapezoidal Method

    7. Simpsons Rule

    8. Runge Kutta Method

    9. Eulars Method

    10. Lagranges Interpolation

    /* Program 1:-Bisection

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    3/25

    Method*/

    #include#include#includeFigure 1float f(float x){return(x*x*x-4*x-9);}void bisect(float *x,float a,float b,int*itr){*x=(a+b)/2;++(*itr);printf("itration no. %3d X=%7.5f\n",*itr, *x);}main(){int itr=0,matrix;clrscr();float x,a,b,aree,x1;printf("enter the value of a,b","allowed error, maximum itration\n");scanf("%f%f%f%d", & a,& b,& aree,& matrix);bisect(&x,a,b,& itr);do{if (f(a)*f(x)

  • 7/31/2019 Numerical Methods File

    4/25

    return 0;}

    x=x1;}while (itr

  • 7/31/2019 Numerical Methods File

    5/25

    /* Output*/

    enter the value of a,b320.000120itration no. 1 X=2.50000after 1 iteration, root= %6.4

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    6/25

    /*Program 2:- NewtonRaphson Method*/

    #include#include#include

    float f(float x){return x*log10(x)-1.2;}

    float df(float x)

    {return log10(x)+.43429;}

    main(){int itr,maxitr;float h,x0,x1,aerr;clrscr();printf("enter x0,allowed error, maximum iterations\n");scanf("%f %f %d",&x0,&aerr,&maxitr);

    for (itr=1;itr

  • 7/31/2019 Numerical Methods File

    7/25

    /*output*/

    Enter x0,allowed error, maximum iterations2.7 0.000001 18

    Iteration no. 1, x= 2.740799Iteration no. 2, x= 2.740646Iteration no. 3, x= 2.740646After 3 iterations root = 2.7406

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    8/25

    /* Program 3:- GaussElimination Method*/

    #include#include#include#define N 3main(){float a[N][N+1],t;int i,j,k;clrscr();printf("enter the elements of the augmented matrix rowise\n");for(i=0;i

  • 7/31/2019 Numerical Methods File

    9/25

    /*Output*/

    Enter the elements of augmented matrix row wise10 -7 3 5 6-6 8 -1 -4 53 1 4 11 25 -9 -2 4 7The upper triangular matrix is:-10.000 -7.0000 3.0000 5.0000 6.00000.0000 3.8000 0.8000 -1.0000 8.6000-0.0000 -0.0000 2.4474 10.3158 -6.81580.0000 -0.0000 -0.0000 9.9247 9.9247

    The solution is: -X [1] = 5.0000X [2] = 4.0000X [3] = -7.0000X [4] = 1.0000

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    10/25

    /*Program 4:- Gauss JordonMethod*/

    #include#include#include#define N 3main(){float a[N][N+1],t;int i,j,k;clrscr();printf("enter the elements of the augmented matrix rowise\n");for(i=0;i

  • 7/31/2019 Numerical Methods File

    11/25

    printf("the solution is:-\n");for(i=0;i

  • 7/31/2019 Numerical Methods File

    12/25

    /*Output*/

    Enter the elements of augmented matrix row wise-10 -7 3 5 6-6 8 -1 -4 53 1 4 11 25 -9 -2 4 7The diagonal matrix is:-10.0000 -0.0000 -0.0000 -0.0000 50.00000.0000 3.8000 -0.0000 0.0000 15.2000-0.0000 0.0000 2.4474 0.0000 -17.13160.0000 -0.0000 0.0000 9.9247 9.9247

    The solution is: -X [1] = 5.0000X [2] = 4.0000X [3] = -7.0000X [4] = 1.0000

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    13/25

    /*Program 5:- Gauss-SeidalMethod*/

    #include#include#include#define N 3

    main(){float a[N][N+1],x[N],aerr,maxerr,t,s,err;int i,j,itr,maxitr;clrscr();

    printf("ENTER the elements of the augmented matrix rowwise\n");for(i=0;i

  • 7/31/2019 Numerical Methods File

    14/25

    for(i=0;i

  • 7/31/2019 Numerical Methods File

    15/25

    /*Output*/

    Enter the elements of augmented matrix row wise20 1 -2 173 20 -1 -182 -3 20 25Enter the allowed error, maximum iteration.0001 10Iteration X(1) X(2) X(3)1 0.8500 -1.02375 1.01092 1.0025 -0.9998 0.99983 1.0000 -1.0000 1.0000

    4 1.0000 -1.0000 1.0000Converges in 4 iterationsX [1] = 1.0000X [2] = -1.0000X [3] = 1.0000

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    16/25

    /*Program 6:-TrapezoidalMethod*/

    #include#include#include

    float y(float x){return 1/(1+x*x);}void main(){float x0, xn, h, s;int i,n;printf("enter the lower limit, upper limit, no. of intervals\n");scanf("%f%f%d",&x0,&xn,&n);h=(xn-x0)/n;s=y(x0)+y(xn);for(i=1; i

  • 7/31/2019 Numerical Methods File

    17/25

    /*output*/

    enter the lower limit, upper limit, no. of intervals1 2 2the value of definite integrals is 0.32885enter the lower limit, upper limit, no. of intervals

    1 3 2the value of definite integrals is 0.50000

    enter the lower limit, upper limit, no. of intervals1 5 9the value of definite integrals is 0.59599

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    18/25

    /*Program 7:-Simpsonsmethod*/

    #include#include#include

    float y(float x){

    return 1/(1+x*x);}void main(){

    float x0, xn, h, s;int i,n;clrscr();printf("enter the lower limit, upper limit, no. of intervals\n");scanf("%f%f%d",&x0,&xn,&n);h=(xn-x0)/n;s=y(x0)+y(xn)+y(x0+h);

    for(i=3;i

  • 7/31/2019 Numerical Methods File

    19/25

    /*Output*/

    enter the lower limit, upper limit, no. of intervals232the value of definite integrals is 0.07299

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    20/25

    /*Program 8:-Runge kuttaMethod*/

    #include#include#includefloat f(float x,float y)

    {return x+y*y;}main(){float x0,y0,x,x1,y1,h;clrscr();printf("Enter the values of x0,y0,x,h \n");scanf("%f%f%f%f",&x0,&y0,&x,&h);x1=x0;y1=y0;while(1){if(x==x1) break;

    y1+=h*f(x1,y1);x1+=h;printf("whenx=%6.4f,y=%6.4f\n",x1,y1);getch();}}

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    21/25

    /*output*/

    Enter the values of x0,y0,x,h0 1 3 .1whenx=0.1000,y=1.1000whenx=0.2000,y=1.2310whenx=0.3000,y=1.4025

    whenx=0.4000,y=1.6292whenx=0.5000,y=1.9347whenx=0.6000,y=2.3590whenx=0.7000,y=2.9755whenx=0.8000,y=3.9308whenx=0.9000,y=5.5560whenx=1.0000,y=8.7329whenx=1.1000,y=16.4591whenx=1.2000,y=43.6594whenx=1.3000,y=234.3935whenx=1.4000,y=5728.5537

    whenx=1.5000,y=3287361.5000whenx=1.6000,y=1080677892096.0000whenx=1.7000,y=116786471838643992000000.0000Floating point error: Overflow.

    Abnormal program termination

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    22/25

    /*Program 9:-Euler Method*/

    #include#include#include

    float f(float x, float y)

    {return x+y*y;}main(){float x0,y0,x1,y1,xn,h;clrscr();printf ("Enter the values of x0,y0,xn,& h\n");scanf ("%f%f%f%f", &x0,&y0,&xn,&h);x1=x0; y1=y0;while (1)

    {if (x1==xn) break;y1+=h*f(x1,y1);x1+=h;printf ("when x=%7.4f, y=%8.5f\n",x1,y1);getch();}}

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    23/25

    /*output*/

    Enter the values of x0,y0,xn,& h1 2 3 4when x= 5.0000, y=22.00000when x= 9.0000, y=1978.00000

    when x=13.0000, y=15651950.00000when x=17.0000, y=979934168219648.00000when x=21.0000, y=3841084041118427740000000000000.00000Floating point error: Overflow.

    Abnormal program termination

    AMIT KUMAR

  • 7/31/2019 Numerical Methods File

    24/25

    /*Program10:-LagrangeMethod*/

    #include#include#include

    #define MAX 100main(){float ax[MAX+1],ay[MAX+1],nr,dr,x,y=0;int i,j,n;printf("enter the values of n\n");scanf("%d",&n);printf("enter the values\n");for(i=0;i

  • 7/31/2019 Numerical Methods File

    25/25

    /*Output*/

    enter the values of n2enter the values4 5 64 5 8enter the values of x for which y is wanted2when x= 2.0000,y=-22.00000

    AMIT KUMAR