$(document).ready(function() { $('pre code').each(function(i, block) { hljs.highlightBlock(block); }); });

Numerical Techniques Programs

Numerical Techniques Programs for CS/IT (III rd Sem) students of UPTU affiliated Colleges
/*Program to find root of a POLYNOMIAL equation using
BISECTION Method
Program by Sayed Ahad Abbas
B.Tech.(II)-CS(A)
Anand Engg. College, Agra
*/
#include
#include
#include
#define MAX_ITR 20
int eqn[20];
int order;
float bisection(float);
void main()
{
static float A, B, mid, val=0;
float valA, valB;
float X, err=1;
int i, j, count=0;
clrscr ();
printf ( "Enter the order of the POLYNOMIAL eqn:" );
3
©Ahad
scanf ("%d", &order );
for ( i=order, j=0 ; i>=0; i--, j++)
{
printf ( "Enter coefft. of X^%d :", i );
scanf ( "%d", &eqn[j] );
}
for ( A=-1; valA<0; A++) valA= bisection ( A ); A--; for ( B=A-1; valB>0; B-- )
valB= bisection ( B );
while (err>0.2)
{
printf ("\nSince F(%.8f)=%.8f,F(%.8f)=%.8f", A, bisection(A), B, bisection(B));
printf ("\n=>The root lies between %.8f and %.8f.", A, B );
mid=(A+B)/2;
printf ("\nSo,(%.8f+%.8f)/2=%.8f", A, B, mid);
val= bisection (mid);
printf ("===>F(%.8f)=%.8f", mid, val);
if (val>0)
A=mid;
else if (val<0) B=mid; err=(A-B)*10000; count++; if ( count==MAX_ITR ) break; } if ( count==MAX_ITR ) printf ("\nSorry,since more than %d iterations performed.", MAX_ITR); else printf ("\nSince F(%.8f)=F(%.8f) correct to 4 decimal places,", A, B); printf ("\nHence the root is %f.", mid); getch(); } float bisection (float X) { int i,j; float val=0; 4 ©Ahad for ( i=order, j=0; i>=0; i--, j++ )
{
if (X==0)
{
if (i>0)
val+=0;
else
val+= eqn[j];
}
else
val+=eqn[j]*pow(X,i);
}
return(val);
}

/*
Program to find root of a POLYNOMIAL equation using
REGULA-FALSI (FALSE POSITION) Method
*/
#include
#include
#include
#define MAX_ITR 20
#define MAX_SIZE 40
int fx[MAX_SIZE];
int order;
float SolveEqn(float);
void main( )
{
static float A, B, val=0, x=0;
float valA, valB, X, err=1;

int i, j, diff;
int count, flag, positive=0;
char ch;
clrscr ( );
printf ( "Enter the order of the POLYNOMIAL eqn:" );
scanf ( "%d", &order );
for (i=order, j=0; i>=0; i--, j++ )
{
printf ( "Enter coefft. of X^%d :", i );
scanf ( "%d", &fx[j] );
}
for( i=0; i=0 )
positive++;
else
break;
}
if( positive==order )
flag=1;
else
flag=0;
printf ( "Want to give interval for the equation? (y/n):" );
scanf ( "%s", &ch );
if ( ch=='Y'||ch=='y' )
{
printf ( "Enter interval values:" );
scanf ( "%f %f", &B, &A );
}
else
{
for ( A=0; valA>0; )
{
valA = SolveEqn ( A );
if ( flag==1 )
A--;
else
A++;
}
A=A-1;

for ( B=A; val B<0; ) { valB= SolveEqn ( B ); if ( flag==1 ) B--; else B++; } B=B-1; } valA= SolveEqn ( A ); valB= SolveEqn ( B ); printf ( "\nPutting consequent values in function, we get F(%.1f)=%.3f, and F(%.1f)=%.3f.\n", A, valA, B, valB ); printf ( "=>Root lies between A=%.1f and B=%.1f.", A, B );
printf ( "\nNow, using REGULA FALSI method, we get following values:\n" );
for ( count=0; err>0.3; count++)
{
valA= SolveEqn ( A );
valB= SolveEqn ( B );
x= ( A* valB - B* valA )/( valB – valA ); //Regula Falsi Method
val= SolveEqn ( x );
diff= (int)( B - A );
if ( diff==0 )
{
if ( val>0 )
err= ( valA – val )*10000;
else
err= ( val - valB )*10000;
}
if ( val>0 )
{
if ( valA>0 )
A=x;
else
B=x;
}
else
©Ahad
{
if ( valA<0 ) A=x; else B=x; } printf ( "\nNow,A=%.5f and B=%.5f-->", A, B);
printf ( "F(A)=%.5f, F(B)=%.5f.", valA, valB );
printf ( "\n=>Root lies between A=%.5f and B=%.5f.", A, B );
if ( count== MAX_ITR )
break;
}
if ( count== MAX_ITR )
printf ( "\nSorry, too large number of iterations." );
else
printf ( "\nSince f[A]=f[A-1] correct to 4 decimal places," );
printf ( "\nSo, approx root=%.4f", x );
getch ( );
}
float SolveEqn ( float X )
{
int i, j;
float val=0;
for ( i= order, j=0; i>=0; i--, j++ )
{
if ( X==0 )
{
if ( i>0 )
val+=0;
else
val+= fx [ j ];
}
else
val+= fx [ j ]* pow ( X, i );
}
return ( val );
}

/*Program to find the root of a POLYNOMIAL equation using
---------------------------------------------------------------------------------------------------------------
NEWTON-RAPHSON'S Method
---------------------------------------------------------------------------------------------------------------*/
#include
#include
#include
#define MAX_ITR 20
#define MODE_FX 0
#define MODE_FDX 1
int fx[20], fdx[20];
int order;
float SolveEqn(float, int eqn[20], int mode);
void main()
{
static float A, B, val=0, x=0;
float valA, valB, X, err;

int i, j;
static int count=0, flag=0, positive=0;
char ch;
clrscr();
printf("Enter the order of the POLYNOMIAL eqn:");
scanf("%d",&order);
for(i=order, j=0; i>=0; i--, j++)
{
printf("Enter coefft. of X^%d :", i);
scanf("%d", &fx[j]);
}
for(i=0; i=0)
positive++;
else
break;
}
if (positive==order)
flag=1;
else
flag=0;
printf("Want to give interval for the equation? (y/n):");
scanf("%s", &ch);
if (ch=='Y' || ch=='y')
{
printf("Enter interval values:");
scanf("%f %f", &B, &A);
}
else
{
for(A=0; valA>0; )
{
valA= SolveEqn(A, fx, MODE_FX);
if (flag==1)
A--;
else
A++;
}
A=A-1;
for(B=A; valB<0; ) { valB= SolveEqn(B, fx, MODE_FX); if (flag==1) B--; else B++; } B=B-1; } valA= SolveEqn(A, fx, MODE_FX); valB= SolveEqn(B, fx, MODE_FX); for( i=0; ix )
err= ( val- x )*10000;
else
err= ( x- val )*10000;
x= val;
if ( count==MAX_ITR )
break;
count++;
} while (err>=0.5);
if (count == MAX_ITR)
printf("\nSorry, too large number of iterations.");
else
printf("\nSince X[%d]=X[%d] correct to 4 decimal places,", count, count-1);
printf("\nSo, approx root=%.4f", x);
getch();
}
float SolveEqn(float X, int eqn[ 20 ], int mode)
{
int i, j;
float val=0;
if (mode==0) //Solves function
{
for (i= order, j=0; i>=0; i--, j++ )
{
if (X==0)
{
if ( i>0 )
val+=0;
else
val+= eqn[ j ];
}
else
val+= eqn[ j ] * pow(X, i);
}
}
else //Solves differential of function
{
for( i=order-1, j=0; i>=0; i--, j++)
{
if (X==0)
{
if ( i>0 )
val+=0;
else
val+= eqn[ j ];
}
else
val+= eqn[ j ] * pow(X, i );
}
}
return ( val );
}


/*
C Program implementation of
------------------------------------------------------------------
-----------Newton's FW/BW Interpolation-----------------
------------------------------------------------------------------*/
#include"stdio.h"
#include"conio.h"
#include"math.h"
#define MAX_SIZE 25
float x[MAX_SIZE], fx[MAX_SIZE], diffA[MAX_SIZE][MAX_SIZE], tempA[MAX_SIZE];
void difference( int, int, float );
void main()
{
int i, j, numT, numD, locX, locY;
float X;
int MODE=1;
char *formula[]={"Newton's Forward difference formula",
"Newton's Backward difference formula"};
clrscr();
printf("--C Program implementation of Newton\'s Forward/Backward Interpolation--");
printf("\n_________________________________________________________________________");
printf("\n How many terms are there in table of X?(Maximum 8 allowed):");
scanf("%d", &numT);
numD= numT-1;
printf("Now enter the values of X and f(X):\n");
printf("____________________________________________________________________________");
printf("\n x:\t|\n");
printf("____________________________________________________________________________");
printf("\n f(x):\t|");
printf("\n----------------------------------------------------------------------------------------\n");
locX=11, locY=6;
for(i=0; ix[%d], We'll use %s.\n", X, i-1, X, i-2,
formula[MODE]);
printf("____________________________________________________________________________");
difference( numD, MODE, X);
getch( );
}
void difference(int numD, int MODE, float X)
{
float FnValue( int MODE, float X, int numT);
int i, j, k, l, tempD;
static int locX=0, locY=0;
float val=0;
char delta;
if (MODE==0)
delta='Δ';
else
delta='';
tempD=numD;
for(i=0; i0)
diffA[ i ][j-1]= tempA[j]- tempA[j-1];
}
tempD--;
}
printf("\nx | f(x)|");
for(i=0; i
#include
#include
#define MAX_SIZE 25
float x[MAX_SIZE], fx[MAX_SIZE], diffA[MAX_SIZE][MAX_SIZE], tempA[MAX_SIZE];
void difference( int, int, float);
void main()
{
int i, j, numT, numD, locX, locY;
float X;
int MODE=1;
char *formula[]={"Gauss's Forward difference formula",
"Gauss's Backward difference formula"};
clrscr();
printf("--C Program implementation of Gauss\'s Forward/Backward Interpolation--");
printf("\n________________________________________________________________________");
printf("\nHow many terms are there in table of X?(Maximum 8 allowed):>>>");
scanf("%d", &numT);
numD=numT-1;
printf("Now enter the values of X and f(X):\n");
printf("___________________________________________________________________________");
printf("\nx:\t|\n");
printf("___________________________________________________________________________");
printf("\nf(x):\t|");
printf("\n-------------------------------------------------------------------------------\n");
locX=11, locY=6;
for(i=0; i>>");
scanf("%f",&X);
for( i=0; i<=(numT/2); i++) { if(MODE==0) break; else if(X%.2f & %.1f<%.2f=>We'll use %s.\n", X, x[i-2], X, x[i-1], formula[MODE]);
printf("___________________________________________________________________________");
difference(numD,MODE,X);
getch( );
}
void difference(int numD, int MODE, float X)
{
float FnValue( int MODE, float X, int numT);
int i, j, k, l, tempD;
static int locX=0, locY=0;
float val=0;
char delta;
if(MODE==0)
delta='';
else
delta='Δ';
tempD= numD;
for( i=0; i0)
diffA[i][j-1]= tempA [ j ]- tempA [ j-1 ];
}
tempD--;
}
printf("\nx | f(x) |");
for( i=0; i1)
{
count=0;
k--;
}
count++;
if(i==0)
val=fx[k];
else if(i==1)
val+=P*diffA[i-1][k];
else
{
if(i>2)
{
if(MODE==0)
tempP2=(tempP1+1);
else if(MODE==1)
tempP2=(tempP1-1);
}
else
tempP2=1;
if(i%2==0)
{
j++;
if(MODE==0)
P=P*(P-j);
else
P=P*(P+j);
}
val+= (tempP2 * P * diffA [i-1] [k] )/fact(i);
}
}
return (val);
}
float fact (int num)
{
if(num<=1) return (num); else return (num * fact(num-1) ); } /* C Program implementation of BESSEL'S INTERPOLATION FORMULA ------------------------------------------------------------------------ */ #include
#include
#include
#define MAX_SIZE 25
float x[MAX_SIZE], fx[MAX_SIZE], diffA[MAX_SIZE][MAX_SIZE], tempA[MAX_SIZE];
void difference( int, float);
void main()
{
int i, j, numT, numD, locX, locY;
float X;
clrscr();
printf("------C Program implementation of Bessel\'s Interpolation Formula---------\n");
printf("___________________________________________________________________________");
printf("\nHow many terms are there in table of X?(Maximum 8 allowed):");
scanf("%d", &numT);
numD= numT-1;
printf("Now enter the values of X and f(X):\n");
printf("___________________________________________________________________________");
printf("\nx:\t |\n");
printf("___________________________________________________________________________");
printf("\nf(x):\t |");
printf("\n----------------------------------------------------------------------------------------\n");
locX=11, locY=6;
for (i=0; i0)
diffA[i][j-1]=tempA[j]-tempA[j-1];
}
tempD--;
}
printf("\nx | f(x)|");
for(i=0; i1)
{
count=0;
k--;
}
if(i==0)
TempDel= ( fx[k]+ fx[k+1] )/2;
else if((i+1)%2==1)
{
TempDel= ( diffA[i-1][k]+ diffA[i-1][k+1] )/2;
temP2=1;
}
else
{
TempDel= diffA[i-1][k];
temP2= P-0.5; // '(p-1/2)' term of eqn
}
if(i>1&&i%2==0)
{
j++;
temP3= temP3*(temP3-j);
}
if(i>3)
temP1= (P+1);
else
temP1=1;
if(i==0)
val= TempDel;
else if(i==1)
val+= temP2*TempDel;
else
val+= ( temP1*temP2*temP3*TempDel )/fact(i);
}
return( va l);
}
float fact(int num)
{
if(num==1)
return( num );
else
return( num*fact(num-1) );
}

/*-----------C Program implementation of-----------
STIRLING'S INTERPOLATION FORMULA
--------------------------------------------------------------*/
#include
#include
#include
#define MAX_SIZE 25
float x[MAX_SIZE], fx[MAX_SIZE], diffA[MAX_SIZE][MAX_SIZE], tempA[MAX_SIZE];
void difference (int, float);
void main( )
{
int i, j, numT, numD, locX, locY;
float X;
clrscr( );
printf("------C Program implementation of Stirling\'s Interpolation Formula--------\n");
printf("___________________________________________________________________________");
printf("\nHow many terms are there in table of X?(Maximum 8 allowed):");
scanf("%d", &numT);
numD= numT-1;
printf("Now enter the values of X and f(X):\n");
printf("___________________________________________________________________________");
printf("\nx:\t |\n");
printf("___________________________________________________________________________");
printf("\nf(x):\t |");
printf("\n-------------------------------------------------------------------------------\n");

locX=11, locY=6;
for(i=0; i0)
diffA[i][j-1]= tempA[j]- tempA[j-1];
}
tempD--;
}
printf("\nx | f(x)|");
for(i=0; i1)
{
count=0;
k--;
}
if(i==0)
val= fx[k];
else
{
if(i%2==1) // gets ODD values of Y for formula
Y=( diffA[i-1][k]+ diffA[i-1][k-1] )/2;
else
Y=diffA[i-1][k]; //gets EVEN values of Y for formula
if(i==1)
val+=P*Y;
else
val+= (P*Y)/fact(i); //previous iteration value of P is taken here
if(i%2==1)
P=P*temP;
else
P=P*( (pow(temP,2)-1)/temP );
}
}
return( val );
}
float fact(int num)
{
if(num==1)
return(num);
else
return( num*fact(num-1) );
}


/*---------------C Program implementation of-------------
NEWTON'S DIVIDED DIFFERENCE FORMULA
------------------------------------------------------------------*/
#include
#include
#include
#define MAX_SIZE 15
float x[MAX_SIZE], fx[MAX_SIZE], Dif_X[MAX_SIZE][MAX_SIZE], tempA[MAX_SIZE];
float Divisor[MAX_SIZE];
void difference(int, float);
void main( )
{
int i, j, numT, numD, locX, locY;
float X;
clrscr( );
printf("-----C Program implementation of Newton\'s Divided Difference Formula-----");
printf("\n________________________________________________________________________");
printf("\nHow many terms are there in table of X?(Maximum 8 allowed):");
scanf("%d", &numT);
numD= numT-1;
printf("Now enter the values of X and f(X):\n");
printf("___________________________________________________________________________");
printf("\nx:\t|\n");
printf("___________________________________________________________________________");
printf("\nf(x):\t|");
printf("\n----------------------------------------------------------------------------------------\n");
locX=11, locY=6;
for(i=0; i0)
Dif_X[i][j-1]= ( tempA[j]- tempA[j-1] )/( Divisor[j-1] );
}
tempD--;
}
printf("\nx | f(x)|");
for(i=0; i
#include
#include
#define MAX_SIZE 15
float x[MAX_SIZE], fx[MAX_SIZE];
float Value(float X, int numT);
void main()
{
int i, j, numT, numD, locX, locY;
float X, val=0;
clrscr();
printf("---------------C Program implementation of Lagrange\'s Formula----------------");
printf("\n________________________________________________________________________");
printf("\nHow many terms are there in table of X?(Maximum 8 allowed):");
scanf("%d", &numT);
printf("Now enter the values of X and f(X):\n");
printf("___________________________________________________________________________");
printf("\nx:\t|\n");
printf("___________________________________________________________________________");
printf("\nf(x):\t|");
printf("\n-------------------------------------------------------------------------------\n");
locX=11, locY=6;
for(i=0; i
#include
#include
#include
#define MAX_SIZE 25
#define SIMP1_3 0
#define SIMP3_8 1
void TrigFunc();
void PrintMenu(int);
void GetPolynomial(int);
void GetXYValues(int type , int numT, int opt);
float Simpson(int MODE, float h, int numT);
float SolveEqn(float eqn[MAX_SIZE], int order, float X);
float lB, uB;
int orderP, orderQ;
char TrigTerm[20][5];
char Eqn[20][10];
char DefTrig[6][5]={"Sin", "Cos", "Tan", "Cot", "Sec", "Cosec"};
float x[MAX_SIZE], y[MAX_SIZE], P[MAX_SIZE], Q[MAX_SIZE];
char *formula[]= {"Simpson's 1/3 Rule",
"Simpson's 3/8 Rule"};
char *ChosenOpt[]= {"POLYNOMIAL EQUATION FORM",
"P/Q FORM"};
int numT, locX, locY, MODE, order, ch;
float h, tempX;
void main()
{
while(1)
{
clrscr();
printf("\n\tC Program implementation of Simpson\'s Rule of Numerical Integration");
printf("\n-------------------------------------------------------------------------------");
printf("\n\t\t\tMain Menu");
PrintMenu(0);
}//while loop ends here
}
void Option(int opt, int ch)
{
int i, j;
float val=0;
switch(ch)
{
case 1:
case 2:
clrscr();
printf("----------------%s chosen--------------------\n", ChosenOpt[ch-1]);
if(opt==0)
{
printf("Lower range of the Integral=");
scanf("%f", &lB);
printf("Upper range of the integral=");
scanf("%f", &uB);
back1:
printf("\nNumber of intervals?=");
scanf("%d", &numT);
if(numT%3==0)//no of intervals=multiple of 3
MODE=SIMP1_3;
else if(numT%2==1)//no of intervals=odd
MODE=SIMP3_8;
else
{
printf("Sorry, the number of intervals should either be odd or a multiple of 3!");
printf("\nHit any key to try again...");
getch();
goto back1;
}
}
h=(float)((uB- lB)/(numT-1));
GetPolynomial(ch);
GetXYValues(ch, numT, opt);
val=Simpson(MODE, h, numT);
if(opt==0)
{
printf("\nUsing %s, we get Y=%f\n", formula[MODE], val);
getch();
}
break;
case 3:TrigFunc();
getch();
break;
case 4:
back2:
clrscr();
printf("--------------TABULAR VALUE ENTRY option chosen----------------");
printf("\nHow many terms are there in table of X?:");
scanf("%d", &numT);
if(numT%3==0)//no of intervals=multiple of 3
MODE=SIMP3_8;
else if(numT%2==1)//no of intervals=odd
MODE=SIMP1_3;
else
{
printf("Sorry, the number of intervals should be either odd or a multiple of 3!");
printf("\nHit any key to try again...");
getch();
goto back2;
}
printf("Enter the values of \'x\' and \'y\':\n");
printf("__________________________________________________________________________");
printf("\nx: | \n");
printf("__________________________________________________________________________");
printf("\ny: | ");
printf("\n-------------------------------------------------------------------------------\n");
locX=6, locY=5;
for(i=0; i=0; i--, j++)
{
if(X==0)
{
if(i>0)
val+=0;
else
val+=eqn[j];
}
else if(eqn[j]==0)
val+=0;
else
val+=eqn[j]*pow(X, i);
}
return(val);
}
float Simpson(int MODE, float h, int numT)
{
float FixedVal=0, valODD=0, valEVEN=0, Val=0;
int i, j;
FixedVal=y[0]+y[numT-1];
if(MODE==SIMP1_3)
{
for(i=1; i=0; i--, j++)
{
printf("Enter coefft. of X^%d :", i);
scanf("%f", &P[j]);
}
if(type==2)//p/q form
{
printf("\nEnter the order of the Q(x):");
scanf("%d", &orderQ);
for(i=orderQ, j=0; i>=0; i--, j++)
{
printf("Enter coefft. of X^%d :", i);
scanf("%f", &Q[j]);
}
}
}
void GetXYValues(int type, int numT, int opt)
{
int i, j;
float tX, numer, denom;
if(opt==0)
{
printf("\n________________________________________________________________________");
printf("\nx: | ");
}
//Saves value of X
for(i=0; i", i+1);
PrintMenu(1);
for(j=0; j Magni[j]=y[j];
printf("\nEnter Trignometric function(Sin,Cos,...):");
scanf("%s", TrigTerm[i]);
PrintMenu(1);
for(j=0; j EqnVal[i][j]=y[j];
}
for(i=0; i {
v=0;
for(j=0; j {
for(k=0; (strcmpi(TrigTerm[k], DefTrig[k]))!=0; k++)
continue;
switch(k)
{
case 0:trigVal=sin(EqnVal[j][i]);
break;
case 1:trigVal=cos(EqnVal[j][j]);
break;
case 2:trigVal=tan(EqnVal[j][i]);
break;
case 3:trigVal=1/tan(EqnVal[j][i]);
break;
case 4:trigVal=1/cos(EqnVal[j][i]);
break;
case 5:trigVal=1/sin(EqnVal[j][i]);
break;
}
v+=Magni[j]*trigVal;
}
y[i]=v;
}
clrscr();
printf("\n_______________________________________________________________________");
printf("\nX | ");
for(i=0; i printf(" %.3f", x[i]);
printf("\nY | ");
for(i=0; i printf(" %.3f", y[i]);
printf("\n-----------------------------------------------------------------------");
Val=Simpson(MODE, h, numT);
printf("\nThe value of the integral is %.8f", Val);
}

Contact Me

Name

Email *

Message *