Numerical Lab

download Numerical Lab

of 12

Transcript of Numerical Lab

  • 8/11/2019 Numerical Lab

    1/12

    Category Archives: NUMERICAL

    ANALYSIS and LINEARPROGRAMMING Lab Programs

    Write a program to solve first order ordinary differential

    equations (initial value problem) using adaptive Runge-

    Kutta method .

    Jun 26

    Posted byjaisha57

    #include

    #include

    #include

    #include

    float fun(float x,float y)

    {

    return(x*y);

    }

    void main(){

    float y,x,h,e,k1,k2,k3,k4,k5,k;

    int n,i;

    clrscr();

    printf(\n enter the value of x0:);

    scanf(%f,&x);

    printf(\n enter the value of y0:);

    scanf(%f,&y);

    printf(\n enter step length h: );

    scanf(%f,&h);

    printf(\n enter the vlaue of x at which y is needed :); scanf(%f,&e);

    n=(e-x)/h;

    for(i=1;i

  • 8/11/2019 Numerical Lab

    2/12

    x=x+h;

    printf(the value of y at x=%f is %f ,x,y);

    }

    getch();

    }

    Posted inNUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs

    Leave a comment

    Write a program to solve first and second order ordinary

    differential equations (initial value problem) using Runge-

    Kutta fourth order method.

    Jun 26

    Posted byjaisha57

    #include

    #include

    #include

    #include

    float fun(float x,float y)

    {

    return(x+fabs(sqrt(y)));

    }void main()

    {

    float y,x,h,e,k1,k2,k3,k4,k;

    int n,i;

    clrscr();

    printf(\n enter the value of x0:);

    scanf(%f,&x);

    printf(\n enter the value of y0:);

    scanf(%f,&y);

    printf(\n enter step length h: );

    scanf(%f,&h);printf(\n enter the vlaue of x at which y is needed :);

    scanf(%f,&e);

    n=(e-x)/h;

    for(i=1;i

  • 8/11/2019 Numerical Lab

    3/12

    y=y+k;

    x=x+h;

    printf(\n the value of y at x=%f is %f,x,y);

    }

    getch();

    }

    Posted inNUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs

    Leave a comment

    Write a program to solve the system of equations Ax = b

    using Gauss-Seidel method

    Jun 26

    Posted byjaisha57

    #include

    #include

    #include

    #include

    void main()

    {

    float a[20][20],x[20],e,big,temp,relerror,sum;

    int n,i,j,maxit,itr;

    char ch;

    clrscr();

    printf(enter the size of the equation:);

    scanf(%d,&n);

    top:for(i=1;i

  • 8/11/2019 Numerical Lab

    4/12

    sum=sum+a[i][j]*x[j];

    }

    temp=(a[i][n+1]-sum)/a[i][i];

    relerror=fabs((x[i]-temp)/temp);

    if(relerror>big)

    big=relerror;x[i]=temp;

    }

    if(big

  • 8/11/2019 Numerical Lab

    5/12

    clrscr();

    printf(enter the size of the equation);

    scanf(%d,&n);

    for(i=1;i

  • 8/11/2019 Numerical Lab

    6/12

    {

    sum=sum+a[i][j]*x[j];

    }

    x[i]=(a[i][n+1]-sum)/a[i][i];

    printf(\nthe value of x%2d is %f\n,i,x[i]);

    }getch();

    }

    Posted inNUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs

    Leave a comment

    Write a program to solve the system of equations Ax = b in

    tridiagonal form using Thomas Algorithm.

    Jun 26

    Posted byjaisha57

    #include

    #include

    #include

    #define MAX_N 20

    void tridge(float a[],float b[],float c[],float f[],int i,int iflag);

    void main()

    {

    float a[MAX_N+1],b[MAX_N+1],c[MAX_N+1],f[MAX_N+1];

    int n,i,j,iflag;

    clrscr();

    printf(\n what is the order n of system ?);

    scanf(%d,&n);

    printf(\n give b[1],c[1],rhs[1] for equation 1:);

    scanf(%f%f%f,&b[1],&c[1],&f[1]);

    for(i=2;i

  • 8/11/2019 Numerical Lab

    7/12

    return;

    }

    void tridge(float a[],float b[],float c[],float f[],int n,int iflag)

    {

    const float zero=0.0;

    int j;if(iflag==0)

    {

    for(j=2;j

  • 8/11/2019 Numerical Lab

    8/12

    {

    return(1/(1+pow(x,2)));

    }

    void main()

    {

    float x0,x1,sum,result;int n,cho=0;

    clrscr();

    printf(enter the lower and upper limit \n);

    scanf(%f%f,&x0,&x1);

    printf(enter number of intervals:\n);

    scan:scanf(%d,&n);

    if(cho==0)

    {

    top:printf(\t enter choice \n);

    printf(\n 1.for simpsons 1/3 rule\n);

    printf(\n 2.for simpsons 3/8 rule\n);scanf(%d,&cho);

    }

    switch(cho)

    {

    case 1:if(n%2==0)

    result=sim_1(x0,x1,n);

    else

    {

    printf(wrong choice of interval);

    printf(please enter even number);

    goto scan;

    }

    break;

    case 2:if(n%3==0)

    result=sim_2(x0,x1,n);

    else

    {

    printf(wrong choice of intervals \n );

    printf(\n please enter a multiple of 3 \n);

    goto scan;

    }break;

    default:printf(\n wrong choice enter again:);

    goto top;

    }

    printf(\n the result=%f,result);

    getch();

    }

    float sim_1(float x0,float x1,int n)

    {

    float result,h;

    h=(x1-x0)/n;sum=fun(x0)+fun(x1);

  • 8/11/2019 Numerical Lab

    9/12

    for(i=1;i

  • 8/11/2019 Numerical Lab

    10/12

    printf(\n enter the lower and upper limits :);

    scanf(%f%f,&x0,&x1);

    printf(\n enter the number of intervals :);

    scanf(%d,&n);

    res=trap(x0,x1,n);

    printf(\n TRAPEZOIDAL RULE =%f \n,res);getch();

    }

    float trap(float x0,float x1,int n)

    {

    float h,result;

    h=(x1-x0)/n;

    sum=fun(x0)+fun(x1);

    for(i=1;i

  • 8/11/2019 Numerical Lab

    11/12

    printf(\nx2=%f,x2);

    for(i=1;x1!=x2;i++)

    {

    printf(\n(%d)%f,i,x2);

    x1=x2;

    x2=x1-(func(x1)/func1(x1));}

    printf(\n the root is %f converge is %d approximately,x2,i);

    getch();

    return(0);

    }

    float func(float x)

    {

    return(pow(x,3)+(-2*x-5));

    }

    float func1(float x)

    {return((3*pow(x,2)-2));

    }

    Posted inNUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs

    Leave a comment

    Write a program to find the roots of an equation f (x) = 0

    using Bisection method.

    Jun 26

    Posted byjaisha57

    #include

    #include

    #include

    float fun(float x)

    {

    return(pow(x,3)-x-1);}

    void main()

    {

    float a,b,eps,x;

    int i=0;

    clrscr();

    printf(\n enter the lower and upper limit );

    scanf(%f%f,&a,&b);

    printf(\n enter the epsilon value);

    scanf(%f,&eps);

    if((fun(a)*fun(b))>0)printf(\n starting value is unsuitable );

    https://bcahelponline.wordpress.com/category/numerical-analysis-and-linear-programming-lab-programs/https://bcahelponline.wordpress.com/category/numerical-analysis-and-linear-programming-lab-programs/https://bcahelponline.wordpress.com/category/numerical-analysis-and-linear-programming-lab-programs/https://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-simplemultiple-roots-of-f-x-0-using-newton-raphson-method/#respondhttps://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-simplemultiple-roots-of-f-x-0-using-newton-raphson-method/#respondhttps://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-roots-of-an-equation-f-x-0-using-bisection-method/https://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-roots-of-an-equation-f-x-0-using-bisection-method/https://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-roots-of-an-equation-f-x-0-using-bisection-method/https://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-roots-of-an-equation-f-x-0-using-bisection-method/https://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-roots-of-an-equation-f-x-0-using-bisection-method/https://bcahelponline.wordpress.com/author/jaisha57/https://bcahelponline.wordpress.com/author/jaisha57/https://bcahelponline.wordpress.com/author/jaisha57/https://bcahelponline.wordpress.com/author/jaisha57/https://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-roots-of-an-equation-f-x-0-using-bisection-method/https://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-roots-of-an-equation-f-x-0-using-bisection-method/https://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-roots-of-an-equation-f-x-0-using-bisection-method/https://bcahelponline.wordpress.com/2012/06/26/write-a-program-to-find-the-simplemultiple-roots-of-f-x-0-using-newton-raphson-method/#respondhttps://bcahelponline.wordpress.com/category/numerical-analysis-and-linear-programming-lab-programs/
  • 8/11/2019 Numerical Lab

    12/12

    else

    while(fabs((b-a)/b)>eps)

    {

    x=(a+b)/2;

    i++;

    if((fun(x)*fun(a))>0)a=x;

    else

    b=x;

    }

    printf(\n solution converges to a root \n);

    printf(\n number of iterationo=%d\n,i);

    printf(%f\n,x);

    getch();

    }

    Posted inNUMERICAL ANALYSIS and LINEAR PROGRAMMING Lab Programs

    https://bcahelponline.wordpress.com/category/numerical-analysis-and-linear-programming-lab-programs/https://bcahelponline.wordpress.com/category/numerical-analysis-and-linear-programming-lab-programs/https://bcahelponline.wordpress.com/category/numerical-analysis-and-linear-programming-lab-programs/https://bcahelponline.wordpress.com/category/numerical-analysis-and-linear-programming-lab-programs/