C Program for Langrange Interpolating Polynomial
/* L.I.P */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
float **x;
void main()
{
float n,i,j,*l,*f,R,a;
clrscr();
puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-");
puts("Lagrange Interpolating Polynomial");
puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-");
printf("How many Values you are Entering");
scanf("%f",&n);
puts("-------------------------------------------------------");
x=(float**)malloc(((n+1)*2)*4);
l=(float*)malloc(n*4);
printf("Enter Value of xi and f(xi) Simultaneously");
for(i=0;i<n;i++)
{
scanf("%f %f",&x[i][0],&x[i][1]);
}
puts("-------------------------------------------------------");
printf("Enter value of x");
scanf("%f",&a);
puts("-------------------------------------------------------");
for(i=0;i<n;i++)
{
l[i]=1;
for(j=0;j<n;j++)
{
if(i!=j)
l[i]*=(a-x[j][0])/(x[i][0]-x[j][0]);
}
}
R=0;
for(i=0;i<n;i++)
{
R+=l[i]*x[i][1];
}
printf("Result=%f",R);
puts("\n-------------------------------------------------------");
getch();
Output:-
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*Lagrange Interpolating Polynomial
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*How many Values you are Entering
3
------------------------------------------------------Enter Value of xi and f(xi) Simultaneously
10
4 1.386294
6 1.791760
------------------------------------------------------Enter value of x
2
------------------------------------------------------Result=0.565844
-------------------------------------------------------
C Program for Polynomial Regression
/* Polynmial Regresion */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
float *x,*y,n,i,x1=0,x12=0,y1=0,x1y1=0,a1,a0,e;
printf("How many values You are Entering");
scanf("%f",&n);
x=(float*)malloc(n*4);
y=(float*)malloc(n*4);
puts("Enter coressponding Elements X & Y");
for(i=0;i<n;i++)
{
scanf("%f %f",&x[i],&y[i]);
x1+=x[i];
y1+=y[i];
x1y1+=(x[i]*y[i]);
x12+=(x[i]*x[i]);
}
a1=(n*x1y1-x1*y1)/(n*x12-x1*x1);
a0=y1/n-a1*x1/n;
e=y[0]-a0-a1*x[0];
printf("ao=%f\na1=%f\ne%f",a0,a1,e);
}
===========================================
Output of Program:How many values You are Entering
6
Enter coressponding Elements X & Y
0 2.1
1 7.7
2 13.6
3 27.2
4 40.4
5 61.1
Matrix
6.000000 15.000000 55.000000 152.100006
15.000000 55.000000 225.000000 583.599976
55.000000 225.000000 979.000000 2480.800049
Required Equation
Y=4.632166+2.271759X+1.869648X2
C Program for Newton Raphson Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<alloc.h>
#include<process.h>
float value(float);
float **e,n;
float deri(float);
void main()
{
int i,p,it=50;
float v,xo,es=0,s,x1,ea;
char ch;
clrscr();
printf("Enter Degree of the Equation\n");
scanf("%f",&n);
puts("--------------------------------------------------");
e=(float**)malloc(((n+1)*2)*sizeof(float));
printf("Enter coeff.in decreasing order of Degree\n");
for(i=0,p=n;i<=n;i++,p--)
{
scanf("%f",&e[i][0]);
e[i][1]=p;
}
puts("-------------------------------------------------");
printf("Enter Value of Xo\n");
scanf("%f",&xo);
puts("-------------------------------------------------");
printf("What would You Like to enter Es or Sig.fig or Iteration =>E/S/I\n");
ch=getche();
puts("\n----------------------------------------------);
if(ch=='E'||ch=='e')
{
printf("Enter value of Es\n");
scanf("%f",&es);
}
else if(ch=='s'||ch=='S')
{
printf("Enter number of Sig. figure\n");
scanf("%f",&s);
es=0.5*pow(10,(s-2));
}
else
{
printf("Enter no. of Iteration\n");
scanf("%d",&it);
}
i=0;
puts("-------------------------------------------------");
printf("Iteration\tX\t\tEa");
while(1)
{
i++;
x1=xo-value(xo)/deri(xo);
ea=(x1-xo)*100/x1;
if(ea<0)
ea=ea*(-1);
if(ea<es||value(x1)==0||i==it)
{
printf("\n%3d\t\t%6.5f\t\t%5.3f",i,x1,ea);
puts("\n---------------------------------------------");
getch();
exit(0);
}
printf("\n%3d\t\t%6.5f\t\t%5.3f",i,x1,ea);
xo=x1;
}
float value(float x)
{
int i;
float s=0;
for(i=0;i<=n;i++)
{
s=s+e[i][0]*pow(x,e[i][1]);
}
return s;
}
float deri(float x)
{
int i;
float s=0,t,h;
for(i=0;i<=n;i++)
{
t=e[i][0]*e[i][1];
if(e[i][1]>0)
h=e[i][1]-1;
else
h=0;
s=s+t*pow(x,h);
}
return s;
Output Of Program:-
First:Enter Degree of the Equation
2
-------------------------------------------------Enter coeff.in decreasing order of Degree
-1 1.8 2.5
------------------------------------------------Enter Value of Xo
5
------------------------------------------------What would You Like to enter Es or Sig.fig or Iteration =>E/S/I
e
------------------------------------------------Enter value of Es
0.05
------------------------------------------------Iteration X Ea
1 3.35366 49.091
2 2.80133 19.717
3 2.72111 2.948
4 2.71934 0.065
5 2.71934 0.000
-----------------------------------------------
Second:Enter Degree of the Equation
3
-------------------------------------------------Enter coeff.in decreasing order of Degree
2 -11.7 17.7 -5
------------------------------------------------Enter Value of Xo
3
------------------------------------------------What would You Like to enter Es or Sig.fig or Iteration =>E/S/I
i
------------------------------------------------Enter no. of Iteration
3
------------------------------------------------Iteration X Ea
1 5.13333 41.558
2 4.26975 20.226
3 3.79293 12.571
-----------------------------------------------
C Program for Bisection Method
/*Bisection Method*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<math.h>
#include<process.h>
#define f(x) log10(x*x*x)-0.7
double value(double);
void main()
{
double x,v,v1,v2,xr,xu,xl,xo=0,ea,et,tv;
double i,p,it=50;
clrscr();
puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
puts("Finding Root By Using Bisection Method");
puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
puts("--------------------------------------------------");
puts("Enter value of xl and xu ");
scanf("%lf %lf ",&xl,&xu);
puts("--------------------------------------------------");
puts("Enter True Value and Iterations");
scanf("%lf %lf",&tv,&it);
puts("--------------------------------------------------");
i=0;
puts("Iteration\t Xr \t\t Ea\t\tEt");
while(1)
{
i++;
v1=f(xl);
v2=f(xu);
if(v1*v2>=0)
{
puts("Invalid Value of xl and xu");
getch();
exit(0);
}
xr=(xl+xu)/2.0;
ea=(xr-xo)*100/xr;
et=(tv-xr)*100/tv;
if(ea<0)
ea=ea*-1;
v=f(xr);
if(v==0||i==it)
{
printf("\n%3d\t\t%6.5lf\t\t%5.6lf\t\t%5.6lf",(int)i,xr,ea,et);
puts("\n----------------------------------------------");
getch();
exit(0);
}
printf("\n%3d\t\t%6.5lf\t\t%5.6lf\t\t%5.6lf",(int)i,xr,ea,et);
if(v1*v<0)
xu=xr;
else
xl=xr;
xo=xr;
}
getch();
}
Output Of Program:-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Finding Root By Using Bisection Method
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
---------------------------------------------------------------Enter value of xl and
xu
1
3
---------------------------------------------------------------
Enter True Value and Iterations
1.262803
10
--------------------------------------------------------------Iteration Xr Ea Et
1 2.00000 100.000000 -58.377831
2 1.50000 33.333333 -18.783373
3 1.75000 14.285714 -38.580602
4 1.62500 7.692308 -28.681988
5 1.68750 3.703704 -33.631295
6 1.71875 1.818182 -36.105948
7 1.70312 0.917431 -34.868622
8 1.71094 0.456621 -35.487285
9 1.71484 0.227790 -35.796617
10 1.71289 0.114025 -35.641951
--------------------------------------------------------------
----------");
printf("Enter coeff.in decreasing order of degree\n");
for(i=0,p=n;i<=n;i++,p--)
{
scanf("%f",&e[i][0]);
e[i][1]=p;
}
puts("------------------------------------------");
printf("Enter value of xl and xu\n");
scanf("%f %f",&xl,&xu);
puts("------------------------------------------");
puts("What would you like to Enter\nEs or Number of sig fig.or Iteration=>E/S/I");
ch=getche();
if(ch=='E'||ch=='e')
{
printf("\nEnter value of Es\n");
scanf("%f",&es);
}
else if(ch=='i'||ch=='I')
{
printf("\nEnter no. of Iteration");
scanf("%d",&it);
}
else
{
printf("\nEnter no. of sig. fig\n");
scanf("%f",&p);
es=0.5*pow(10,(2-p));
}
puts("-------------------------------------------");
i=0;
puts("Iteration\t Xr \t\t Ea");
while(1)
{
i++;
v1=value(xl);
v2=value(xu);
if(v1*v2>=0)
{
printf("Invalid Value of xl and xu");
getch();
exit(0);
}
xr=(xl+xu)/2.0;
ea=(xr-xo)*100/xr;
if(ea<0)
ea=ea*-1;
v=value(xr);
if(v==0||ea<es||i==it)
{
printf("\n%3d\t\t%6.5f\t\t%5.3f",i,xr,ea);
puts("\n---------------------------------------------");
getch();
exit(0);
}
printf("\n%3d\t\t%6.5f\t\t%5.3f",i,xr,ea);
if(v1*v<0)
xu=xr;
else
xl=xr;
xo=xr;
}
}
float value(float x)
{
int i;
float s=0;
for(i=0;i<=n;i++)
{
s=s+e[i][0]*pow(x,e[i][1]);
}
return s;
}
Output Of Program:First:Enter degree
2
-----------------------------------------Enter coeff.in decreasing order of degree
-0.4 2.2 4.7
-----------------------------------------Enter value of xl and xu
5 10
-----------------------------------------What would you like to Enter
Es or Number of sig fig.or Iteration=>E/S/I
e
Enter value of Es
1
------------------------------------------Iteration Xr Ea
1 7.50000 100.000
2 6.25000 20.000
3 6.87500 9.091
4 7.18750 4.348
5 7.03125 2.222
6 7.10938 1.099
7 7.14844 0.546
-----------------------------------------------
Second:Enter degree
5
-----------------------------------------Enter coeff.in decreasing order of degree
0.65 -9 45.5 -88 82.3 -26
-----------------------------------------Enter value of xl and xu
0.5 1
-----------------------------------------What would you like to Enter
Es or Number of sig fig.or Iteration=>E/S/I
e
Enter value of Es
10
------------------------------------------Iteration Xr Ea
1 0.75000 100.000
2 0.62500 20.000
3 0.56250 11.111
4 0.59375 5.263
----------------------------------------------