PROGRAM FOR BISECTION METHOD IN C
#include<stdio.h>
//function used is x^3-2x^2+3
double func(double x)
{
return x*x*x - 2*x*x + 3;
}
double e=0.01;
double c;
void bisection(double a,double b)
{
if(func(a) * func(b) >= 0)
{
printf("Incorrect a and b");
return;
}
c = a;
while ((b-a) >= e)
{
c = (a+b)/2;
if (func(c) == 0.0){
printf("Root = %lf\n",c);
break;
}
else if (func(c)*func(a) < 0){
printf("Root = %lf\n",c);
b = c;
}
else{
printf("Root = %lf\n",c);
a = c;
}
}
}
int main()
{
double a,b;
a=-10;
b=20;
printf("The function used is x^3-2x^2+3\n");
printf("a = %lf\n",a);
printf("b = %lf\n",b);
bisection(a,b);
printf("\n");
printf("Accurate Root calculated is = %lf\n",c);
return 0;
}
Output
a = -10.000000
b = 20.000000
Root = 5.000000
Root = -2.500000
Root = 1.250000
Root = -0.625000
Root = -1.562500
Root = -1.093750
Root = -0.859375
Root = -0.976563
Root = -1.035156
Root = -1.005859
Root = -0.991211
Root = -0.998535
Accurate Root calculated is = -0.998535
SOURCE CODE FOR REGULA FALSI METHOD IN C:
C Program for Regula Falsi Method Source Code
1 #include<stdio.h>
2 #include<math.h>
3 float f(float x)
4 {
5 return cos(x) - x*exp(x);
6 }
7 void regula (float *x, float x0, float x1, float fx0, float fx1, int *itr)
8 {
9 *x = x0 - ((x1 - x0) / (fx1 - fx0))*fx0;
10 ++(*itr);
11 printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
12 }
13 void main ()
14 {
15 int itr = 0, maxmitr;
16 float x0,x1,x2,x3,allerr;
17 printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n");
18 scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr);
19 regula (&x2, x0, x1, f(x0), f(x1), &itr);
20 do
21 {
22 if (f(x0)*f(x2) < 0)
23 x1=x2;
24 else
25 x0=x2;
26 regula (&x3, x0, x1, f(x0), f(x1), &itr);
27 if (fabs(x3-x2) < allerr)
28 {
29 printf("After %d iterations, root = %6.4f\n", itr, x3);
30 return 0;
31 }
32 x2=x3;
33 }
34 while (itr<maxmitr);
35 printf("Solution does not converge or iterations not sufficient:\n");
36 return 1;
37 }
Input/Output:
SOURCE CODE FOR NEWTON RAPHSON METHOD IN C:
C Program for Newton-Raphson Method Source Code
1 #include<stdio.h>
2 #include<math.h>
3 float f(float x)
4 {
5 return x*log10(x) - 1.2;
6 }
7 float df (float x)
8 {
9 return log10(x) + 0.43429;
10 }
11 void main()
12 {
13 int itr, maxmitr;
14 float h, x0, x1, allerr;
15 printf("\nEnter x0, allowed error and maximum iterations\n");
16 scanf("%f %f %d", &x0, &allerr, &maxmitr);
17 for (itr=1; itr<=maxmitr; itr++)
18 {
19 h=f(x0)/df(x0);
20 x1=x0-h;
21 printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
22 if (fabs(h) < allerr)
23 {
24 printf("After %3d iterations, root = %8.6f\n", itr, x1);
25 return 0;
26 }
27 x0=x1;
28 }
29 printf(" The required solution does not converge or iterations are insufficient\n");
30 return 1;
31 }
Input/Output:
C PROGRAM FOR BIRGE-VIETA METHOD
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float p[6], ply[6],q[6];
float synth(int m, float r){
int i;
q[0] = p[0];
for(i=1;i<=m;i++){
q[i] = (q[i-1]*r)+p[i];
}
printf("\n");
for(i=0;i<m;i++){
printf("\t%f",q[i]);
}
printf("\t%f",q[m]);
return(q[m]);
}
void main(){
clrscr();
int m,i,flag=0;
float r, x,x1, fx, fdx;
printf("\t\tBIRGE-VIETA METHOD");
printf("\n Enter the highest degree of the equation (max 5): ");
scanf("%d",&m);
for(i=0;i<=m;i++){
printf("\n Coefficient x[%d] = ",m-i);
scanf("%f",&p[i]);
ply[i] = p[i];
}
printf("\n Enter the initial value x0 : ");
scanf("%f",&r);
x = r;
do{
printf("\n%f\n",x);
fx = synth(m,x);
for(i=0;i<=m;i++){
p[i]=q[i];
}
fdx = synth(m-1,x);
x1 = x - (fx/fdx);
if(fabs(x1-x) <= 0.0009){
flag = 1;
}
x = x1;
for(i=0;i<=5;i++){
p[i]=ply[i];
}
}while(flag!=1);
printf("\nApproximate root = %f", x1);
getch();
Sample Output:
BIRGE-VIETA METHOD
Enter the highest degree of the equation (max 5): 3
Coefficient x[3] = 1
Coefficient x[2] = 1
Coefficient x[1] = -3
Coefficient x[0] = -3
Enter the initial value x0 : 2
2.000000
1.000000 3.000000 3.000000 3.000000
1.000000 5.000000 13.000000
1.769231
1.000000 2.769231 1.899408 0.360492
1.000000 4.538462 9.928994
1.732924
1.000000 2.732924 1.735948 0.008266
1.000000 4.465847 9.474921
Approximate root = 1.732051
C PROGRAM FOR SECANT METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define E 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 > E);
printf("\n______________________________________________\n");
printf("\n\nApp.root = %f",x3);
getch();
OUTPUT
Enter the value of x1: 4
Enter the value of x2: 2
-----------------------------------------------
x0 x1 x2 f(x0) f(x1)
-----------------------------------------------
4.000 2.000 9.0000 -10.000 -14.000
2.000 9.000 4.0000 -14.000 35.000
9.000 4.000 5.1111 35.000 -10.000
4.000 5.111 5.9565 -10.000 -4.321
5.111 5.957 5.7225 -4.321 1.654
5.957 5.722 5.7411 1.654 -0.143
5.722 5.741 5.7417 -0.143 -0.004
5.741 5.742 5.7417 -0.004 0.000
SOURCE CODE FOR GAUSS ELIMINATION METHOD IN C:
C Program for Gauss Elimination Method
1 #include<stdio.h>
2 int main()
3 {
4 int i,j,k,n;
5 float A[20][20],c,x[10],sum=0.0;
6 printf("\nEnter the order of matrix: ");
7 scanf("%d",&n);
8 printf("\nEnter the elements of augmented matrix row-wise:\n\n");
9 for(i=1; i<=n; i++)
10 {
11 for(j=1; j<=(n+1); j++)
12 {
13 printf("A[%d][%d] : ", i,j);
14 scanf("%f",&A[i][j]);
15 }
16 }
17 for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
18 {
19 for(i=1; i<=n; i++)
20 {
21 if(i>j)
22 {
23 c=A[i][j]/A[j][j];
24 for(k=1; k<=n+1; k++)
25 {
26 A[i][k]=A[i][k]-c*A[j][k];
27 }
28 }
29 }
30 }
31 x[n]=A[n][n+1]/A[n][n];
32 /* this loop is for backward substitution*/
33 for(i=n-1; i>=1; i--)
34 {
35 sum=0;
36 for(j=i+1; j<=n; j++)
37 {
38 sum=sum+A[i][j]*x[j];
39 }
40 x[i]=(A[i][n+1]-sum)/A[i][i];
41 }
42 printf("\nThe solution is: \n");
43 for(i=1; i<=n; i++)
44 {
45 printf("\nx%d=%f\t",i,x[i]); /* x1, x2, x3 are the required solutions*/
46 }
47 return(0);
48 }
Input/Output:
SOURCE CODE FOR GAUSS-JORDAN METHOD IN C:
C Program for Gauss Jordan Method
1 #include<stdio.h>
2 int main()
3 {
4 int i,j,k,n;
5 float A[20][20],c,x[10];
6 printf("\nEnter the size of matrix: ");
7 scanf("%d",&n);
8 printf("\nEnter the elements of augmented matrix row-wise:\n");
9 for(i=1; i<=n; i++)
10 {
11 for(j=1; j<=(n+1); j++)
12 {
13 printf(" A[%d][%d]:", i,j);
14 scanf("%f",&A[i][j]);
15 }
16 }
17 /* Now finding the elements of diagonal matrix */
18 for(j=1; j<=n; j++)
19 {
20 for(i=1; i<=n; i++)
21 {
22 if(i!=j)
23 {
24 c=A[i][j]/A[j][j];
25 for(k=1; k<=n+1; k++)
26 {
27 A[i][k]=A[i][k]-c*A[j][k];
28 }
29 }
30 }
31 }
32 printf("\nThe solution is:\n");
33 for(i=1; i<=n; i++)
34 {
35 x[i]=A[i][n+1]/A[i][i];
36 printf("\n x%d=%f\n",i,x[i]);
37 }
38 return(0);
39 }
Input/Output:
PROGRAM OF GAUSS JACOBI IN C
#include<stdio.h>
#include<conio.h>
#include<math.h>
float fx(float y,float z)
float x1; x1=4-2*y-3*z;
return x1;
float fy(float x,float z)
float y1;
y1=(8-5*x-7*z)/6;
return y1;
float fz(float x,float y)
float z1;
z1=(3-9*x-y)/2;
return z1;
void main()
int i,j,n;
float a1,b1,c1;
float a,b,c;
float ar[3][4],x[3];
clrscr();
printf("Enter the no. of Iteration : ");
scanf("%d",&n);
printf("Enter The initial value : ");
scanf("%f %f %f",&a,&b,&c);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
a1=fx(b,c);
b1=fy(a,c);
c1=fz(a,b);
a=a1;
b=b1;
c=c1;
printf("a1 = %f\n a2 = %f\n a3 = %f",a1,b1,c1);
getch();
PROGRAM OF GAUSS SEIDEL IN C
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define X1(x2,x3) ((17 - 20*(x2) + 2*(x3))/20)
#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)
#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)
void main()
double x1=0,x2=0,x3=0,y1,y2,y3;
int i=0;
clrscr();
printf("\n_______________________________________\n");
printf("\n x1\t\t x2\t\t x3\n");
printf("\n_______________________________________\n");
printf("\n%f\t%f\t%f",x1,x2,x3);
do
y1=X1(x2,x3);
y2=X2(y1,x3);
y3=X3(y1,y2);
if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP )
printf("\n_______________________________________\n");
printf("\n\nx1 = %.3lf",y1);
printf("\n\nx2 = %.3lf",y2);
printf("\n\nx3 = %.3lf",y3);
i = 1;
else
x1 = y1;
x2 = y2;
x3 = y3;
printf("\n%f\t%f\t%f",x1,x2,x3);
}
}
while(i != 1);
getch();
Output
SOURCE CODE FOR LAGRANGE INTERPOLATION IN C:
C Program for Lagrange Interpolation
1 #include<stdio.h>
2 main()
3 {
4 float x[100],y[100],a,s=1,t=1,k=0;
5 int n,i,j,d=1;
6 printf("\n\n Enter the number of the terms of the table: ");
7 scanf("%d",&n);
8 printf("\n\n Enter the respective values of the variables x and y: \n");
9 for(i=0; i<n; i++)
10 {
11 scanf ("%f",&x[i]);
12 scanf("%f",&y[i]);
13 }
14 printf("\n\n The table you entered is as follows :\n\n");
15 for(i=0; i<n; i++)
16 {
17 printf("%0.3f\t%0.3f",x[i],y[i]);
18 printf("\n");
19 }
20 while(d==1)
21 {
22 printf(" \n\n\n Enter the value of the x to find the respective value of y\n\n\n");
23 scanf("%f",&a);
24 for(i=0; i<n; i++)
25 {
26 s=1;
27 t=1;
28 for(j=0; j<n; j++)
29 {
30 if(j!=i)
31 {
32 s=s*(a-x[j]);
33 t=t*(x[i]-x[j]);
34 }
35 }
36 k=k+((s/t)*y[i]);
37 }
38 printf("\n\n The respective value of the variable y is: %f",k);
39 printf("\n\n Do you want to continue?\n\n Press 1 to continue and any other key to exit");
40 scanf("%d",&d);
41 }
42 }
Input/Output:
NEWTON FORWARD INTERPOLATION IN C:
C Program for Newton Forward Interpolation Source Code
1 #include<stdio.h>
2 #define MAXN 100
3 #define ORDER 4
4
5 main()
6 {
7 float ax[MAXN+1], ay [MAXN+1], diff[MAXN+1][ORDER+1], nr=1.0, dr=1.0,x,p,h,yp;
8 int n,i,j,k;
9 printf("\nEnter the value of n:\n");
10 scanf("%d",&n);
11
12 printf("\nEnter the values in form x,y:\n");
13 for (i=0;i<=n;i++)
14 scanf("%f %f",&ax[i],&ay[i]);
15 printf("\nEnter the value of x for which the value of y is wanted: \n");
16 scanf("%f",&x);
17 h=ax[1]-ax[0];
18
19 //now making the difference table
20 //calculating the 1st order of differences
21 for (i=0;i<=n-1;i++)
22 diff[i][1] = ay[i+1]-ay[i];
23
24 //now calculating the second and higher order differences
25 for (j=2;j<=ORDER;j++)
26 for(i=0;i<=n-j;i++)
27 diff[i][j] = diff[i+1][j-1] - diff[i][j-1];
28
29 //now finding x0
30 i=0;
31 while (!(ax[i]>x))
32 i++;
33
34 //now ax[i] is x0 and ay[i] is y0
35 i--;
36 p = (x-ax[i])/h;
37 yp = ay[i];
38
39 //now carrying out interpolation
40 for (k=1;k<=ORDER;k++)
41 {
42 nr *=p-k+1;
43 dr *=k;
44 yp +=(nr/dr)*diff[i][k];
45 }
46 printf("\nWhen x = %6.1f, corresponding y = %6.2f\n",x,yp);
47 }
Input/Output:
C SOURCE CODE: BACKWARD DIFFERENCE TABLE
#include<stdio.h>
#include<conio.h>
int main()
{
float x[20], y[20][20];
int i,j, n;
clrscr();
/* Input Section */
printf("Enter number of data?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i = 0; i < n ; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);
printf("y[%d]=", i);
scanf("%f", &y[i][0]);
}
/* Generating Backward Difference Table */
for(i = 1; i < n; i++)
{
for(j = n-1; j > i-1; j--)
{
y[j][i] = y[j][i-1] - y[j-1][i-1];
}
}
/* Displaying Backward Difference Table */
printf("\nBACKWARD DIFFERENCE TABLE\n\n");
for(i = 0; i < n; i++)
{
printf("%0.2f", x[i]);
for(j = 0; j <= i ; j++)
{
printf("\t%0.2f", y[i][j]);
}
printf("\n");
}
getch(); /* Holding Screen */
return 0;
C Output: Backward Difference Table
Enter number of data?
4
Enter data:
x[0]=0
y[0]=1
x[1]=1
y[1]=2
x[2]=2
y[2]=1
x[3]=3
y[3]=10
BACKWARD DIFFERENCE TABLE
0.00 1.00
1.00 2.00 1.00
2.00 1.00 -1.00 -2.00
3.00 10.00 9.00 10.00 12.00
NEWTON DIVIDED DIFFERENCE IN C:
C Program Source Code for Newton Divided Difference
1 #include<stdio.h>
2 #include<conio.h>
3
4 void main()
5 {
6 int x[10], y[10], p[10];
7 int k,f,n,i,j=1,f1=1,f2=0;
8 printf("\nEnter the number of observations:\n");
9 scanf("%d", &n);
10
11 printf("\nEnter the different values of x:\n");
12 for (i=1;i<=n;i++)
13 scanf("%d", &x[i]);
14
15 printf("\nThe corresponding values of y are:\n");
16 for (i=1;i<=n;i++)
17 scanf("%d", &y[i]);
18
19 f=y[1];
20 printf("\nEnter the value of 'k' in f(k) you want to evaluate:\n");
21 scanf("%d", &k);
22
23 do
24 {
25 for (i=1;i<=n-1;i++)
26 {
27 p[i] = ((y[i+1]-y[i])/(x[i+j]-x[i]));
28 y[i]=p[i];
29 }
30 f1=1;
31 for(i=1;i<=j;i++)
32 {
33 f1*=(k-x[i]);
34 }
35 f2+=(y[1]*f1);
36 n--;
37 j++;
38 }
39
40 while(n!=1);
41 f+=f2;
42 printf("\nf(%d) = %d", k , f);
43 getch();
44 }
Input/Output:
SOURCE CODE FOR EULER’S METHOD IN C:
C Program for Euler's Method
1 #include<stdio.h>
2 float fun(float x,float y)
3 {
4 float f;
5 f=x+y;
6 return f;
7 }
8 main()
9 {
10 float a,b,x,y,h,t,k;
11 printf("\nEnter x0,y0,h,xn: ");
12 scanf("%f%f%f%f",&a,&b,&h,&t);
13 x=a;
14 y=b;
15 printf("\n x\t y\n");
16 while(x<=t)
17 {
18 k=h*fun(x,y);
19 y=y+k;
20 x=x+h;
21 printf("%0.3f\t%0.3f\n",x,y);
22 }
23 }
Input/Output:
SOURCE CODE FOR MODIFIED EULER’S METHOD IN C:
C Program for Modified Euler's Method
1 #include<stdio.h>
2 #include<math.h>
3 #include<string.h>
4 float fun(float,float);
5 main()
6 {
7 int i,j,c;
8 float x[100],y[100],h,m[100],m1,m2,a,s[100],w;
9 printf("\n C program for Modified Euler Method \n\n");
10 printf(" Enter the initial value of x:");
11 scanf("%f",&x[0]);
12 printf("\n Enter the value of increment h:");
13 scanf("%f",&h);
14 printf("\n Enter the final value of x:");
15 scanf("%f",&a);
16 printf("\n Enter the initial value of the variable y :");
17 scanf("%f",&y[0]);
18 s[0]=y[0];
19 for(i=1;x[i-1]<a;i++)
20 {
21 w=100.0;
22 x[i]= x[i-1]+h;
23 m[i]=fun(x[i-1],y[i-1]);
24 c=0;
25 while(w>0.0001)
26 {
27 m1=fun(x[i],s[c]);
28 m2=(m[i]+m1)/2;
29 s[c+1]=y[i-1]+m2*h;
30 w=s[c]-s[c+1];
31 w=fabs(w);
32 c=c+1;
33 }
34 y[i]=s[c];
35 }
36 printf("\n\n The respective values of x and y are\n x \t y\n\n");
37 for(j=0;j<i;j++)
38 {
39 printf(" %f\t%f",x[j],y[j]);
40 printf("\n");
41 }
42 }
43 float fun(float a,float b)
44 {
45 float c;
46 c=a*a+b;
47 return(c);
48 }
Input/Output:
SOURCE CODE FOR RUNGE KUTTA METHOD IN C:
C Program for Runge Kutta 4 Method (1st Order)
1 #include<stdio.h>
2 #include<math.h>
3 float f(float x,float y);
4 int main()
5 {
6 float x0,y0,m1,m2,m3,m4,m,y,x,h,xn;
7 printf("Enter x0,y0,xn,h:");
8 scanf("%f %f %f %f",&x0,&y0,&xn,&h);
9 x=x0;
10 y=y0;
11 printf("\n\nX\t\tY\n");
12 while(x<xn)
13 {
14 m1=f(x0,y0);
15 m2=f((x0+h/2.0),(y0+m1*h/2.0));
16 m3=f((x0+h/2.0),(y0+m2*h/2.0));
17 m4=f((x0+h),(y0+m3*h));
18 m=((m1+2*m2+2*m3+m4)/6);
19 y=y+m*h;
20 x=x+h;
21 printf("%f\t%f\n",x,y);
22 }
23 }
24 float f(float x,float y)
25 {
26 float m;
27 m=(x-y)/(x+y);
28 return m;
29 }
Input/Output: