Numerical Methods
Numerical Methods
#include<stdio.h>
double e=0.01;
double c;
c = a;
int main()
{
double a,b;
a=-10;
b=20;
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
1 #include<stdio.h>
2 #include<math.h>
3 float f(float x)
4 {
6 }
7 void regula (float *x, float x0, float x1, float fx0, float fx1, int *itr)
8 {
10 ++(*itr);
12 }
13 void main ()
14 {
16 float x0,x1,x2,x3,allerr;
17 printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n");
21 {
22 if (f(x0)*f(x2) < 0)
23 x1=x2;
24 else
25 x0=x2;
28 {
30 return 0;
31 }
32 x2=x3;
33 }
34 while (itr<maxmitr);
36 return 1;
37 }
Input/Output:
1 #include<stdio.h>
2 #include<math.h>
3 float f(float x)
4 {
6 }
7 float df (float x)
8 {
10 }
11 void main()
12 {
18 {
19 h=f(x0)/df(x0);
20 x1=x0-h;
23 {
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:
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
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);
for(i=0;i<=5;i++){
p[i]=ply[i];
}
}while(flag!=1);
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
void main(){
float x1,x2,x3,f1,f2,t;
clrscr();
scanf("%f",&x1);
scanf("%f",&x2);
printf("\n______________________________________________\n");
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;
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
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;
7 scanf("%d",&n);
10 {
14 scanf("%f",&A[i][j]);
15 }
16 }
17 for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
18 {
20 {
21 if(i>j)
22 {
23 c=A[i][j]/A[j][j];
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];
34 {
35 sum=0;
37 {
38 sum=sum+A[i][j]*x[j];
39 }
40 x[i]=(A[i][n+1]-sum)/A[i][i];
41 }
44 {
46 }
47 return(0);
48 }
Input/Output:
1 #include<stdio.h>
2 int main()
3 {
4 int i,j,k,n;
5 float A[20][20],c,x[10];
7 scanf("%d",&n);
10 {
12 {
14 scanf("%f",&A[i][j]);
15 }
16 }
19 {
21 {
22 if(i!=j)
23 {
24 c=A[i][j]/A[j][j];
26 {
27 A[i][k]=A[i][k]-c*A[j][k];
28 }
29 }
30 }
31 }
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:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float y1;
y1=(8-5*x-7*z)/6;
return y1;
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();
scanf("%d",&n);
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;
getch();
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
double x1=0,x2=0,x3=0,y1,y2,y3;
int i=0;
clrscr();
printf("\n_______________________________________\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);
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
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;
7 scanf("%d",&n);
10 {
11 scanf ("%f",&x[i]);
12 scanf("%f",&y[i]);
13 }
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);
25 {
26 s=1;
27 t=1;
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 }
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:
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]);
}
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
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:
1 #include<stdio.h>
3 {
4 float f;
5 f=x+y;
6 return f;
7 }
8 main()
9 {
10 float a,b,x,y,h,t,k;
12 scanf("%f%f%f%f",&a,&b,&h,&t);
13 x=a;
14 y=b;
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:
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;
11 scanf("%f",&x[0]);
13 scanf("%f",&h);
15 scanf("%f",&a);
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 }
37 for(j=0;j<i;j++)
38 {
39 printf(" %f\t%f",x[j],y[j]);
40 printf("\n");
41 }
42 }
44 {
45 float c;
46 c=a*a+b;
47 return(c);
48 }
Input/Output:
SOURCE CODE FOR RUNGE KUTTA METHOD IN C:
1 #include<stdio.h>
2 #include<math.h>
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 }
25 {
26 float m;
27 m=(x-y)/(x+y);
28 return m;
29 }
Input/Output: