Showing posts with label computational techniques. Show all posts
Showing posts with label computational techniques. Show all posts

Wednesday, 23 April 2014

Program to find the solution of the equation using Runge-Kutta fourth order Method

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
float x1,y1,x2,y2,xf,h,s,s1,s2,s3,s4;
//clrscr();
printf("enter the initial points :\n");
scanf("%f%f",&x1,&y1);
printf("enter the final points :\n");
scanf("%f",&xf);
printf("enter the step size :\n");
scanf("%f",&h);
//printline();
printf("X\t\tY\n");
//printline();
printf("%.2f\t\t%.2f\n",x1,y1);
while(x1<=xf)
{
  s1=1-(x1*x1*x1);
  x2=x1+(h/2);
  y2=y1+(s1*(h/2));
  s2=1-(x1*x1*x1);
  y2=y1+(s2*(h/2));
  s3=1-(x1*x1*x1);
  x2=x1+h;
  y2=y1+(s3*h);
  s4=1-(x1*x1*x1);
  s=(s1+2*s2+2*s3+s4)/6;
  y2=y1+h*s;
  x1=x2;
  y1=y2;
  printf("%.2f\t\t%.2f\n",x1,y1);
}


getch();}
Read More

Program to find the solution of the equation using Euler's Method

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
float x1,y1,x2,y2,xf,h,s;
//clrscr();
printf("enter the initial points :\n");
scanf("%f%f",&x1,&y1);
printf("enter the final points :\n");
scanf("%f",&xf);
printf("enter the step size :\n");
scanf("%f",&h);
printf("X\t\tY\n");

while(x1<=xf)
{
  printf("%.2f\t\t%.2f\n",x1,y1);
  s=x1*y1;
  y2=y1+h*s;
  x2=x1+h;
  x1=x2;
  y1=y2;
}

getch();
}
Read More

NEWTON DIVIDED DIFFERENCE FORMULA

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#define n 5
int main()
{
    float y[n],x[n],d[n-1],xa,u,sum=0,tnum,tden;
    int i,j,l,k,m;
   // clrscr();
    printf("enter the values of interpolation x points\n");
    for(i=1;i<=n;i++)
    scanf("%f",&x[i]);
    printf("enter the corresponding values of y\n");
    for(i=1;i<=n;i++)
    scanf("%f",&y[i]);
    printf("enter the value of x at which y(x) is needed\n");
    scanf("%f",&xa);
    if(fabs(xa-x[1])>fabs(xa-x[n]))
    {
       printf("this is not a suitable formula\n");
         return(1);
     }
     m=n-1;
     for(i=1;i<n;i++)
                d[i]=y[i+1]-y[i];
                 for(i=2;i<n;i++)
                 {
                  for(l=i;i>l;l--)
                  printf("\t");
                   for(j=i;j<n;j++)
                   {
                   k=m-j+i;
                   d[k]=d[k]-d[k-1];
                   }
                   }
                   u=((xa-x[1])/fabs(x[2]-x[1]));
                   sum=y[1];
                   tnum=u;
                   tden=1.0;
                   for(i=1;i<n;i++)
                   {
                   sum=sum+(tnum/tden)*d[i];
                   tnum=tnum*(u-1);
                   tden=tden*(i+1);
                   }
                   printf("the value of y is %f\n",sum);
                   getch();

 }
Read More

Program to solve interpolation formula using Lagrange interpolation method

Here is a C program to solve interpolation formula using Lagrange interpolation method.

 #include<stdio.h>  
 #include<conio.h>  
 int main()  
 {  
 int n,i,j;  
 float x1,x[10],f[10],sum=0,pf;  
 //clrscr();  
 printf("enter the order of polynomial :\n");  
 scanf("%d",&n);  
 printf("enter the value to be find :\n");  
 scanf("%f",&x1);  
 printf("enter values for xi & f(xi) :\n");  
 for(i=0;i<n;i++)  
 {  
  scanf("%f",&x[i]);  
  scanf("%f",&f[i]);  
 }  
 sum=0;  
 for(i=0;i<(n+1);i++)  
 {  
  pf=1;  
  for(j=0;j<(n+1);j++)  
  {  
   if(j!=i)  
    pf=pf*(x1-x[j])/(x[i]-x[j]);  
    }  
  sum=sum+f[i]*pf;  
 }  
 printf("f(%.2f)=%.2f\n",x1,sum);  
 getch();  
 }  
Read More

Program to solve simultaneous equation using by Gauss Seidal Method

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int n,i,j,k,maxit;
float a[10][10],e,big,sum,temp,x[10],relerr,prod=1,prod1=1;
//clrscr();
printf("enter the allowable error :");
scanf("%f",&e);
printf("\nenter the maximum number of iteration :");
scanf("%d",&maxit);
printf("\nenter the number of equations :");
scanf("%d",&n);
printf("\nenter the co-efficients of equations :\n");
for(i=1;i<=n;i++)
 for(j=1;j<=(n+1);j++)
  scanf("%f",&a[i][j]);
printf("\nentered co-efficient matrix is\n");
for(i=1;i<=n;i++)
{
 for(j=1;j<=(n+1);j++)
  printf("%.2f\t",a[i][j]);
 printf("\n");
}

for(i=1,j=n;i<=n;i++,j--)
{
  prod=prod*a[i][i];
  prod1=prod1*a[i][j];
}
if(fabs(prod) < fabs(prod1))
{
  printf("diagonal dominancy");
  getch();
  return(0);
}
else
for(i=1;i<+n;i++)
  x[i]=0;
//printline();
for(i=1;i<=n;i++)
  printf("X%d\t",i);
printf("\n");
//printline();
for(k=1;k<=maxit;k++)
{
  big=0;
  for(i=1;i<=n;i++)
  {
    sum=0;
    for(j=1;j<=n;j++)
    {
      if(i!=j)
                sum=sum+a[i][j]*x[j];
    }//end for j
    temp=(a[i][n+1]-sum)/a[i][i];
    relerr=fabs((x[i]-temp)/temp);
    if(relerr>big)
      big=relerr;
    x[i]=temp;
    printf("%.2f\t",x[i]);
  }//end for i

  printf("\n");
  if(big<=e)
  {
    printf("\nconverges to a solution in %dth iteration",k);
    printf("\nsolution vector is:\n");
    for(i=1;i<=n;i++)
      printf("x[%d]=%.2f\n",i,x[i]);
    getch();
    return(0);
  }

    }//end for k
  printf("dose not converge in %d iterations",maxit);
  getch();

}
Read More

Program to solve simultaneous equation using by Guess Jordan method

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int n,i,j,k;
float a[10][10],x[10],u;
//clrscr();
printf("enter the number of equations :");
scanf("%d",&n);
printf("\nenter the co-efficients of equations\n");
for(i=1;i<=n;i++)
 for(j=1;j<=(n+1);j++)
  scanf("%f",&a[i][j]);
printf("\nentered co-efficient matrix is\n");
for(i=1;i<=n;i++)
{
 for(j=1;j<=(n+1);j++)
  printf("%.2f\t",a[i][j]);
 printf("\n");
}
    //Diagonalisation Steps//
for(k=1;k<=n;k++)
  for(i=1;i<=n;i++)
  {
    if(i != k)
    {
      u=a[i][k]/a[k][k];
      for(j=1;j<=(n+1);j++)
      a[i][j]=a[i][j]-(u*a[k][j]);
    }
  }
  printf("\nDiagonalised matrix is\n");
  for(i=1;i<=n;i++)
  {
    for(j=1;j<=(n+1);j++)
      printf("%.2f\t",a[i][j]);
    printf("\n");
  }
     //End Diagonalisation Steps//

     //Forward Substitution Steps //
  x[n]=a[n][n+1]/a[n][n];
  for(i=1;i<=n;i++)
    x[i]=(a[i][n+1])/a[i][i];
  printf("\nsolution vector is\n");
  for(i=1;i<=n;i++)
    printf("x[%d]=%.2f\n",i,x[i]);
  getch();

}
Read More

Program to solve simultaneous equation using by Guess elimination method

#include<stdio.h>
#include<conio.h>
#include<math.h>
//#include<stdlib.h>

int main()
{
int n,i,j,m,k,p,q;
float e,a[10][10],x[10],temp,max,sum,u;
//clrscr();
printf("enter the number of equations :");
scanf("%d",&n);
printf("\nenter the co-efficients of equations\n");
for(i=1;i<=n;i++)
 for(j=1;j<=(n+1);j++)
  scanf("%f",&a[i][j]);
printf("\nenter the value of prescribed precision :");
scanf("%f",&e);
printf("\nentered co-efficient matrix is\n");
for(i=1;i<=n;i++)
{
 for(j=1;j<=(n+1);j++)
  printf("%.2f\t",a[i][j]);
 printf("\n");
}
    //Lower Triangularisation Steps//
for(k=1;k<=(n-1);k++)
   //Pivotal Condensation Steps//
{
  max=fabs(a[k][k]);
  p=k;
  for(m=(k+1);m<=n;m++)
    if(max < fabs(a[m][k]))
    {
      max=fabs(a[m][k]);
      p=m;
    }
  if(max <= e)
  {
    printf("\nill conditional equation\n");
    return(0);
  }
  if (p!=k)
    for(q=k;q<=(n+1);q++)
    {
      temp=a[p][q];
      a[p][q]=a[k][q];
      a[k][q]=temp;
    }
    //End of Pivoting Steps.
  for(i=(k+1);i<=n;i++)
  {
    u=a[i][k]/a[k][k];
    for(j=k;j<=(n+1);j++)
      a[i][j]=a[i][j]-(u*a[k][j]);
  }
}
  printf("\nLower Triangularised matrix is\n");
  for(i=1;i<=n;i++)
  {
    for(j=1;j<=(n+1);j++)
      printf("%.2f\t",a[i][j]);
    printf("\n");
  }
     //End Lower Triangularisation Steps//

     //Backward Substitution Steps //
  x[n]=a[n][n+1]/a[n][n];
  for(i=(n-1);i>=1;i--)
  {
    sum=0;
    for(j=i+1;j<=n;j++)
      sum=sum+a[i][j]*x[j];
    x[i]=(a[i][j]-sum)/a[i][i];
  }
  printf("\nsolution vector is\n");
  for(i=1;i<=n;i++)
    printf("x[%d]=%.2f\n",i,x[i]);

  getch();

}
Read More

Program to find the roots of a polynomial equation using newton raphson method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x-5*x+3
#define df(x) 3*x*x-5

//int printline()
//{
//int i;
//for(i=1;i<=60;i++)
//printf("-");
//printf("\n");
//}

int main()
{
float x0,x1,y0,dy0,e,delta;
int i,n;
printf("enter the initial guess\n");
scanf("%f%f",&x0,&x1);
printf("enter the prescribed precision value\n");
scanf("%f",&e);
printf("enter the maximum number of iteration\n");
scanf("%d",&n);
printf("enter the minimum allowed value of slope\n");
scanf("%f",&delta);
//printline();
printf("iter\tX0\tF(X0)\tF'(x0)\tX1\n");
//printline();
for(i=1;i<=n;i++)
{
  y0 = f(x0);
  dy0= df(x0);
  if ((fabs(dy0)) < delta)
  {
    printf("slope is too small\n");
    return(0);
  }
  x1=x0-(y0/dy0);
  printf("  %d\t%.2f\t%.2f\t%.2f\t%.2f\n",i,x0,y0,dy0,x1);
  if ((fabs(x1-x0)/x1)<=e)
  {
    //printline();
    printf("converges to the root\n");
    printf("number of iteration is %d\n",i);
    printf("root is %.2f\n",x1);
    getch();
    return(0);
  }
  x0=x1;
}
//printline();
printf("does not converges to the root in %d iteration",n);
getch();
}
Read More

Program to find the roots of a polynomial equation using method of false position

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x-4*x+1

int main()
{
float x0,x1,e,y0,y1,x2,y2;
int i,n;
//clrscr();
printf("enter the initial guesses\n");
scanf("%f%f",&x0,&x1);
printf("enter the prescribed precision value\n");
scanf("%f",&e);
printf("enter the maximum number of iteration\n");
scanf("%d",&n);
y0=f(x0);
y1=f(x1);
if(y0*y1 > 0)
{
  printf("initial guesses are not suitable\n");
  return (0);
  //exit(0);
}
//printline();
printf("iter\tX0\tF(X0)\tX1\tF(X1)\tX2\tF(X2)\n");
//printline();
//for(i=1;i<=n;i++)
{
  x2=(x0*y1-x1*y0)/(y1-y0);
  y2=f(x2);
  printf("  %d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",i,x0,y0,x1,y1,x2,y2);
  if ((fabs(y2))<=e)
  {
    //printline();
    printf("converges to the root\n");
    printf("number of iteration is %d\n",i);
    printf("root is %.2f\n",x2);
    getch();
    return (0);
    //exit(0);
  }
  if (y0*y2>0)
  {
    x0=x2;
    y0=y2;
  }
  else
  {
    x1=x2;
    y1=y2;
  }
}
//printline();
printf("does not converges to the root in %d iteration",n);
getch();

}
Read More

Program to find the roots of a polynomial equation using bisection method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x-x-1
int main()
{
float x0,x1,e,y0,y1,x2,y2;
int i=0;
printf("enter the initial guesses\n");
scanf("%f%f",&x0,&x1);
printf("enter the prescribed precision value\n");
scanf("%f",&e);
y0=f(x0);
y1=f(x1);
if(y0*y1>0)
{
  printf("initial guesses are not suitable\n");
  return(0);
}
//printline();
printf("iter\tX0\tF(X0)\tX1\tF(X1)\tX2\tF(X2)\n");
//printline();
while (fabs((x1-x0)/x1)>=e)
{
  x2=(x0+x1)/2;
  y2=f(x2);
  i=i+1;
  printf("  %d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",i,x0,y0,x1,y1,x2,y2);
  if(y0*y2>0)
  {
    x0=x2;
}
  else
  {
    x1=x2;
}
}

printf("converges to the root\n");
printf("number of iteration is %d\n",i);
printf("root is %.2f",x2);
getch();

}
Read More

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2022