Code for Gauss Elimination Method in C

C code to implement  Gauss Elimination  method .  Compiled in DEV C++

 

# include <stdio.h>
# include <conio.h>
int main()
{
int i, j, k, n ;
float a[20][20], x[20] ;
double s, p ;
printf("Enter the number of equations : ") ;
scanf("%d", &n) ;
printf("\nEnter the co-efficients of the equations :\n\n") ;
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf("a[%d][%d] = ", i + 1, j + 1) ;
scanf("%f", &a[i][j]) ;
}
printf("b[%d] = ", i + 1) ;
scanf("%f", &a[i][n]) ;
}
for(k = 0 ; k < n - 1 ; k++)
{
for(i = k + 1 ; i < n ; i++)
{
p = a[i][k] / a[k][k] ;
for(j = k ; j < n + 1 ; j++)
a[i][j] = a[i][j] - p * a[k][j] ;
}
}
x[n-1] = a[n-1][n] / a[n-1][n-1] ;
for(i = n - 2 ; i >= 0 ; i--)
{
s = 0 ;
for(j = i + 1 ; j < n ; j++)
{
s += (a[i][j] * x[j]) ;
x[i] = (a[i][n] - s) / a[i][i] ;
}
}
printf("\nThe result is :\n") ;
for(i = 0 ; i < n ; i++)
printf("\nx[%d] = %.2f", i + 1, x[i]) ;
getch() ;
}

website_caption1

You might be also interested in :

Advertisements

Code for Lagrange interpolation in C

C code to implement  Lagrange interpolation  method .  Compiled in DEV C++

website_caption1

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
  float x[10],y[10],temp=1,f[10],sum,p;
  int i,n,j,k=0,c;

  printf("\nhow many record you will be enter: ");
  scanf("%d",&n);
  for(i=0; i<n; i++)
  {
   printf("\n\nenter the value of x%d: ",i);
   scanf("%f",&x[i]);
   printf("\n\nenter the value of f(x%d): ",i);
   scanf("%f",&y[i]);
  }
  printf("\n\nEnter X for finding f(x): ");
  scanf("%f",&p);

  for(i=0;i<n;i++)
  {
    temp = 1;
    k = i;
    for(j=0;j<n;j++)
    {
      if(k==j)
      {
        continue;
      }
      else
      {
        temp = temp * ((p-x[j])/(x[k]-x[j]));
      }
    }
    f[i]=y[i]*temp;
  }

  for(i=0;i<n;i++)
  {
     sum = sum + f[i];
  }
  printf("\n\n f(%.1f) = %f ",p,sum);
  getch();
}

website_caption1

You might be also interested in :

Code for Newton Divided Difference in C

C code to implement  Newton Divided Difference  method .  Compiled in DEV C++

website_caption1

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
  float x[10],y[10][10],sum,p,u,temp;
  int i,n,j,k=0,f,m;
  float fact(int);

  printf("\nhow many record you will be enter: ");
  scanf("%d",&n);
  for(i=0; i<n; i++)
  {
   printf("\n\nenter the value of x%d: ",i);
   scanf("%f",&x[i]);
   printf("\n\nenter the value of f(x%d): ",i);
   scanf("%f",&y[k][i]);
  }
  printf("\n\nEnter X for finding f(x): ");
  scanf("%f",&p);

  for(i=1;i<n;i++)
  {
    k=i;
    for(j=0;j<n-i;j++)
    {
     y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]);
     k++;
    }
  }
  printf("\n_____________________________________________________\n");
  printf("\n  x(i)\t   y(i)\t    y1(i)    y2(i)    y3(i)    y4(i)");
  printf("\n_____________________________________________________\n");
  for(i=0;i<n;i++)
  {
    printf("\n %.3f",x[i]);
    for(j=0;j<n-i;j++)
    {
     printf("   ");
     printf(" %.3f",y[j][i]);
    }
   printf("\n");
  }

  i=0;
  do
  {
   if(x[i]<p && p<x[i+1])
    k=1;
   else
    i++;
  }while(k != 1);
  f=i;

  sum=0;
  for(i=0;i<n-1;i++)
  {
   k=f;
   temp=1;
   for(j=0;j<i;j++)
   {
    temp = temp * (p - x[k]);
    k++;
   }
    sum = sum + temp*(y[i][f]);
  }
  printf("\n\n f(%.2f) = %f ",p,sum);
  getch();
}

website_caption1

You might be also interested in :

Code for Runge Kutta method method in C

C code to implement  Runge Kutta  method  .  Compiled in DEV C++

website_caption1

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

float math(float x, float y)
{
float f;
f=sqrt(x+y);
return(f);
}

int main()
{
int i,n;
float x0,y0,x,h,k1,k2,k3,k4;

printf("Type the value of the point x where value of y to be found\n");
scanf("%f",&x);
printf("Type the initial value of x and y\n");
scanf("%f%f",&x0,&y0);
printf("Type the number of step length n\n");
scanf("%d",&n);
printf("x0=%f\t  y0=%f\t  n=%d \n",x0,y0,n);
h=(x-x0)/n;
for(i=1;i<=n;i++)
{
k1=h*math(x0,y0);
k2=h*math(x0+h/2,y0+k1/2);
k3=h*math(x0+h/2,y0+k2/2);
k4=h*math(x0+h,y0+k3);
y0+=(k1+2*k2+2*k3+k4)/6;
x0+=h;
}
printf("at x=%f\t,y=%f\n",x,y0);
getch();
}

website_caption1

 

You might be also interested in :

Code for Taylor series method in C

C code to implement  Taylor series method  .  Compiled in DEV C++

website_caption1

#include<stdio.h>
#include<conio.h>
#include<math.h>
long int factorial(int n);

int main()
{
 int x,i;
 float s,r;
 char c;

printf("You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!..x^x/x!");
 printf("To which term you want its sum of ?  ");
 scanf("%d",&x);
 s=0;
 for (i=1;i<=x;i++)
  {   s=s+((float)pow(x,i)/(float)factorial(i)); 
}
 printf("The sum of %d terms is %f",x,1+s);

 fflush(stdin);
      getch();
}

long int factorial(int n)
 {
  if (n<=1)
 return(1);
  else
 n=n*factorial(n-1);
 return(n);
 }

website_caption1

You might be also interested in :

Code for Modified Euler’s method in C

C code to implement  modified Euler’s  method  .  Compiled in DEV C++

website_caption1

#include<stdio.h>
#include <math.h>
#include<conio.h>
#define F(x,y)  (x)*(x)+(y)
int main()
{
  double y0,x0,y1,x1,y1_0,a,n,h,f,f1;
  int j,count,flag;

  printf("\nEnter the value of x0: ");
  scanf("%lf",&x0);
  printf("\nEnter the value of y0: ");
  scanf("%lf",&y0);
  printf("\nEnter the value of h: ");
  scanf("%lf",&h);
  printf("\nEnter the value of last point: ");
  scanf("%lf",&n);
  for(x1=x0+h,j=1; x1<=n+h; x1=x1+h,j++)
  {
    count=0;
    flag=0;
    f=F(x0,y0);
    y1_0 = y0 + (h * f);
    printf("\n\n * * y%d_0 = %.3lf * *",j,y1_0);
     do
      {
    count++;
    f=F(x0,y0);
    f1=F(x1,y1_0);
    y1 = y0 + h/2 * ( f + f1);
    printf("\n\n * * x = %.3lf => y%d_%d = %.3lf * *",x1,j,count,y1);
    if(fabs(y1-y1_0)<0.00001)
     {
      printf("\n\n\n\n * * * * y%d = %.3lf * * * *\n\n",j,y1);
      flag=1;
     }
    else
      y1_0 = y1;
      }while(flag!=1);
    y0 = y1;
  }
getch();
}

website_caption1

You might be also interested in :

Code for Euler’s method in C

C code to implement  Euler’s  method  .  Compiled in DEV C++

website_caption1

/*Euler method */
# include <stdio.h>
# include <conio.h>
int main()
{
int c=0;
float x,y,xp,h,dy,i,n;
float f(float,float);
printf("Solution by Euler Method\n");
printf("Enter initial Boundry condition x,y : ");
scanf("%f%f",&x,&y);
printf("Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. \t X\t f(x,y) \t Y\n");
printf("----------------------------------------------------------\n");
for(i=x;i<=xp;i=i+h)
{
c++;
n=y+h*f(i,y);
printf("%2d\t %2.3f\t %5.5f\t %5.6f\n",c,i,f(i,y),n);
y=n;
}
printf("----------------------------------------------------------\n");
printf("Value of y @ %f is %f",xp,n);
getch();
}
float f(float x,float y)
{
return (y-x)/(y+x);
}

website_caption1

You might be also interested in :

Code for Waddle’s Rule method in C

C code to implement  Waddle’s Rule  method .  Compiled in DEV C++

website_caption1

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
 float f(float x)
  {
        float y;
        y=1/(1+x*x);
        return(y);
  }                          
main()
{
      float a,b,h,s1=0,s2=0,s=0;
      int i,n,m;
      printf("Enter the value of upper limit= ");
      scanf("%f",&b);
       printf("Enter the value of lower limit= ");
      scanf("%f",&a);
       printf("Enter the value of n=");
      scanf("%d",&n);
      h=(b-a)/n;
      printf("h= %f",h);
      m=n/6;
      s=0;
      if(n%6==0)
      {
      for(i=1;i<=m;i++)
          {
          s=s+((3*h/10)*(f(a)+f(a+2*h)+5*f(a+h)+6*f(a+3*h)+f(a+4*h)+5*f(a+5*h)+f(a+6*h)));  
          a=a+6*h;  
          }
          printf("Result is : %f",s);
       }
        else
          {
              printf(" Weddle's rule is not applicable");
          }         
              getch();
   }

website_caption1

 You might be also interested in :

Code for Bisection method in C

C code to implement Bisection method .  Compiled in DEV C++

website_caption1

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
float f(float x)
{
float sum;
sum=pow(x,3)+x+3;
return sum;
}
main()
{
float a,b,c,y0,y1,m;
step:
printf("enter the value of a,b and c ");
scanf("%f%f%f",&a,&b,&c);
y1=f(a);
y0=f(b);
if(y0*y1>0)
           {
           goto step;
           }
step1:
m=(a+b)/2;
if(f(m)==0)
{
printf("the root is : %f",m);
exit(0);
}
else
     {
     if (y1*f(m)<0)
        b=m;
     else
        a=m;
     }
 if(a-b>c)
          {
                   goto step1;
                   }
                   printf("the root is :%f",m);
                   getch();
                   }

website_caption1
 You might be also interested in :

Code for Newton’s forward interpolation in C

C code to implement Newton’s forward interpolation .  Compiled in DEV C++

website_caption1

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
main()
{
  float x[20],y[20],f,s,h,d,p;
  int j,i,n;
  printf("enter the value of n :");
  scanf("%d",&n);
  printf("enter the elements of x:");
  for(i=1;i<=n;i++)
   {
        scanf("%f",&x[i]);
         }
                   printf("enter the elements of y:");
               for(i=1;i<=n;i++)
               {
              scanf("%f",&y[i]);                  
                                }
  h=x[2]-x[1];
  printf("Enter the value of f:");
  scanf("%f",&f);
s=(f-x[1])/h;
p=1;
d=y[1];                                  
for(i=1;i<=(n-1);i++)
 {
                   for(j=1;j<=(n-i);j++)
                    {
                          y[j]=y[j+1]-y[j];
                            
                    }
                    p=p*(s-i+1)/i;
                    d=d+p*y[1];
 }                         
printf("For the value of x=%6.5f THe value is %6.5f",f,d);
 getch();
}

 website_caption1

You might be also interested in :