Skip to main content

Numerical Method Programming

1. Program to implement Bisection Method

 

#include<conio.h>

#include<stdio.h>

#include<math.h>

float f(float x)

{

       //return x*x*x*x-x-10;

       //return x*x*x-x-1;

       return x - cos(x);

}

void main()

{

       float x0,x1,x2;

       float f0,f1,f2;

       float e=0.001;

       int i=0;

       clrscr();

       printf("Enter the values of x0 and x1 : ");

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

       do

       {

              f0=f(x0);

              f1=f(x1);

              x2=(x0+x1)/2;

              f2=f(x2);

              if(f0*f2>0)

              {

                     x0=x2;

              }

              else

              {

                     x1=x2;

              }

              i++;

              printf("\n\nNumber of Iterations = %d",i);

              printf("\nRoot = %f",x2);

              printf("\nValue of the function = %f",f2);

       }

        while(fabs(f2)>e);

       getch();

}

 

2. Program to implement (False Position) Regula-Falsi Method

 

#include<conio.h>

#include<stdio.h>

float f(float x)

{

       //return x*x*x-2*x-5;

       return x*x*x*x-x-10;

}

void main()

{

       float a,b,x,e=0.00012;

       int i=0;

       printf("Enter the values A and B : ");

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

       do

       {

              x=(a*f(b)-b*f(a))/(f(b)-f(a));

              if(f(x)==0)

              {

                     printf("Root is %f",x);

                     exit(0);

              }

              if(f(a)*f(x)>0)

              {

                     a=x;

              }

              else

              {

                     b=x;

              }

              printf("\n Value of x %f",x);

              //    getch();

              i++;

       }while(i<4);//while(f(x)>e);

getch();

}

 

3. Program to implement Secant Method.

 

#include<conio.h>

#include<stdio.h>

#include<math.h>

float f(float x)

{

        return(x*x*x*x-x-10);

}

void main()

{

        float a,b,c,d,e;

        int count=0,n;

        printf("\n\nEnter the values of a and b : ");

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

        printf("Enter the values of allowed error : ");

        scanf("%f",&e);

        printf("Enter the maximum number of iterations : ");

        scanf("%d",&n);

        do

        {

               if(f(a)==f(b))

               {

                       printf("\nSolution cannot be found as the values of a and b are same.\n");

               }

               c=(a*f(b)-b*f(a))/(f(b)-f(a));

               a=b;

               b=c;

               printf("Iteration No-%d    x=%f\n",count,c);

               count++;

               if(count==n)

               {

                      break;

               }

        } 

        while(fabs(f(c))>e);

        printf("\nThe required solution is %f\n",c);

        getch();

}

 

4. Program to illustrate the iteration method.

 

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define phi(x)  10/sqrt(x+1) //(3*x-1)/(x*x) //(cos(x)+3)/2

void main()

{

       int k=0;

       float x1,x0;

       float e;

       printf("\nEnter the initial Approximation X0 : ");

       scanf("%f",&x0);

       printf("Enter the value of allowed error : ");

       scanf("%f",&e);

       x1=x0;

       do

       {

              k++;

              x0=x1;

              x1=phi(x0);

              printf("Ietration %d - X = %f\n",k,x1);

       }while(fabs(x1-x0)>e);

       printf("One root is %f obtained at %d th iteration ",x1,k);

       getch();

}

 

5. Program to illustrate the Newton-Raphson method.

 

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<stdlib.h>

float f(float x)

{

    return x*x*x-4*x-9;

}

float df (float x)

{

    return 3*x*x-4;

}

void main()

{

    int i, max;

    float h,x0, x1,e;

    printf("\nEnter Intitial Approximation : ");

    scanf("%f",&x0);

    printf("Enter allowed error : ");

    scanf("%f",&e);

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

    scanf("%d",&max);

    for (i=1; i<=max; i++)

    {

       h=f(x0)/df(x0);

       x1=x0-h;

       printf("At Iteration %d, Approximation root is x = %5.4f\n", i, x1);

       if(fabs(h)<e)

       {

           printf("After %d iterations, Approximation root = %5.4f\n",i,x1);

           getch();

           exit(0);

       }

       x0=x1;

    }

    printf("\nThe required solution does not converge");

    getch();

}

 

6. Program to Implement Gauss Elimination Method

 

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<stdlib.h>

#define   SIZE   10

void main()

{

        float a[SIZE][SIZE], x[SIZE], ratio;

        int i,j,k,n;

        clrscr();

        printf("Enter number of unknowns: ");

        scanf("%d", &n);

        for(i=1;i<=n;i++)

        {

                for(j=1;j<=n+1;j++)

                {

                        printf("a[%d][%d] = ",i,j);

                        scanf("%f", &a[i][j]);

                }

        }

        for(i=1;i<=n-1;i++)

        {

                if(a[i][i] == 0.0)

                {

                        printf("Mathematical Error!");

                        exit(0);

                }

                for(j=i+1;j<=n;j++)

                {

                        ratio = a[j][i]/a[i][i];

 

                        for(k=1;k<=n+1;k++)

                        {

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

                        }

                }

        }

        x[n] = a[n][n+1]/a[n][n];

 

        for(i=n-1;i>=1;i--)

        {

                x[i] = a[i][n+1];

                for(j=i+1;j<=n;j++)

                {

                           x[i] = x[i] - a[i][j]*x[j];

                }

                x[i] = x[i]/a[i][i];

        }

        printf("\nSolution:\n");

        for(i=1;i<=n;i++)

        {

              printf("x[%d] = %0.3f\n",i, x[i]);

        }

        getch();

}

 

7. An Example to implement Gauss Jordan Method.

 

#include<stdio.h>

void main()

{

      int i,j,k,n;

      float A[20][20],c,x[10];

      printf("\nEnter the size of matrix: ");

      scanf("%d",&n);

      printf("\nEnter the elements of matrix row-wise:\n");

      for(i=1; i<=n; i++)

     {

           for(j=1; j<=(n+1); j++)

           {

                   printf(" A[%d][%d]:", i,j);

                   scanf("%f",&A[i][j]);

           }

      }  

        for(j=1; j<=n; j++)

        {

           for(i=1; i<=n; i++)

           {

               if(i!=j)

               {

                  c=A[i][j]/A[j][j];

                  for(k=1; k<=n+1; k++)

                  {

                      A[i][k]=A[i][k]-c*A[j][k];

                  }

               }

           }

        }

        printf("\nThe solution is:\n");

        for(i=1; i<=n; i++)

        {

           x[i]=A[i][n+1]/A[i][i];

           printf("\n x%d=%f\n",i,x[i]);

        }

        getch();

 

8. Program to illustrate Gauss Seidel Method

 

#include<stdio.h>

#include<math.h>

#define X 2

main()

{

          float x[X][X+1],a[X], ae, max,t,s,e;

          int i,j,r,mxit;

          for(i=0;i<X;i++) a[i]=0;

          puts(" Enter the elements of augmented matrix row wise\n");

          for(i=0;i<X;i++)

          {

                     for(j=0;j<X+1;j++)

                     {

                                 scanf("%f",&x[i][j]);

                     }

          }

          printf(" Enter the allowed error and maximum number of iteration: ");

          scanf("%f%d",&ae,&mxit);

          printf("Iteration\tx[1]\tx[2]\n");

          for(r=1;r<=mxit;r++)

          {

                      max=0;

                      for(i=0;i<X;i++)

                      {

                                  s=0;

                                  for(j=0;j<X;j++)

                                  if(j!=i)

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

                                  t=(x[i][X]-s)/x[i][i];

                                  e=fabs(a[i]-t);

                                  a[i]=t;

                      }

                      printf(" %5d\t",r);

                      for(i=0;i<X;i++)

                      printf(" %9.4f\t",a[i]);

                      printf("\n");

                      if(max<ae)

                      {

                                  printf(" Converses in %3d iteration\n", r);

                                  for(i=0;i<X;i++)

                                  printf("a[%3d]=%7.4f\n", i+1,a[i]);

                                  return 0;

                      }

 

           }

}

 

9. Program to implement Newton's Forward formula for Interpolation. 

 

#include<conio.h>

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

void main()

{

       int i,j,k,n;

       float x1,x[10],y[10],d[10][10],h,p,sum,prod,fact;

       printf("Enter the total number of points in the table ");

       scanf("%d",&n);

       printf("\nEnter the valules of x and y \n");

       for(i=1;i<=n;i++)

       {

              scanf("%f%f",&x[i],&y[i]);

       }

       printf("\nEnter the point to be interpolated ");

       scanf("%f",&x1);

       if((x1<x[1]) || (x1>x[n]))

       {

              printf("Value is outside the tabulated range");

              getch();

              exit();

       }

       for(i=1;i<=n;i++)

       {

              d[i][0]=y[i];

       }

       for(j=1;j<=(n-1);j++)

       {

              for(i=1;i<=(n-j);i++)

              {

                     d[i][j]=d[i+1][j-1]-d[i][j-1];

              }

       }

       i=1;

       while(x1>x[i])

       {

              i++;

       }

       i=i-1;

       h=x[2]-x[1];

       p=(x1-x[i])/h;

       sum=y[i];

       for(k=1;k<=n-1;k++)

       {

              prod=1;

              fact=1;

              for(j=0;j<=k-1;j++)

              {

                     prod=prod*(p-j);

                     fact=fact*(j+1);

              }

              sum=sum+(d[i][k]*prod)/fact;

       }

       printf("Value of y at x=%f is %f",x1,sum);

       getch();

}

 

10. Program to implement Trapezoidal formula for Numerical Integration. 

 

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

 

       float x[20],y[20],s,h,it;

       int i,n;

       printf("Enter the Number of elements : ");

       scanf("%d",&n);

       for(i=0;i<n;i++)

       {

              printf("X%d = ",i+1);

              scanf("%f",&x[i]);

              printf("Y%d = ",i+1);

              scanf("%f",&y[i]);

       }

       s=(y[0]+y[n-1])/2;

       for(i=1;i<n-1;i++)

       {

              s=s+y[i];

       }

       h=fabs(x[1]-x[0]);

       it=h*s;

       printf("Value of Integral is %f",it);

        getch();

}

 

11. Program to implement Simpson's 1/3 formula for Numerical Integration. 

 

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

       float x[20],y[20],s,s1=0,s2=0,it,h;

       int i,n;

       printf("Enter the number of elements : ");

       scanf("%d",&n);

       for(i=0;i<n;i++)

       {

              printf("X%d = ",i+1);

              scanf("%f",&x[i]);

              printf("Y%d = ",i+1);

              scanf("%f",&y[i]);

       }

       s=y[0]+y[n-1];

       for(i=1;i<n-1;i++)

       {

              if(i%2==1)

              {

                     s1=s1+y[i];

              }

              else

              {

                     s2=s2+y[i];

              }

       }

       printf("s=%f, s1=%f, s2=%f",s,s1,s2);

       s=s+4*s1+2*s2;

       h=abs(abs(x[1])-abs(x[0]));

       it=(h/3)*s;

       printf("Value of Integral is %f",it);

       getch();

}

 

12. Program to implement Simpson's 3/8 formula for Numerical Integration. 

 

#include<conio.h>

#include<stdio.h>

#include<math.h>

void main()

{

       float x[20],y[20],s,s1=0,s2=0,h,it;

       int i,n;

       printf("Enter the number of elements : ");

       scanf("%d",&n);

       for(i=0;i<n;i++)

       {

              printf("Enter X%d : ",i+1);

              scanf("%f",&x[i]);

              printf("Enter Y%d : ",i+1);

              scanf("%f",&y[i]);

       }

       s=y[0]+y[n-1];

       for(i=1;i<n-2;i=i+3)

       {

              s1=s1+y[i]+y[i+1];

       }

       for(i=3;i<n-3;i=i+3)

       {

              s2=s2+y[i];

       }

       s=s+3*s1+2*s2;

       h=abs(abs(x[1])-abs(x[0]));

       //h=x[1]-x[0];

        it=(3*h/8)*s;

       printf("value of integral is %f",it);

       getch();

}

 

13. Program to implement Runge Kutta's second order method  . 

 

#include<conio.h>

#include<stdio.h>

float f(float x, float y)

{

         return (x*y);

}

void main()

{

       float x,y,z,x0,y0,xn,h,k1,k2;

       printf("Enter the values for X0 Y0 Xn and H : ");

       scanf("%f%f%f%f",&x0,&y0,&xn,&h);

       x=x0;

       y=y0;

       while(x<xn)

       {

              k1=h*f(x,y);

              x=x+h;

              z=y+k1;

              k2=h*f(x,z);

              y=y+(k1+k2)/2;

              printf("\nSolution at %f is %f",x,y);

       }

       getch();

}

 

14. Program to implement Runge Kutta's fourth order method. 

 

#include<conio.h>

#include<stdio.h>

float f(float x, float y)

{

        //return 1+y*y;

         return x+y*y;

}

void main()

{

       float x,y,z,x0,y0,xn,h,k1,k2,k3,k4;

       printf("Enter the values for X0 Y0 Xn and H : ");

       scanf("%f%f%f%f",&x0,&y0,&xn,&h);

       x=x0;

       y=y0;

       while(x<xn)

       {

              k1=h*f(x,y);

              x=x+h/2;

              z=y+k1/2;

              k2=h*f(x,z);

              z=y+k2/2;

              k3=h*f(x,z);

              x=x+h/2;

              z=y+k3;

              k4=h*f(x,z);

              y=y+(k1+2*k2+2*k3+k4)/6;

              printf("\nSolution at %f is %f",x,y);

       }

       getch();

}

 

15. Program to implement Euler's method. 

 

#include<conio.h>

#include<stdio.h>

float f(float x, float y)

{

       return(x*y);

}

void main()

{

       float x0,y0,xn,h,x,y;

       printf("Enter the value for X0 Y0 Xn and H : ");

       scanf("%f%f%f%f",&x0,&y0,&xn,&h);

       x=x0;

       y=y0;

       while(x<xn)

       {

              y=y+h*f(x,y);

              x=x+h;

              printf("Solution at x=%f is %f",x,y);

       }

       getch();

}

 

16. Program to implement Euler's Modified method. 

 

#include<conio.h>

#include<stdio.h>

float f(float x, float y)

{

       return(x*y);

}

void main()

{

       float x0,y0,x,y,y1,xn,h,e;

       printf("Enter the values for X0 Y0 Xn H and E ");

       scanf("%f%f%f%f%f",&x0,&y0,&xn,&h,&e);

       x=x0;

       y=y0;

       while(x<xn)

       {

              y1=y+h*f(x,y);

              do

              {

                     y0=y1;

                     y1=y+(h/2)*(f(x,y)+f(x+h,y1));

              }

              while((y1-y0)/y1>e);

              y=y1;

              x=x+h;

              printf("Solution at x=%f is %f",x,y);

       }

       getch();

}

 

Comments

Post a Comment

Popular posts from this blog

Doubly Linked List - Data Structure

                           Doubly Linked List A doubly linked list is a  linked list data structure that includes a link back to the previous node in each node in the structure . This is contrasted with a singly linked list where each node only has a link to the next node in the list. List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list. Link  − Each link of a linked list can store a data called an element. Next  − Each link of a linked list contains a link to the next link called Next. Prev  − Each link of a linked list contains a link to the previous link called Prev. LinkedList  − A Linked List contains the connection link to the first link called First and to the last link called Last. Doubly Linked List Representation As per the above illustration, following are the important points to be considered. Dou

Information Gathering Tool (System Analysis and Design)

  Information gathering tools... Introduction Information Gathering is a very key part of the feasibility analysis process. Information gathering is both an art and a science. It is a science because it requires a proper methodology and tools in order to be effective. It is an art too, because it requires a sort of mental dexterity to achieve the best results. In this article we will explore the various tools available for it, and which tool would be best used depending on the situation. Information Gathering Tools There is no standard procedures defined when it comes to the gathering of information. However, an important rule that must be followed is the following: information must be acquired accurately and methodically, under the right conditions and with minimum interruption to the individual from whom the information is sought. 1. Review of Procedural Forms These are a very good starting point for gathering information. Procedural manuals can give a good picture of the system to b

CPP Programming

  1. Program for declaring function inside the class, declaring the objects and calling functions //Simple Program for declaring functions inside the class #include<iostream.h> #include<conio.h> class TestSimple { int x,y,s; //Three member variables public: void getdata() // function to store values on member variables { cout<<"\nEnter two numbers "; cin>>x>>y; } void sum() { s=x+y; // adding the values ov x and y } void putdata() { cout<<"\nSum of "<<x<<" and "<<y<<" is "<<s; // displaying the values of variables } }; void main() { TestSimple t; //Object declaration t.getdata(); // Function Call t.sum(); t.putdata(); getdata(); } 2. Program for declaring function outside the class //Simple Program for functions outside the class #inclu