You are on page 1of 27

False Position Method or Regula Falsi Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (x*x*x-5*x+1);
}
int main()
{
clrscr();
int itr=0, maxitr;
float x1,x2,x3,x4,aerr;
printf("\nProgram to find Root of an Equation by Regula falsi Method\n\n");
printf("\nEnter value of x0, x1, allowed error and maximum iteration\n");
scanf("%f %f %f %d", &x1, &x2, &aerr, &maxitr);
x3=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
printf("\n\nIn iteration %d, Value of x3 =\t%f",itr+1,x3);
do
{
if (f(x1)*f(x3)<0)
x2=x3;
else
x1=x3;
x4=x3;
x3=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
itr++;
printf("\nIn iteration %d Value of x=\t%f", itr+1,x3);
if (fabs(x4-x3)<aerr)
{
printf("\n\nAfter %d iteration, Root= %f", itr,x4);
getch();
return 0;
}
}
while (itr<maxitr);
printf("\n\nSolution does not converge Iteration not sufficient");
getch();
return 1;
}
LAGRANGE'S INVERSE INTERPOLATION METHOD

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

void main()
{
FILE *fp;
int number,i,j;
float xvalue[MAX],yvalue[MAX],search,product;
float sum=0;
fp=fopen("lgrninv.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 y for which you want to find x : ");
fprintf(fp,"\nEnter any value of y for which you want to find x : ");
scanf("%f",&search);
fprintf(fp,"%f",search);
for(i=0;i<number;i++)
{
product=1;
for(j=0;j<number;j++)
{
if(i!=j)
{
product=product*(search-yvalue[j])/(yvalue[i]-yvalue[j]);
}
}
sum=sum+xvalue[i]*product;
}
clrscr();
printf("\n\n\n\n");
fprintf(fp,"\n\n\n\n");
printf("LAGRANGE'S INVERSE INTERPOLATION METHOD ");
fprintf(fp,"LAGRANGE'S INVERSE INTERPOLATION METHOD ");
printf("\n\n");
fprintf(fp,"\n\n");
printf(" X Y ");
fprintf(fp," X Y ");
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]);
printf("\n");
fprintf(fp,"\n");
}
printf("\n\n");
fprintf(fp,"\n\n");
printf("Interpolated value is : %.4f ",sum);
fprintf(fp,"Interpolated value is : %.4f ",sum);
fclose(fp);
getch();
}

Polynomial addition, subtraction and multiplication.


#include<stdio.h>
#include<conio.h>
#include "poly.h"void main()
{
clrscr();

Polynomial *p1,*p2,*sum,*sub,*mul;

printf("\nENTER THE FIRST POLYNOMIAL.");


p1=createPolynomial();

printf("\nENTER THE SECOND POLYNOMIAL.");


p2=createPolynomial();

clrscr();

printf("\nFIRST : ");
displayPolynomial(p1);

printf("\nSECOND : ");
displayPolynomial(p2);

printf("\n\nADDITION IS : ");
sum=addPolynomial(p1,p2);
displayPolynomial(sum);

printf("\n\nSUBTRACTION IS : ");
sub=subPolynomial(p1,p2);
displayPolynomial(sub);

printf("\n\nMULTIPLICATION IS : ");
mul=mulPolynomial(p1,p2);
displayPolynomial(mul);

getch();
}

Program of matrix transpose and matrix multiplication in C


Programming
#include <stdio.h>

#define M 10
#define P 7
#define N 8

void ftrans( int a[][3], int b[][3] ) {


/*
* finds fast-transpose of a in b.
*/
int t[P+1];
int i, j, n, terms;
int temp, temp2;

n = a[0][1];
terms = a[0][2];
b[0][0] = n;
b[0][1] = a[0][0];
b[0][2] = terms;

if( terms <= 0 )


return;
for( i=0; i<n; ++i )
t[i] = 0;
for( i=1; i<=terms; ++i )
t[a[i][1]]++;
temp = t[0];
t[0] = 1;
for( i=1; i<n; ++i ) {
temp2 = t[i], t[i] = t[i-1]+temp, temp = temp2;
//printf( "t[%d] = %d.\n", i, t[i] );
}
for( i=1; i<=terms; ++i ) {
j = t[a[i][1]];
b[j][0] = a[i][1], b[j][1] = a[i][0], b[j][2] = a[i][2];
t[a[i][1]] = j+1;
}
}

void printMatrix( int a[][3] ) {


/*
* prints the matrix in the form of 3-tuples.
*/
int i;
int nterms = a[0][2];

printf( "rows=%d cols=%d vals=%d.\n", a[0][0], a[0][1], a[0][2] );


for( i=1; i<=nterms; ++i )
printf( "a[%d][%d] = %d.\n", a[i][0], a[i][1], a[i][2] );
putchar( '\n' );
}

void insert( int c[][3], int row, int col, int val ) {
/*
* insert or add the triplet (row,col,val) in c.
* update c[0][2] is necessary.
*/
int i, terms = c[0][2];

for( i=1; i<=terms && c[i][0]<row; ++i )


;
for( ; i<=terms && c[i][1]<col; ++i )
;
if( i<=terms && c[i][1] == col ) // already inserted.
c[i][2] += val;
else { // a new entry should be inserted at i.
c[i][0] = row; c[i][1] = col; c[i][2] = val;
c[0][2]++;
}
}

void mmult( int a[][3], int b[][3], int c[][3] ) {


/*
* c = a*b;
*/
int i, j, mn = M*N;
int aterms = a[0][2], bterms = b[0][2];
int rowsofb = b[0][0];
int *t = (int *)malloc( rowsofb*sizeof(int) );
int temp, temp2;
int arow, acol, aval, brow, browstart, browend;

c[0][0] = a[0][0];
c[0][1] = b[0][1];

// init c.for( i=0; i<=mn; ++i )


c[i][2] = 0;

// fill t[] : t[i] points to row of b where actual row i starts.// last+1 entry is
also maintained for easing loops.for( i=0; i<=rowsofb; ++i )
t[i] = 0;
for( i=1; i<=bterms; ++i )
t[b[i][0]]++;
temp = t[0];
t[0] = 1;
for( i=1; i<=rowsofb; ++i )
temp2 = t[i], t[i] = t[i-1]+temp, temp = temp2;

// now start mult.for( i=1; i<=aterms; ++i ) {


arow = a[i][0]; acol = a[i][1]; aval = a[i][2];
brow = acol;
browstart = t[brow]; browend = t[brow+1];
for( j=browstart; j<browend; ++j )
insert( c, arow, b[j][1], aval*b[j][2] );
}
}

int main() {
int a[][3] = {
/*{4,5,4},
{0,0,1},
{0,1,2},
{1,0,3},
{1,1,4}*/

{4,2,3},
{0,1,2},
{1,0,3},
{3,1,4}
/*{7,7,8},
{1,1,15},
{1,4,22},
{1,6,-15},
{2,2,11},
{2,3,3},
{3,4,-6},
{5,1,91},
{6,3,28}*/

};
int b[][3] = {
/*{5,3,4},
{0,1,5},
{0,2,6},
{1,1,7},
{1,2,8}*/

{2,3,3},
{0,2,5},
{1,0,7},
{1,1,6}
};
int c[M*N+1][3];

printMatrix(a);
//ftrans( a, b );
printMatrix(b);
mmult( a, b, c );
printMatrix(c);

return 0;
}

Secant for particular equation in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define E 0.001

float f (float x)
{
return (cos(x)-(x*exp(x)));
}
void main()
{
int ctr,temp,ctr1;
float res,res1;
int p,NO;
float X1=0.00,X2=0.00;
float X3,FX1,FX2,FX3;
int co[10];
clrscr();

//For Scanning X1 and X2

printf("\nEnter the Roots:1::");


flushall();
scanf("%f",&X1);

printf("\nEnter the Roots:2::");


flushall();
scanf("%f",&X2);

ctr1=0;
clrscr();

printf("\n==========================================================");
printf("\nNO\tX1\tFX1\tX2\tFX2\tX3\tFX3");
printf("\n==========================================================");

do
{

FX1=FX2=FX3=0;
FX1 = f(X1);
FX2 = f(X2);

X3=((X1*FX2)-(X2*FX1))/(FX2-FX1);

FX3 = f(X3);

printf("\n%d\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t
%.4f",ctr1+1,X1,FX1,X2,FX2,X3,FX3);

X1=X2;
FX1=FX2;
X2=X3;
FX2=FX3;
ctr1++;
}while(fabs(FX3)>=E );

printf("\n\n%.4f",X3);

getch();
}

Code for Graeffe Method in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float coe[10],sq[10],mul[10]={0},ans[10],f_ans[10];
float temp,div=0.5;
int c,po,ctr,ctr1,ctr2,N;
clrscr();

printf("\nEnter the Highest Power::");


flushall();
scanf("%d",&po);
N=po;

//Scanning Equationfor(ctr=po;ctr>=0;ctr--)
{
printf("\nEnter the value of %d= ",ctr);
scanf("%f",&coe[ctr]);
}

do
{ c=po;
//Squaring the value:for(ctr=po;ctr>=0;ctr--)
{
sq[ctr]=coe[ctr]*coe[ctr];
}
//Multiplying & placing in another arrayfor(ctr=po,ctr1=po-
1;ctr>=0,ctr1>0;ctr--,ctr1--)
{
mul[ctr1]=(coe[ctr]*coe[ctr-2])*(-2);
}
//Adding the sq[] with mul[]for(ctr=po;ctr>=0;ctr--)
{
ans[ctr]=sq[ctr]+mul[ctr];
}
//Dividing the valuesfor(ctr=0;ctr<=po;ctr++)
{
temp=ans[ctr]/ans[ctr+1];
f_ans[ctr]=pow(temp,div);
}
for(ctr=0;ctr<=po;ctr++)
{
printf("\n%.4f",f_ans[ctr]);
}
div=div/2;
c--;

}
while(c!=0);
getch();
}

NEWTON'S BACKWARD DIFFERENCE INTERPOLATION in C


Programming

#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 DIVIDED DIFFERENCE INTERPOLATION in C Programming

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

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("nwddi.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 DIVIDED DIFFERENCE INTERPOLATION ");
fprintf(fp,"\n\nNEWTON'S DIVIDED DIFFERENCE INTERPOLATION ");
for(j=0;j<number-1;j++)
{
for(i=0;i<number-(j+1);i++)
{
if(j==0)
{
differ[i][j]=(yvalue[i+1]-yvalue[i])/(xvalue[i+1]-xvalue[i]);
}
else
{
differ[i][j]=(differ[i+1][j-1]-differ[i][j-1])/(xvalue[i+(j+1)]-
xvalue[i]);
}
}
}
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<number-(i+1);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;
}
}
k=k-1;
sum=yvalue[k];
for(i=0;i<number-(k+1);i++)
{
product=1;
for(j=0;j<=i;j++)
{
product=product*(search-xvalue[j+k]);
}
sum=sum+(differ[k][i]*product);

}
printf("\n\n");
fprintf(fp,"\n\n");
printf("Interpolated value is : %f ",sum);
fprintf(fp,"Interpolated value is : %f ",sum);
}
fclose(fp);
getch();
}

SUCCESSIVE APPROXIMATION METHOD in C Programming

#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,t=0;
float x2=0;

void main()
{
clrscr();

printf("\n\n\t\t\t PROGRAM FOR SUCESSIVE APPROXIMATION");

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

/*************************************************************************/

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

do
{
cnt++;
x1=x2;
x2=(2-(log10(x1)));
printf("\n %d %.3f %.3f ",cnt,x1,x2);

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

/*******************************OUTPUT**********************************/

PROGRAM FOR SUCESSIVE APPROXIMATION

INTIAL X1---->10

********************************
ITERATION X1 FX1
********************************
1 10.000 1.000
2 1.000 2.000
3 2.000 1.699
4 1.699 1.770
5 1.770 1.752
6 1.752 1.756
7 1.756 1.755
8 1.755 1.756
9 1.756 1.756
********************************

THE ROOT OF EQUATION IS 1.755633

ADAM-BASHFORTH METHOD in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 20

float equation(float x,float y)


{
return(1+(y*y));

void main()
{
FILE *fp;
int i=0,count=-1;
float lower,upper,h,y1,xvalue[MAX],yvalue[MAX],result;
float function[MAX],search,final,temp;
fp=fopen("admbsh.dat","w");
clrscr();
printf("ADAM-BASHFORTH METHOD ");
fprintf(fp,"ADAM-BASHFORTH METHOD ");
printf("\n");
fprintf(fp,"\n");
printf("\nEnter the lower bound of x : ");
fprintf(fp,"\nEnter the lower bound of x : ");
scanf("%f",&lower);
fprintf(fp,"%f",lower);
printf("\nEnter the upper bound of x : ");
fprintf(fp,"\nEnter the upper bound of x : ");
scanf("%f",&upper);
fprintf(fp,"%f",upper);
printf("\nEnter the value of y(lower) : ");
fprintf(fp,"\nEnter the value of y(lower) : ");
scanf("%f",&y1);
fprintf(fp,"%f",y1);
printf("\nEnter the value of h : ");
fprintf(fp,"\nEnter the value of h : ");
scanf("%f",&h);
fprintf(fp,"%f",h);
printf("\nEnter the value of x for which you want to find y :");
fprintf(fp,"\nEnter the value of x for which you want to find y :");
scanf("%f",&search);
fprintf(fp,"%f",search);
xvalue[i]=lower;
yvalue[i]=y1;
for(i=0;xvalue[i]<=upper;i++)
{
xvalue[i+1]=xvalue[i]+h;
}
for(i=0;xvalue[i]<=upper;i++)
{
result=equation(xvalue[i],yvalue[i]);
yvalue[i+1]=yvalue[i]+(h*result);
}
printf("\n\n");
fprintf(fp,"\n\n");
printf("The table is ");
fprintf(fp,"The table is ");
printf("\n\n");
fprintf(fp,"\n\n");
printf(" i x y f(x,y) ");
fprintf(fp," i x y f(x,y) ");
printf("\n\n");
fprintf(fp,"\n\n");
for(i=0;xvalue[i]<=upper;i++)
{
function[i]=equation(xvalue[i],yvalue[i]);
printf(" %d. %.4f %.4f %.4f ",i,xvalue[i],yvalue[i],function[i]);
fprintf(fp," %d. %.4f %.4f %.4f ",i,xvalue[i],yvalue[i],function[i]);
count=count+1;
printf("\n");
fprintf(fp,"\n");
}
yvalue[search]=yvalue[count]+(h/24)*((-9*function[count-3])+(37*function[count-
2])-(59*function[count-1])+(55*function[count]));
final=equation(search,yvalue[search]);
yvalue[search]=yvalue[count]+(h/24)*((function[count-2])-(5*function[count-1])
+(19*function[count])+(9*final));
printf("\n\n");
fprintf(fp,"\n\n");
printf("Approximate value is : %.4f ",yvalue[search]);
fprintf(fp,"Approximate value is : %.4f ",yvalue[search]);
fclose(fp);
getch();
}

BISECTION METHOD USING LOG10(X)-COS(X) in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define epsil 0.0001
int p,c[10];

void appro();
void bisection(float,float,float,float);

float fun(float x)
{
int i;
float y=0.0;
y=log10(x)-cos(x);
return y;
}

void main()
{
int i;
clrscr();
appro();
getch();
}

void bisection(float x1,float x2,float fx1,float fx2)


{
float x3,fx3;
int n=1;
if(fx1<0&&fx2>0||fx1>0&&fx2<0)
{
printf("******************************************************************\n");
printf("\nIt x1 fx1 x2 fx2 x3 fx3\n");
printf("******************************************************************\n");
do
{
x3=(x1+x2)/2;
fx3=fun(x3);
if(fx3==0)
{
break;
}
if((fx1*fx3)>0)
{
x1=x3;
fx1=fx3;
}
else
{
x2=x3;
fx2=fx3;
}
printf("%d %.4f %.4f %.4f %.4f %.4f
%.4f\n",n,x1,fx1,x2,fx2,x3,fx3);
n=n+1;
}while((fabs(x1-x2)>epsil)&&(fx3!=0));
printf("\n\t\t********************\n");
printf("\n\t\tThe root is %.4f\n",x3);
printf("\t\t********************\n");
}
else
{
clrscr();

printf("\nError in the Initial approximation\n ");


printf("\nPlease enter right approximation\n\n");
appro();
}
}
void appro()
{
float x1,x2,x3,fx1,fx2,fx3;
printf("\nPlease Enter first approximation: ");
scanf("%f",&x1);
printf("\nPlease Enter first approximation: ");
scanf("%f",&x2);
fx1=fun(x1);
fx2=fun(x2);
bisection(x1,x2,fx1,fx2);
}
/*************************************OUTPUT**************************************

Please Enter first approximation: 1

Please Enter first approximation: 2


******************************************************************

It x1 fx1 x2 fx2 x3 fx3


******************************************************************
1 1.0000 -0.5403 1.5000 0.1054 1.5000 0.1054
2 1.2500 -0.2184 1.5000 0.1054 1.2500 -0.2184
3 1.3750 -0.0562 1.5000 0.1054 1.3750 -0.0562
4 1.3750 -0.0562 1.4375 0.0247 1.4375 0.0247
5 1.4062 -0.0157 1.4375 0.0247 1.4062 -0.0157
6 1.4062 -0.0157 1.4219 0.0045 1.4219 0.0045
7 1.4141 -0.0056 1.4219 0.0045 1.4141 -0.0056
8 1.4180 -0.0006 1.4219 0.0045 1.4180 -0.0006
9 1.4180 -0.0006 1.4199 0.0020 1.4199 0.0020
10 1.4180 -0.0006 1.4189 0.0007 1.4189 0.0007
11 1.4180 -0.0006 1.4185 0.0001 1.4185 0.0001
12 1.4182 -0.0003 1.4185 0.0001 1.4182 -0.0003
13 1.4183 -0.0001 1.4185 0.0001 1.4183 -0.0001
14 1.4184 -0.0000 1.4185 0.0001 1.4184 -0.0000

********************
The root is 1.4184
********************
************************************************************************/

SECANT METHOD USING e(x)-3x in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define epsil 0.00001

int p,c[10];

void appro();
voidfalse(float,float,float,float);

void main()
{
clrscr();
appro();
getch();
}

voidfalse(float x1,float x2,float fx1,float fx2)


{
float x3,fx3,temp;
int n=1;
printf("******************************************************************\n");
printf("\nIt x1 fx1 x2 fx2 x3 fx3\n");
printf("******************************************************************\n");
do
{
temp=x3;
x3=(((x1*fx2)-(x2*fx1))/(fx2-fx1));
fx3=(exp(x3)-(3*(x3)));
x1=x2;
fx1=fx2;
x2=x3;
fx2=fx3;
printf("%d %.4f %.4f %.4f %.4f %.4f
%.4f\n",n,x1,fx1,x2,fx2,x3,fx3);
n=n+1;
}while((fabs(temp-x3)>=epsil)&&(fx3!=0));
printf("\n\t\t********************\n");
printf("\n\t\tThe root is %.4f\n",x3);
printf("\t\t********************\n");
}

void appro()
{
float x1,x2,x3,fx1,fx2,fx3;
printf("\nPlease Enter first approximation: ");
scanf("%f",&x1);
printf("\nPlease Enter second approximation: ");
scanf("%f",&x2);
fx1=(exp(x1)-(3*(x1)));
fx2=(exp(x2)-(3*(x2)));
false(x1,x2,fx1,fx2);
}

FALSE POSITION METHOD USING x1-exp(-x1) in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define epsil 0.00001
int p,c[10];

void appro();
voidfalse(float,float,float,float);

void main()
{
clrscr();
appro();
getch();
}

voidfalse(float x1,float x2,float fx1,float fx2)


{
float x3,fx3,temp;
int n=1;
if(fx1<0&&fx2>0||fx1>0&&fx2<0)
{
printf("******************************************************************\n");
printf("\nIt x1 fx1 x2 fx2 x3 fx3\n");
printf("******************************************************************\n");
do
{
temp=x3;
x3=(((x1*fx2)-(x2*fx1))/(fx2-fx1));
fx3=x3-exp(-x3);
if(fx3==0)
{
break;
}
if((fx1*fx3)>0)
{
x1=x3;
fx1=fx3;
}
else
{
x2=x3;
fx2=fx3;
}
printf("%d %.4f %.4f %.4f %.4f %.4f
%.4f\n",n,x1,fx1,x2,fx2,x3,fx3);
n=n+1;
}while((fabs(temp-x3)>epsil)&&(fx3!=0));
printf("\n\t\t********************\n");
printf("\n\t\tThe root is %.4f\n",x3);
printf("\t\t********************\n");
}
else
{
clrscr();

printf("\nError in the Initial approximation\n ");


printf("\nPlease enter right approximation\n\n");
appro();
}
}
void appro()
{
float x1,x2,x3,fx1,fx2,fx3;
printf("\nPlease Enter first approximation: ");
scanf("%f",&x1);
printf("\nPlease Enter second approximation: ");
scanf("%f",&x2);
fx1=x1-exp(-x1);
fx2=x2-exp(-x2);
false(x1,x2,fx1,fx2);
}

SUCESSIVE APPROXIMATION METHOD in C Programming

#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 SUCESSIVE APPROXIMATION");

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;
t=x1;
for(i=user_power;i>=0;i--)
{
fdx1+=coef[i]* (i*pow(x1,(i-1)));
}
printf("\n %d %.3f %.3f %.3f ",cnt,x1,fx1,fdx1);
x1=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

Falsi position for particular equation in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define E 0.001

float f (float x)
{
return (x-exp(x*-1));
}
void main()
{
int ctr,temp,ctr1;
float res,res1;
int p,NO;
float X1=0.00,X2=0.00,prevX3;
float X3,FX1,FX2,FX3;
int co[10];
clrscr();

//For Scanning X1 and X2

printf("\nEnter the Roots:1::");


flushall();
scanf("%f",&X1);

printf("\nEnter the Roots:2::");


flushall();
scanf("%f",&X2);

//Greater than Zero roots

FX1=f(X1);
FX2=f(X2);

if((FX1 * FX2)>0)
{
printf("\nThis is not Proper Approximation");

ctr1=0;
clrscr();

printf("\n==========================================================");
printf("\nNO\tX1\tFX1\tX2\tFX2\tX3\tFX3");
printf("\n==========================================================");

do
{

prevX3=X3;

X3 = ((X1*FX2) - (X2*FX1)) / (FX2 - FX1);

FX1=FX2=FX3=0.0;

FX3 = f(X3);
FX1 = f(X1);
FX2 = f(X2);

printf("\n%d\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t
%.4f",ctr1+1,X1,FX1,X2,FX2,X3,FX3);

if((FX1 * FX3)>0)
{
X1=X3;

}
elseif(FX3 == 0)
{
printf("%.4f",X3);
break;
}
else
{
X2=X3;

ctr1++;
}
while(fabs(prevX3-X3)>E);

printf("\n\n\tThe Root:: %.4f",X3);

getch();
}

SECANT METHOD OF PARTICULAR EQUATION IS log(x)-cos(x) in C


Programming

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define e 0.0001

int i=0,cnt=0,flag=0;
float x1,x2,x3=0,t=0;
float fx1=0,fx2=0,fx3=0,temp=0;

float func(float f)
{
return(log10(f)-cos(f));
}

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

printf("\n\tINTIAL X2---->");
scanf("%f",&x2);

fx1=fx2=fx3=0.0;

if( ((func(x1)*func(x2)))>0)
{
printf("\n\t INTIAL VALUES ARE NOT PERFECT.");
return(1);
}
return(0);
}

void main()
{
clrscr();
printf("\n\n\t\t PROGRAM FOR SECANT METHOD");

printf("\n\t PARTICULAR EQUATION IS ::: log(x)-cos(x)");

while(1)
{
if(check()==0)
{
flag=1;
break;
}
check();
}

printf("\n ******************************************************");
printf("\n ITERATION X1 FX1 X2 FX2 X3 FX3 ");
printf("\n **********************************************************");

if(flag==1)
{
do
{
cnt++;
fx1=fx2=fx3=0;
fx1=func(x1);
fx2=func(x2);
x3=((x1*fx2)-(x2*fx1))/(fx2-fx1);
fx3=func(x3);

printf("\n %d %.4f %.4f %.4f %.4f %.4f


%.4f",cnt,x1,fx1,x2,fx2,x3,fx3);
x1=x2;
x2=x3;
getch();
}while(fabs(x2 - x1)>=e);
printf("\n\t ROOT OF EQUATION IS %f",x3);
}
getch();
}

/*******************************OUTPUT************************************

PROGRAM FOR SECANT METHOD

PARTICULAR EQUATION IS ::: log(x)-cos(x)

INTIAL X1---->1.4

INTIAL X2---->1.45

******************************************************
ITERATION X1 FX1 X2 FX2 X3 FX3
******************************************************
1 1.4000 -0.0238 1.4500 0.0409 1.4184 0.0000
2 1.4500 0.0409 1.4184 0.0000 1.4184 0.0000
******************************************************

ROOT OF EQUATION IS 1.418406

You might also like