Showing posts with label Numerical Methods. Show all posts
Showing posts with label Numerical Methods. Show all posts

Saturday, April 1, 2017

Some common Numerical Method Programs for 3rd Semester BSc. CSIT students

comments 0 Blogger Comments
Pro tip: If you are searching for a speciic program, hit Ctrl+F to search faster. This is quite a long list.

1. Program to find Rootof a fuction by using Bisection method
/*program to find Rootof a fuction by using Bisection method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100
#define E 0.00001
#define f(x) x*x-4*x-10
void main()
{
 clrscr();
   int count=0;
   float x0,x1,x2,f0,f1,f2;
   printf("Enter the value of x1\n");
   scanf("%f",&x1);
   printf("Enter the value of x2\n");
   scanf("%f",&x2);
   f1=f(x1);
   f2=f(x2);
   if (f1*f2>0)
   printf("Solution does not exit");
   else
   {
 begin:
 x0=(x1+x2)/2;
 f0=f(x0);
 if((f1*f0)<0)
  x2=x0;
      else
      {
 x1=x0;
  f1=f0;
      }
      if((fabs(x2-x1)/x1)<E)
      {
 x0=(x1+x2)/2;
  printf("\nRoor=%f",x0);
  printf("\nFuctional value =%f",count);
  printf("\nIteration steps =%d",count);
      }
      else
      {
 count ++;
  goto begin;
      }
   }
   getch();
}
  

2. Program to find Rootof a fuction by using Newton Raption method
/*program to find Rootof a fuction by using Newton Raption method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
#define E 0.0001
#define f(x)  x*x*x-3*x-2
#define fd(x)  3*x*x-3
void main()
{
    clrscr();
    int count=0;
    float x0,x1,fx,fdx;
    printf("\nEnter the initial value of x0\n");
    scanf("%f",&x0);
begin:
    printf("\nStep %d",count+1);
    fx=f(x0);
    printf("\nvalue of fx%d=%f",count,fx);
    fdx=fd(x0);
    printf("\nvalue of fdx%d= %f",count,fdx);
    x1=x0-(fx/fdx);
    if((fabs(x1-x0)/x1)<E)
    {
        printf("\nRoot=%f",x1);
        printf("\nThe fuction value=%f",f(x1));
        printf("\nNo. of Iritation steps=%d",count);
    }
    else
    {
        x0=x1;
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It takes more than 10 steps So solution does not coverse");
        }
    }
    getche();
}

  

2. Program to Rootof a fuction by using Newton Raption method (Alternate)
/*program to find Rootof a fuction by using Newton Raption method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
#define E 0.0001
#define f(x) x*x-3*x+2
#define fd(x) 2*x-3
void main()
{
    clrscr();
    int count=0;
    float x0,x1,fx,fdx;
    printf("\nEnter the initial value of x0");
    scanf("%f",&x0);
begin:
    fx=f(x0);
    fdx=fd(x0);
    x1=x0-(fx/fdx);
    if((fabs(x1-x0)/x1)<E)
    {
        printf("\nRoot=%f",x1);
        printf("\nThe fuction value=%f",f(x1));
        printf("\nNo. of Iritation steps=%d",count);
    }
    else
    {
        x0=x1;
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It takes more than 10 steps So solution does not coverse");
        }
    }
    getche();
}

  

3. Program to find Root of a fuction by using Secant method
/*program to find Root of a fuction by using Secant method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
#define E 0.0001
#define f(x) x*x-4*x-10
void main()
{
    clrscr();
    int count=0;
    float x1,x2,x3,f1,f2;
    printf("\nEnter the two value of x1 and x2\n");
    scanf("%f%f",&x1,&x2);
begin:
    f1=f(x1);
    f2=f(x2);
    x3=x2-f2*(x2-x1)/(f2-f1);
    if((fabs(x3-x2)/x2)<E)
    {
        printf("\nRoot=%f",x3);
        printf("\nNo. of Iteration steps=%d",count);
    }
    else
    {
        x1=x2;
        x2=x3;
        f1=f2;
        f2=f(x3);
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It solution does not coverge");
        }

    }
    getche();
}

  

4. Program to find Rootof a fuction by using Fixed point method
/*program to find Rootof a fuction by using Fixed point method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100
#define E 0.001
#define g(x) exp(x)/3
void main()
{
    clrscr();
    int count=0;
    float x0,x1,error;
    printf("\nEnter the initial guess value of x0\n");
    scanf("%f",&x0);
begin:
    x1=g(x0);
    error=fabs(x1-x0);
    if(error<=E)
    {
        printf("\nThe root is %f",x1);
        printf("\nThe error value %f",error);
        printf("\nThe iteration steps is %d",count);
    }
    else
    {
        x0=x1;
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It solution does not coverge");
        }

    }
    getch();
}

  

5. Demonstrate Lagranges Interpolation
//Lagranges interpolation
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
void main()
{
    clrscr();
    int i,j,n;
    float x[MAX],f[MAX],sum=0,pro,xp,fp;
    printf("Enter the number of data points:\n");
    printf("\nn= ");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        printf("\nInput the data points for x[%d]&f[%d]",i,i);
        printf("\nx[%d]= ",i);
        scanf("%f",&x[i]);
        printf("\nf[%d]= ",i);
        scanf("%f",&f[i]);
    }
    printf("\nInput the specied value of x:");
    scanf("%f",&xp);
    for(i=0; i<=n; i++)
    {
        pro=1;
        for(j=0; j<=n; j++)
        {
            if(i!=j)
                pro=pro*(xp-x[j])/(x[i]-x[j]);
        }
        sum=sum+pro*f[i];
    }
    fp=sum;
    printf("\nThe required functional value at %f=%f",xp,fp);
    getch();
}

  

6. Demonstrate Newton Interpolation
// NEWTON INTERPOLATION
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 10
void main()
{
    int i,j,n;
    float xp,fp,pro,sum,a[MAX],f[MAX],x[MAX],d[MAX][MAX];
    printf("\nInput the number of data points:");
    printf("\nn=");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        printf("\nInput the data of x[%d]&f[%d]:",i,i);
        printf("\nx[%d]=",i);
        scanf("%f",&x[i]);
        printf("\nf[%d]=",i);
        scanf("%f",&f[i]);
    }
    for(i=1; i<=n; i++)
        d[i][1]=f[i];
    for(j=2; j<=n; j++)
        for(i=1; i<=n-j+1; i++)
            d[i][j]=(d[i+1][j-1]-d[i][j-1])/(x[i+j-1]-x[i]);
    for(j=1; j<=n; j++)
        a[j]=d[1][j];
    printf("\nInput the value of xp point:");
    printf("\nxp=");
    scanf("%f",&xp);
    sum=a[1];
    for(i=2; i<=n; i++)
    {
        pro= 1.0;
        for(j=1; j<=i-1; j++)
            pro=pro*(xp-x[j]);
        sum=sum+a[i]*pro;
    }
    fp=sum;
    printf("\n At xp=%f is %f",xp,fp);
    getch();
}
  

7. Demonstrate Least Square Method
// Least square method
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 10
void main()
{
    int i,n;
    float a0,a1;
    float x[MAX],y[MAX],sumx=0.0,sumy=0.0,sumxy=0.0,sumxsq=0.0;
    printf("***Least square method***" );
    printf("\nInput the number of data points:");
    printf("\nn=");
    scanf("%d",&n);
    printf("Enter the data sets one after another:");
    for(i=0; i<n; i++)
    {
        // printf("\nInput the data of x[%d]&f[%d]:",i,i);
        printf("\nx[%d]=",i);
        scanf("%f",&x[i]);
        printf("\ny[%d]=",i);
        scanf("%f",&y[i]);
        sumx+=x[i];
        sumy+=y[i];
        sumxy+=x[i]*y[i];
        sumxsq+=x[i]*x[i];
    }
    a0=(sumy*sumxsq-sumx*sumxy)/(n*sumxsq-pow(sumx,2));
    a1=(n*sumxy-sumx*sumy)/(n*sumxsq-pow(sumx,2));
    printf("The equation of line:\n");
    printf("\n y=%fx+%f",a1,a0);
    getch();
}
  

8. Program to find Integration of given function by using Trapezoidal rule
/*program to find Integration of given funtion by using Trapezoidal rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x+1
void main()
{
    clrscr();
    float a,b,h,It;
    printf("\nEnter initial value of X:");
    scanf("%f",&a);
    printf("\nEnter  Final  value of X:");
    scanf("%f",&b);
    h=(b-a)/2;
    It =h*(f(a)+f(b));
    printf("\nThe integration value of function :%f",It);
    getch();
}

  

9. Program to find Integration of given tabulated data using Composite Trapezoidal rule*
/*program to find Integration of given tabulated data using  Composit Trapezoidal rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 15
void main()
{
    clrscr();
    int n,n1,n2,i;
    float a,b,h,sum,ict,x[max],y[max];
    printf("\nEnter the number of data points:");
    printf("\nn=");
    scanf("%d",&n);
    printf("\nInput the values set by set");
    for(i=1; i<=n; i++)
    {
        printf("\nX%i=",i);
        scanf("%f",&x[i]);
        printf("\nF(x%i)=",i);
        scanf("%f",&y[i]);
    }
    printf("\nInput the initial value.");
    printf("\na=");
    scanf("%f",&a);
    printf("\nInput the final value.");
    printf("\nb=");
    scanf("%f",&b);
    h=x[2]-x[1];
    n1= (int)(fabs(a-x[1])/h+1.5);
    n2= (int)(fabs(b-x[1])/h+1.5) ;
    sum=0.0;
    for(i=n1; i<=n2; i++)
        sum =sum+y[i]+y[i+1];
    ict= sum*h/2.0;
    printf("\nIntegral from %f to %f is %f\n",a,b,ict);
    getche();
}
  

10. Program to find Integration of given funtion by using Composite Trapezoidal rule
/*program to find Integration of given funtion by using composit Trapezoidal rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    clrscr();
    int i,n;
    float a,b,h,sum,Ict;
    float F(float x);
    printf("Enter the initial value of a:");
    scanf("%f",&a);
    printf("\nEnter the Final  value of b: ");
    scanf("%f",&b);
    printf("\nEnter the segment width:");
    scanf("%f",&h);
    n=(b-a)/h;
    sum =(F(a)+F(b))/2.0;
    for(i=1; i<n-1; i++)
    {
        sum= sum +F(a+i*h);
    }
    Ict=sum*h;
    printf("\nThe integration between %f and %f at h=%f is %f",a,b,h,Ict);
    getch();
}
float F(float x)
{
    float f;
    f=1-exp(-x/2.0) ;
    return (f);
}

  

11. Program to find Integration of given funtion by using Simson's 1/3 rule
/*program to find Integration of given funtion by using Simson's 1/3 rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) 1-exp(-(x)/2.0)
void main()
{
 clrscr();
   int n,m,i;
   float a,b,h,sum,Ics,x,f1,f2,f3;
   printf("Enter initial value of X:");
   printf("\na=");scanf("%f",&a);
   printf("\nEnter  Final  value of X:");
   printf("\nb=");scanf("%f",&b);
   printf("\nEnter number of segments (Even number):");
   printf("\nN=");scanf("%d",&n);
   h=(b-a)/n;
   m=n/2;
   sum =0.0;
   x=a;
   f1=F(x);
   for(i=1;i<m;i++)
   {
    f2=F(x+h);
      f3=F(x+2*h);
      sum=sum+f1+4*f2+f3;
      f1=f3;
      x=x+2*h;
   }
   Ics=sum *h/3.0;
   printf("\nIntegral from %f to %f\n",a,b);
   printf("When h=%f is %f \n",h,Ics);
   getch();
}
  

12. Program to find Integration of given funtion by using Simson's 1/8 rule
/*program to find Integration of given funtion by using Simson's 1/8 rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) (x*x*x+1)
void main()
{
    clrscr();
    float a,b,h,Is;
    printf("\nEnter initial value of X:");
    scanf("%f",&a);
    printf("\nEnter  Final  value of X:");
    scanf("%f",&b);
    h=(b-a)/3;
    Is=3*h*(F(a)+3*F(a+h)+3*F(a+2*h)+F(b))/8;
    printf("\nIntegral from %f to %f\n",a,b);
    printf("Integration is %f \n",Is);
    getch();
}

  

13. Program to solve Integration equation by using Gauss Integration Method for n=2 interpolating point
/*program to solve Integration equation by using gauss integration method for
n=2 interpolating point*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) exp(x)
void main()
{
    clrscr();
    int n=2,i;
    float a,b,w[2],z[2],x[2],Ig =0.0;
    printf("\nInput initial value of x:");
    printf("\na=");
    scanf("%f",&a);
    printf("\nInput final value of x:");
    printf("\nb=");
    scanf("%f",&b);
    w[0]=1.0;
    w[1]=1.0;
    z[0]=-0.5773502;
    z[1]=0.5773502;
    for (i=0; i<n; i++)
        x[i]=((b-a)/2)* z[i]+((b+a)/2);
    for(i=0; i<n; i++)
        Ig +=((b-a)/2)*(w[i]*f(z[i]));
    printf("\nIntegral from %f to %f is %f\n",a,b,Ig);
    getche();
}

  

14. Program to solve Integration equation by using gauss integration method for n=3 interpolating point
/*program to solve Integration equation by using gauss integration method for
n=3 interpolating point*/
#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
#include&lt;math.h&gt;
#define f(x) exp(x)
void main()
{
    clrscr();
    int n=3,i;
    float a,b,w[3],z[3],x[3],Ig =0.0000;
    printf("\nInput initial value of x:");
    printf("\na=");
    scanf("%f",&amp;a);
    printf("\nInput final value of x:");
    printf("\nb=");
    scanf("%f",&amp;b);
    w[0]=0.55556;
    w[1]=0.88889;
    w[2]=0.55556;
    z[0]=-0.77460;
    z[1]=0.00;
    z[2]=0.77460;
    for (i=0; i&lt;n; i++)
        x[i]=((b-a)/2)* z[i]+((b+a)/2);
    for(i=0; i&lt;n; i++)
        Ig +=((b-a)/2)*(w[i]*f(z[i]));
    printf("\nIntegral from %f to %f is %f\n",a,b,Ig);
    getche();
}
  

15. Demonstrate Euler Method
//Eluer method
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define f(x,y) 2*y/x
void main()
{
    clrscr();
    int i,n;
    float x,y,h,xp,dy;
    printf("Input initial value of x and y");
    scanf ("%f%f",&x,&y);
    printf("Input X-value at which Y is required :\n");
    scanf("%f",&xp);
    printf("Input  step size:\n");
    scanf("%f",&h);
    n=((xp-x)/h+0.5);
    for (i=1; i<=n; i++)
    {
        dy=h*f(x,y);
        x=x+h;
        y=y+dy;
        printf("x=%f and y=%f\n",x,y);
    }
    printf("\n Value of y at x =%f is %f",x,y);
    getche();
}

  

16. Demonstrate Heuns Method
// Heuns method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 15
void main()
{
    clrscr();
    int n,i;
    float x,y,xp,h,m1,m2;
    float func(float,float);
    printf("\n***Solution of Heuns method***");
    printf("\nEnter the initial value of x \n");
    scanf("%f",&x);
    printf("\nEnter the initial value of y \n");
    scanf("%f",&y);
    printf("\nInput x at which y is required");
    scanf("%f",&xp);
    printf("\nInput step size");
    scanf("%f",&h);
    n=(int)((xp-x)/h+0.5);
    for(i=1; i<=n; i++)
    {
        m1=func(x,y);
        m2=func(x+h,y+m1*h);
        x=x+h;
        y=y+0.5*h*(m1+m2);
        printf("%5d %10.6f %10.6f\n",i,x,y);
    }
    printf("\nThe value of y at x=%f is %f\n",x,y);
    getch();
}
float func(float x,float y)
{
    float f;
    f=2.0*y/x;
    return(f);
}

  

17. Demonstrate Runge-Kutta Method
#include <stdio.h>
#include <conio.h>
#include <math.h>
//#define f(x,y) (2.0*y/x)
void main()
{
  clrscr();
  int n,i;
  float x,y,xp,h;
  float m1,m2,m3,m4;
  float func(float , float );
  printf("Input initial value of x and y");
  scanf ("%f%f",&x,&y);
  printf("Input X-value at which Y is required :\n");
  scanf("%f",&xp);
  printf("Input  step size:\n");
  scanf("%f",&h);
  n=(int)((xp-x)/h+0.5);
  for (i=1;i<=n;i++)
  {
    m1=func(x,y);
      m2=func(x+0.5*h,y+0.5+m1*h);
      m3=func(x+0.5*h,y+0.5+m2*h);
      m4=func(x+h,y+m3*h);
      x=x+h;
      y=y+(m1+2.0*m2+2.0*m3+m4)*h/6.0;
      printf("x=%f and y=%f\n",x,y);
  }
  printf("\n Value of y at x=%f is %f",x,y);
    getche();
}
float func(float x, float y)
  {
    float f;
      f=x*x+y*y;
      return(f);
  }
  

18. Runge-Kutta (Alternate)
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define f(x,y) 2.0*y/x
void main()
{
    clrscr();
    int i,n;
    float x,y,xp,h;
    float m1,m2,m3,m4;
    printf("Input initial value of x and y");
    scanf ("%f%f",&x,&y);
    printf("Input X-value at which Y is required :\n");
    scanf("%f",&xp);
    printf("Input  step size:\n");
    scanf("%f",&h);
    n=(int)((xp-x)/h+0.5);
    for (i=1; i<=n; i++)
    {
        m1=f(x,y);
        m2=f(x+0.5*h,y+(m1*h*0.5));
        m3=f(x+0.5*h,y+(m2*h*0.5));
        m4=f(x+h,y+m3*h);
        x=x+h;
        y=y+((m1+2.0*m2+2.0*m3+m4)*h)/6.0;
        printf("x=%f and y=%f\n",x,y);
    }
    printf("\n Value of y at xp =%f is %f",x,y);
    getche();
}

  

Read More

Program to find Root of a fuction by using Newton Raphson method

comments 0 Blogger Comments

Program to find Root of a fuction by using Newton Raphson method
/*program to find Rootof a fuction by using Newton Raphson method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
#define E 0.0001
#define f(x)  x*x*x-3*x-2
#define fd(x)  3*x*x-3
void main()
{
    clrscr();
    int count=0;
    float x0,x1,fx,fdx;
    printf("\nEnter the initial value of x0\n");
    scanf("%f",&x0);
begin:
    printf("\nStep %d",count+1);
    fx=f(x0);
    printf("\nvalue of fx%d=%f",count,fx);
    fdx=fd(x0);
    printf("\nvalue of fdx%d= %f",count,fdx);
    x1=x0-(fx/fdx);
    if((fabs(x1-x0)/x1)<E)
    {
        printf("\nRoot=%f",x1);
        printf("\nThe fuction value=%f",f(x1));
        printf("\nNo. of Iritation steps=%d",count);
    }
    else
    {
        x0=x1;
        count ++;
        if (count<MAX)
        {
            goto begin;
        }
        else
        {
            printf("It takes more than 10 steps So solution does not coverse");
        }
    }
    getche();
}

  

Read More

Program to find Root of a fuction by using Bisection method

comments 0 Blogger Comments

Program to find Rootof a fuction by using Bisection method
/*program to find Rootof a fuction by using Bisection method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100
#define E 0.00001
#define f(x) x*x-4*x-10
void main()
{
  clrscr();
   int count=0;
   float x0,x1,x2,f0,f1,f2;
   printf("Enter the value of x1\n");
   scanf("%f",&x1);
   printf("Enter the value of x2\n");
   scanf("%f",&x2);
   f1=f(x1);
   f2=f(x2);
   if (f1*f2>0)
   printf("Solution does not exit");
   else
   {
  begin:
  x0=(x1+x2)/2;
  f0=f(x0);
  if((f1*f0)<0)
    x2=x0;
      else
      {
  x1=x0;
   f1=f0;
      }
      if((fabs(x2-x1)/x1)<E)
      {
  x0=(x1+x2)/2;
   printf("\nRoor=%f",x0);
   printf("\nFuctional value =%f",count);
   printf("\nIteration steps =%d",count);
      }
      else
      {
  count ++;
   goto begin;
      }
   }
   getch();
}
  

Read More

Most read this week!