Professional Documents
Culture Documents
NM Programming
NM Programming
#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 *p1,*p2,*sum,*sub,*mul;
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();
}
#define M 10
#define P 7
#define N 8
n = a[0][1];
terms = a[0][2];
b[0][0] = n;
b[0][1] = a[0][0];
b[0][2] = terms;
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];
c[0][0] = a[0][0];
c[0][1] = b[0][1];
// 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;
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;
}
#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();
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();
}
#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();
//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();
}
#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);
}
#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();
}
#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\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**********************************/
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
********************************
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 20
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();
}
#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();
}
********************
The root is 1.4184
********************
************************************************************************/
#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();
}
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);
}
#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();
}
#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();
for(i=0;i<=user_power;i++)
{
printf("\n\t x^%d::",i);
scanf("%d",&coef[i]);
}
printf("\n");
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**********************************
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
**************************************
#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();
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;
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);
getch();
}
#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");
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);
/*******************************OUTPUT************************************
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
******************************************************