Educational Blog: November 2014

Tuesday 18 November 2014

LAGRANGE'S INTERPOLATION Method For Finding X in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  float x[10],y[10],temp=1,f[10],sum,p;
  int i,n,j,k=0,c;
  clrscr();
  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 f(x) for finding 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-y[j])/(y[k]-y[j]));
      }
    }
    f[i]=x[i]*temp;
  }

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



 ------------------------OUT PUT-------------------------



how many record you will be enter: 4


enter the value of x0: 20


enter the value of f(x0): 0.342


enter the value of x1: 25


enter the value of f(x1): 0.423


enter the value of x2: 30


enter the value of f(x2): 0.500


enter the value of x3: 35


enter the value of f(x3): 0.650


Enter f(x) for finding x: 0.390


* * * x = 22.840574 * * *

RUNGE-KUTTA Method in C

#include<stdio.h>
#include <math.h>
#include<conio.h>
//dy/dx = y - x#define F(x,y)  (y)-(x)
void main()
{
  double y0,x0,y1,n,h,f,f1,k1,k2;
  int j;
  clrscr();
  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(; x0<n; x0=x0+h)
  {
    f=F(x0,y0);
    k1 = h * f;
    f1 = F(x0+h,y0+k1);
    k2 = h * f1;
    y1 = y0 + ( k1 + k2)/2;
    printf("\n\n  k1 = %.4lf ",k1);
    printf("\n\n  k2 = %.4lf ",k2);
    printf("\n\n  y(%.4lf) = %.3lf ",x0+h,y1);
    y0=y1;
  }
getch();
}

------------------------OUT PUT----------------------


Enter the value of x0: 0

Enter the value of y0: 2

Enter the value of h: 0.05

Enter the value of last point: 0.1


     k1 = 0.1000

     k2 = 0.1025

     y(0.0500) = 2.101

     k1 = 0.1026

     k2 = 0.1052

     y(0.1000) = 2.205

NEWTON'S BACKWARD DIFFERENCE INTERPOLATION Method in C

#include<stdio.h>
#include<conio.h>
#define MAX 10

int factorial(intvalue);

void main()
{
    FILE *fp;
    int number,i,j,k=0,m;
    float xvalue[MAX],yvalue[MAX],search;
    float differ[MAX][MAX],uvalue,hvalue,product,sum;
    fp=fopen("nwbdi.dat","w");
    clrscr();
    printf("\n\n");
    fprintf(fp,"\n\n");
    printf("How many numbers you want to enter for x  : ");
    fprintf(fp,"How many numbers you want to enter for x  : ");
    scanf("%d",&number);
    fprintf(fp,"%d",number);
    for(i=0;i<number;i++)
    {
        printf("\nEnter value for x(%d)  : ",i);
        fprintf(fp,"\nEnter value for x(%d)  : ",i);
        scanf("%f",&xvalue[i]);
        fprintf(fp,"%f",xvalue[i]);
        printf("\nEnter value for y(%d)  : ",i);
        fprintf(fp,"\nEnter value for y(%d)  : ",i);
        scanf("%f",&yvalue[i]);
        fprintf(fp,"%f",yvalue[i]);
    }
    printf("\nEnter any value of x for which you want to find y : ");
    fprintf(fp,"\nEnter any value of x for which you want to find y : ");
    scanf("%f",&search);
    fprintf(fp,"%f",search);
    if(search<xvalue[0] || search>xvalue[number-1])
    {
        printf("\n\nValue lies outside the given values of x ");
        fprintf(fp,"\n\nValue lies outside the given values of x ");
        getch();
        exit(1);
    }
    else
    {
        clrscr();
        printf("\n\nNEWTON'S BACKWARD DIFFERENCE INTERPOLATION ");
        fprintf(fp,"\n\nNEWTON'S BACKWARD DIFFERENCE INTERPOLATION ");
        for(j=0;j<number-1;j++)
        {
            for(i=j+1;i<number;i++)
            {
                if(j==0)
                {
                    differ[i][j]=yvalue[i]-yvalue[i-1];

                }
                else
                {
                    differ[i][j]=differ[i][j-1]-differ[i-1][j-1];
                }
            }
        }
        printf("\n\n");
        fprintf(fp,"\n\n");
        printf(" x       y   ");
        fprintf(fp," x       y   ");
        for(i=1;i<number;i++)
        {
            printf("   d^%dy(i) ",i);
            fprintf(fp,"   d^%dy(i) ",i);
        }
        printf("\n\n");
        fprintf(fp,"\n\n");
        for(i=0;i<number;i++)
        {
            printf(" %.2f    %.2f  ",xvalue[i],yvalue[i]);
            fprintf(fp," %.2f    %.2f  ",xvalue[i],yvalue[i]);
            for(j=0;j<i;j++)
            {
            printf("  %.4f  ",differ[i][j]);
            fprintf(fp,"  %.4f  ",differ[i][j]);
            }
            printf("\n");
            fprintf(fp,"\n");
        }
        for(i=0;i<number;i++)
        {
            if(search>xvalue[i])
            {
                k=k+1;
            }
        }
        hvalue=xvalue[1]-xvalue[0];
        uvalue=(search-xvalue[k])/hvalue;
        sum=yvalue[k];
        for(i=0;i<number-1 ;i++)
        {
            product=1;
            for(j=0;j<=i;j++)
            {
                product=product*(uvalue+j);
            }
            m=factorial(i+1);
            sum=sum+(differ[k][i]*product)/m;
        }
        printf("\n\n");
        printf("Interpolated value is  :  %f ",sum);
        fprintf(fp,"Interpolated value is  :  %f ",sum);
    }
    fclose(fp);
    getch();
}

int factorial(intvalue)
{
    int i,temp=1;
    for(i=value;i>=1;i--)
    {
        temp=temp*i;
    }
    return(temp);
}

NEWTON'S FORWARD DIFFERENCE Method in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  float x[10],y[10][10],sum,p,u,temp;
  int i,n,j,k=0,f,m;
  float fact(int);
  clrscr();
  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++)
  {
    for(j=0;j<n-i;j++)
    {
     y[i][j]=y[i-1][j+1]-y[i-1][j];
    }
  }
  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;
  u=(p-x[f])/(x[f+1]-x[f]);
  printf("\n\n u = %.3f ",u);

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

float fact(int a)
{
  float fac = 1;

  if (a == 0)
   return (1);
  else
   fac = a * fact(a-1);

  return(fac);
}


------------------------OUT PUT-----------------------


how many record you will be enter: 5


enter the value of x0: 2


enter the value of f(x0): 9


enter the value of x1: 2.25


enter the value of f(x1): 10.06


enter the value of x2: 2.5


enter the value of f(x2): 11.25


enter the value of x3: 2.75


enter the value of f(x3): 12.56


enter the value of x4: 3


enter the value of f(x4): 14


Enter X for finding f(x): 2.35

_____________________________________________________

  x(i)     y(i)     y1(i)    y2(i)    y3(i)    y4(i)
_____________________________________________________

 2.000    9.000     1.060    0.130   -0.010    0.020

 2.250    10.060    1.190    0.120    0.010

 2.500    11.250    1.310    0.130

 2.750    12.560    1.440

 3.000    14.000


 u = 0.400

 f(2.35) = 10.522240

NEWTON RAPHSON Method in C

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

int user_power,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1=0,x2=0,t=0;
float fx1=0,fdx1=0;

void main()
{

    clrscr();

    printf("\n\n\t\t\t PROGRAM FOR NEWTON RAPHSON GENERAL");

    printf("\n\n\n\tENTER THE TOTAL NO. OF POWER:::: ");
    scanf("%d",&user_power);

    for(i=0;i<=user_power;i++)
    {
        printf("\n\t x^%d::",i);
        scanf("%d",&coef[i]);
    }

    printf("\n");

    printf("\n\t THE POLYNOMIAL IS ::: ");
    for(i=user_power;i>=0;i--)//printing coeff.
    {
        printf(" %dx^%d",coef[i],i);
    }

    printf("\n\tINTIAL X1---->");
    scanf("%f",&x1);

    printf("\n ******************************************************");
    printf("\n ITERATION    X1    FX1    F'X1  ");
    printf("\n **********************************************************");

    do
    {
            cnt++;
            fx1=fdx1=0;
            for(i=user_power;i>=1;i--)
            {
                fx1+=coef[i] * (pow(x1,i)) ;
            }
            fx1+=coef[0];
            for(i=user_power;i>=0;i--)
            {
                fdx1+=coef[i]* (i*pow(x1,(i-1)));
            }
            t=x2;
            x2=(x1-(fx1/fdx1));

            x1=x2;

            printf("\n %d         %.3f  %.3f  %.3f ",cnt,x2,fx1,fdx1);

    }while((fabs(t - x1))>=0.0001);
    printf("\n\t THE ROOT OF EQUATION IS %f",x2);
    getch();
}

--------------------------OUTPUT----------------------------


            PROGRAM FOR NEWTON RAPHSON GENERAL


    ENTER THE TOTAL NO. OF POWER:::: 3

     x^0::-3

     x^1::-1

     x^2::0

     x^3::1


     THE POLYNOMIAL IS :::  1x^3 0x^2 -1x^1 -3x^0

     INTIAL X1---->3

 **************************************
 ITERATION    X1    FX1    F'X1
 **************************************
 1         2.192  21.000 26.000
 2         1.794  5.344  13.419
 3         1.681  0.980  8.656
 4         1.672  0.068  7.475
 5         1.672  0.000  7.384
 **************************************

     THE ROOT OF EQUATION IS 1.671700

SECANT Method in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define F(x) (x)*(x) - 4*(x) - 10
void main()
{
  float x1,x2,x3,f1,f2,t;
  clrscr();
  printf("\nEnter the value of x1: ");
  scanf("%f",&x1);
  printf("\nEnter the value of x2: ");
  scanf("%f",&x2);
  printf("\n______________________________________________\n");
  printf("\n    x1\t  x2\t  x3\t     f(x1)\t f(x2)");
  printf("\n______________________________________________\n");
  do
  {
  f1=F(x1);
  f2=F(x2);
  x3=x2-((f2*(x2-x1))/(f2-f1));
  printf("\n%f   %f   %f   %f   %f",x1,x2,x3,f1,f2);
  x1=x2;
  x2=x3;
  if(f2<0)
    t=fabs(f2);
  else
    t=f2;
  }while(t>ESP);
printf("\n______________________________________________\n");
printf("\n\nApp.root = %f",x3);
getch();
}


---------------------- OUT PUT -------------------------


Enter the value of x1: 4

Enter the value of x2: 2

___________________________________________________________

    x1        x2         x3         f(x1)       f(x2)
___________________________________________________________

4.000000   2.000000   9.000000   -10.000000   -14.000000
2.000000   9.000000   4.000000   -14.000000    35.000000
9.000000   4.000000   5.111111    35.000000   -10.000000
4.000000   5.111111   5.956522   -10.000000   -4.320987
5.111111   5.956522   5.722488   -4.320987     1.654063
5.956522   5.722488   5.741121    1.654063    -0.143084
5.722488   5.741121   5.741659   -0.143084    -0.004015
5.741121   5.741659   5.741657   -0.004015     0.000010
___________________________________________________________


App.root = 5.741657

FALSE POSITION Method in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define F(x) 3*(x) - 1 - cos(x)
void main()
{
  float x0,x1,x2,f1,f2,f0;
  int count=0;
  clrscr();
  do
  {
  printf("\nEnter the value of x0: ");
  scanf("%f",&x0);
  }while(F(x0) > 0);
  do
  {
  printf("\nEnter the value of x1: ");
  scanf("%f",&x1);
  }while(F(x1) < 0);
  printf("\n__________________________________________________________\n");
  printf("\n    x0\t       x1\t x2\t   f0\t   f1\t   f2");
  printf("\n__________________________________________________________\n");
  do
  {
  f0=F(x0);
  f1=F(x1);
  x2=x0-((f0*(x1-x0))/(f1-f0));
  f2=F(x2);
  printf("\n%f %f %f %f %f %f",x0,x1,x2,f0,f1,f2);
  if(f0*f2<0)
   {
    x1=x2;
   }
   else
   {
    x0 = x2;
   }
  }while(fabs(f2)>ESP);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",x2);
getch();
}


 -------------------- OUT PUT --------------------


Enter the value of x0: -1

Enter the value of x1: 1

__________________________________________________________

    x0         x1        x2        f0      f1      f2
__________________________________________________________

-1.000000 1.000000 0.513434 -4.540302 1.459698 -0.330761
0.513434  1.000000 0.603320 -0.330761 1.459698 -0.013497
0.603320  1.000000 0.606954 -0.013497 1.459698 -0.000527
0.606954  1.000000 0.607096 -0.000527 1.459698 -0.000021
__________________________________________________________


App.root = 0.607096

Bisection Method in C

#include<stdio.h>
#include <math.h>
#include<conio.h>
#define ESP 0.001
#define F(x) (x)*(x)*(x) + (x)*(x) + (x) + 7
void main()
{
  int i = 1;
  float x0,x1,x2;
  double f1,f2,f0,t;
  clrscr();
  printf("\nEnter the value of x0: ");
  scanf("%f",&x0);

  printf("\nEnter the value of x1: ");
  scanf("%f",&x1);
  printf("\n__________________________________________________________________\n");
  printf("\niteration\t x0\t       x1\t x2\t   f0\t   f1\t   f2");
  printf("\n___________________________________________________________________\n");
  do
  {
  x2=(x0+x1)/2;
  f0=F(x0);
  f1=F(x1);
  f2=F(x2);
  printf("\n%d %f %f %f %lf %lf %lf", i, x0,x1,x2,f0,f1,f2);
  if(f0*f2<0)
   {
    x1=x2;
   }
   else
   {
    x0=x2;
   }
   i++;
  }while(fabs(f2)>ESP);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",x2);
getch();
}


 ----------------------------OUT PUT---------------------------



Enter the value of x0: -2

Enter the value of x0: -1

Enter the value of x1: -2

__________________________________________________________

    x0         x1        x2        f0       f1      f2
__________________________________________________________

-1.000000 -2.000000 -1.500000 -5.000000 2.000000 -1.750000
-1.500000 -2.000000 -1.750000 -1.750000 2.000000  0.062500
-1.500000 -1.750000 -1.625000 -1.750000 0.062500 -0.859375
-1.625000 -1.750000 -1.687500 -0.859375 0.062500 -0.402344
-1.687500 -1.750000 -1.718750 -0.402344 0.062500 -0.170898
-1.718750 -1.750000 -1.734375 -0.170898 0.062500 -0.054443
-1.734375 -1.750000 -1.742188 -0.054443 0.062500  0.003967
-1.734375 -1.742188 -1.738281 -0.054443 0.003967 -0.025253
-1.738281 -1.742188 -1.740234 -0.025253 0.003967 -0.010647
-1.740234 -1.742188 -1.741211 -0.010647 0.003967 -0.003341
-1.741211 -1.742188 -1.741699 -0.003341 0.003967  0.000313
__________________________________________________________


App.root = -1.741699

Monday 17 November 2014

TRAPEZOIDAL RULE in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  float x[10],y[10],sum=0,h,temp;
  int i,n,j,k=0;
  float fact(int);
  clrscr();
  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]);
  }
  h=x[1]-x[0];
  n=n-1;
  for(i=0;i<n;i++)
  {
    if(k==0)
    {
     sum = sum + y[i];
     k=1;
    }
    else
     sum = sum + 2 * y[i];
   }
   sum = sum + y[i];
   sum = sum * (h/2);
   printf("\n\n  I = %f  ",sum);
getch();
}
---------------------------- OUTPUT ----------------------------
how many record you will be enter: 6


enter the value of x0: 7.47


enter the value of f(x0): 1.93


enter the value of x1: 7.48


enter the value of f(x1): 1.95


enter the value of x2: 7.49


enter the value of f(x2): 1.98


enter the value of x3: 7.50


enter the value of f(x3): 2.01


enter the value of x4: 7.51


enter the value of f(x4): 2.03


enter the value of x5: 7.52


enter the value of f(x5): 2.06


  I = 0.099652

SIMPSON'S 3/8 Method in C

#include<stdio.h> #include<conio.h> #include<math.h> void main() { float x[10],y[10],sum=0,h,temp; int i,n,j,k=0; float fact(int); clrscr(); 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]); } h=x[1]-x[0]; n=n-1; sum = sum + y[0]; for(i=1;i<n;i++) { if(k==0) { sum = sum + 4 * y[i]; k=1; } else { sum = sum + 2 * y[i]; k=0; } } sum = sum + y[i]; sum = sum * (h/3); printf("\n\n I = %f ",sum); getch(); }
-------------------------- OUTPUT ----------------------
how many record you will be enter: 5


enter the value of x0: 0


enter the value of f(x0): 1


enter the value of x1: 0.25


enter the value of f(x1): 0.8


enter the value of x2: 0.5


enter the value of f(x2): 0.6667


enter the value of x3: 0.75


enter the value of f(x3): 0.5714


enter the value of x4: 1


enter the value of f(x4): 0.5


       I = 0.693250

Simpson's 1/3 Method in C

#include<stdio.h> #include<conio.h> #include<math.h> void main() { float x[10],y[10],sum=0,h,temp; int i,n,j,k=0; float fact(int); clrscr(); 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]); } h=x[1]-x[0]; n=n-1; sum = sum + y[0]; for(i=1;i<n;i++) { if(k==0) { sum = sum + 4 * y[i]; k=1; } else { sum = sum + 2 * y[i]; k=0; } } sum = sum + y[i]; sum = sum * (h/3); printf("\n\n I = %f ",sum); getch(); }
-------------------------- OUTPUT ----------------------
how many record you will be enter: 5


enter the value of x0: 0


enter the value of f(x0): 1


enter the value of x1: 0.25


enter the value of f(x1): 0.8


enter the value of x2: 0.5


enter the value of f(x2): 0.6667


enter the value of x3: 0.75


enter the value of f(x3): 0.5714


enter the value of x4: 1


enter the value of f(x4): 0.5


       I = 0.693250