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

System Analysis and Design Elias M. Awad (Unit - 1)

Systems development is systematic process which includes phases such as planning, analysis, design, deployment, and maintenance. example :- Computer System , Business System , Hotel , Library , College. Audience This tutorial will help budding software professionals to understand how a system is designed in a systematic and phased manner, starting from requirement analysis to system implementation and maintenance. Prerequisites This tutorial is designed for absolute beginners and hence there are no prerequisites as such, however it is assumed that the reader is familiar with the fundamentals of computers. System   Systems development is systematic process which includes phases such as planning, analysis, design, deployment, and maintenance. Here, in this tutorial, we will primarily focus on − Systems analysis Systems design Systems Analysis It is a process of collecting and interpreting facts, identifying the problems, and decomposition of a system into its components. System analy...

Data Structure & Algorithm Basic Concepts [Part - 1]

  Data Definition Data Definition defines a particular data with the following characteristics. Atomic  − Definition should define a single concept. Traceable  − Definition should be able to be mapped to some data element. Accurate  − Definition should be unambiguous. Clear and Concise  − Definition should be understandable. Data Object Data Object represents an object having a data. Data Type Data type is a way to classify various types of data such as integer, string, etc. which determines the values that can be used with the corresponding type of data, the type of operations that can be performed on the corresponding type of data. There are two data types − Built-in Data Type Derived Data Type Built-in Data Type Those data types for which a language has built-in support are known as Built-in Data types. For example, most of the languages provide the following built-in data types. Integers Boolean (true, false) Floating (Decimal numbers) Character and Strings ...

Full Java Tutorial

Introduction to Java + Installing Java JDK and IntelliJ IDEA for Java #1   Introduction to Java + Installing Java JDK and IntelliJ IDEA for Java Java is one of the most popular programming languages because it is used in various tech fields like app development, web development, client-server applications, etc. Java is an object-oriented programming language developed by Sun Microsystems of the USA in 1991. It was originally called Oak by James Goslin. He was one of the inventors of Java. Java = Purely Object-Oriented. How Java Works? The source code in Java is first compiled into the bytecode. Then the Java Virtual Machine(JVM) compiles the bytecode to the machine code. Java Installation: Step 1:  Downloading JDK  JDK stands for Java Development Kit. It contains Java Virtual Machine(JVM) and Java Runtime Environment(JRE). JDK –  Java Development Kit = Collection of tools used for developing and running java programs. JRE –  Java Runtime Environment = Helps in e...