Skip to content

Commit e35ba2a

Browse files
Add files via upload
1 parent 54f7c1f commit e35ba2a

16 files changed

+626
-0
lines changed

Bisection_method.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include<stdio.h>
2+
#define f(x) (x*x*x-2*x-5)
3+
4+
int main()
5+
{
6+
float x1,x2,x0;
7+
printf("Enter the value of x1:");
8+
scanf("%f",&x1);
9+
printf("f(x1)=%f\n",f(x1));
10+
printf("Enter the value of x2:");
11+
scanf("%f",&x2);
12+
printf("f(x2)=%f\n",f(x2));
13+
if((f(x1)*f(x2))<0)
14+
{
15+
for(int i=1;i<=4;i++)
16+
{
17+
printf("For %d approximation of root\n ",i);
18+
x0=(x1+x2)/2;
19+
printf("x0=%f\n",x0);
20+
printf("f(x0)=%f\n",f(x0));
21+
if(f(x0)>0)
22+
{
23+
if(f(x1)>0)
24+
{
25+
x1=x0;
26+
}
27+
if(f(x2)>0)
28+
{
29+
x2=x0;
30+
}
31+
}
32+
else if(f(x0)<0)
33+
{
34+
if(f(x1)<0)
35+
{
36+
x1=x0;
37+
}
38+
if(f(x2)<0)
39+
{
40+
x2=x0;
41+
}
42+
}
43+
else
44+
{
45+
printf("Invalid...");
46+
}
47+
printf("x1=%f\nx2=%f",x1,x2);
48+
}
49+
}
50+
else
51+
{
52+
printf("\nInvalid input.");
53+
}
54+
return 0;
55+
}

Euler's_method.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<stdio.h>
2+
#define f(x,y) (x+y) //((y-x)/(y+x))
3+
int main()
4+
{
5+
float x0,y0,y,xrq,h,n;
6+
7+
printf("Enter the initial value of x:");
8+
scanf("%f",&x0);
9+
printf("Enter the initial value of y:");
10+
scanf("%f",&y0);
11+
printf("Enter the value of x for which y required:");
12+
scanf("%f",&xrq);
13+
printf("Enter the number of steps:");
14+
scanf("%f",&n);
15+
h=(xrq-x0)/n; //h=interval width(size of each step)
16+
printf("Step length=%f",h);
17+
for(int i=0;i<n;i++)
18+
{
19+
if(x0<=xrq)
20+
{
21+
y=y0+(h*f(x0,y0));
22+
y0=y;
23+
x0=x0+h;
24+
printf("\nx%d=%f\n",(i+1),x0);
25+
printf("y%d=%f\n",(i+1),y0);
26+
}
27+
else
28+
{
29+
break;
30+
}
31+
32+
}
33+
printf("\nThe value of y is %f at x=%f",y,x0);
34+
35+
return 0;
36+
}

Gauss_Elimination_method.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<stdio.h>
2+
int main()
3+
{
4+
int i,j,k,n;
5+
float A[20][20],ratio,x[10],sum=0;
6+
printf("Enter the order of the matrix:");
7+
scanf("%d",&n);
8+
printf("\nEnter the elements of the augmented matrix:");
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++)
18+
{
19+
for(i=1;i<=n;i++)
20+
{
21+
if(i>j)
22+
{
23+
ratio=A[i][j]/A[j][j];
24+
for(k=1;k<=n+1;k++)
25+
{
26+
A[i][k]=A[i][k]-ratio*A[j][k];
27+
}
28+
}
29+
}
30+
}
31+
x[n]=A[n][n+1]/A[n][n];
32+
for(i=n-1;i>=1;i--)
33+
{
34+
sum=0;
35+
for(j=i+1;j<=n;j++)
36+
{
37+
sum=sum+A[i][j]*x[j];
38+
}
39+
x[i]=(A[i][n+1]-sum)/A[i][i];
40+
}
41+
printf("\nSolution:");
42+
for(i=1;i<=n;i++)
43+
{
44+
printf("\nx%d=%f\t",i,x[i]);
45+
}
46+
return(0);
47+
}

Gauss_Jordan_method.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<stdio.h>
2+
#include<math.h>
3+
#define SIZE 10
4+
int main()
5+
{
6+
float a[SIZE][SIZE], x[SIZE], ratio;
7+
int i,j,k,n;
8+
printf("Enter number of unknowns: ");
9+
scanf("%d", &n);
10+
printf("Enter coefficients of Augmented Matrix:\n");
11+
for(i=1;i<=n;i++)
12+
{
13+
for(j=1;j<=n+1;j++)
14+
{
15+
printf("a[%d][%d] = ",i,j);
16+
scanf("%f", &a[i][j]);
17+
}
18+
}
19+
for(i=1;i<=n;i++)
20+
{
21+
if(a[i][i] == 0.0)
22+
{
23+
printf("Mathematical Error!");
24+
}
25+
for(j=1;j<=n;j++)
26+
{
27+
if(i!=j)
28+
{
29+
ratio = a[j][i]/a[i][i];
30+
for(k=1;k<=n+1;k++)
31+
{
32+
a[j][k] = a[j][k] - ratio*a[i][k];
33+
}
34+
}
35+
}
36+
}
37+
for(i=1;i<=n;i++)
38+
{
39+
x[i] = a[i][n+1]/a[i][i];
40+
}
41+
printf("\nSolution:\n");
42+
for(i=1;i<=n;i++)
43+
{
44+
printf("x[%d] = %0.3f\n",i, x[i]);
45+
}
46+
return(0);
47+
}

LU_factorization.c

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include<stdio.h>
2+
void main()
3+
{
4+
float a[10][10],l[10][10],u[10][10],z[10],x[10],b[10];
5+
int i,j,k,n;
6+
7+
printf("\nEnter the size of the coefficient matrix: ");
8+
scanf("%d",&n);
9+
printf("Enter the elements row-wise: ");
10+
for(i=0;i<n;i++)
11+
for(j=0;j<n;j++)
12+
scanf("%f",&a[i][j]);
13+
printf("Enter the right hand vector: ");
14+
for(i=0;i<n;i++)
15+
scanf("%f",&b[i]);
16+
for(i=0;i<n;i++)
17+
l[i][1]=a[i][1];
18+
for(j=2;j<=n;j++)
19+
u[1][j]=a[1][j]/l[1][1];
20+
for(i=0;i<n;i++)
21+
u[i][i]=1;
22+
for(i=2;i<=n;i++)
23+
for(j=2;j<=n;j++)
24+
if(i>=j)
25+
{
26+
l[i][j]=a[i][j];
27+
for(k=1;k<=j-1;k++)
28+
l[i][j]-=l[i][k]*u[k][j];
29+
}
30+
else
31+
{
32+
u[i][j]=a[i][j];
33+
for(k=1;k<=j-1;k++)
34+
u[i][j]=-l[i][k]*u[k][j];
35+
u[i][j]/=l[i][i];
36+
}
37+
printf("\nThe lower triangular matrix L: \n");
38+
for(i=0;i<=n;i++)
39+
{
40+
for(j=0;j<n;j++)
41+
printf("%f ",l[i][j]);
42+
printf("\n");
43+
}
44+
printf("\nThe upper triangular matrix U: \n");
45+
for(i=0;i<=n;i++)
46+
{
47+
for(j=0;j<i;j++)
48+
printf(" ");
49+
for(j=i;j<n;j++)
50+
printf("%f ",l[i][j]);
51+
printf("\n");
52+
}
53+
z[1]=b[1]/l[1][1];
54+
for(j=1;i<n;i++)
55+
{
56+
z[i]=b[i];
57+
for(j=0;j<i-1;j++)
58+
z[i]-=l[i][j]*z[j];
59+
z[i]/=l[i][i];
60+
}
61+
x[n]=z[n];
62+
for(i=n-1;i>=0;i--)
63+
{
64+
x[i]=z[i];
65+
for(j=i+1;j<n;j++)
66+
x[i]-=u[i][j]*x[j];
67+
}
68+
printf("The solution is: ");
69+
for(i=0;i<n;i++)
70+
printf("%f ",x[i]);
71+
}

Langrange_Interpolation_method.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<stdio.h>
2+
int main(){
3+
int i,j,n,xr,x[100],y[100];
4+
float p,yr=0;
5+
printf("Enter the num of entries:");
6+
scanf("%d",&n);
7+
printf("Enter the values of x:\n");
8+
for(i=0;i<n;i++)
9+
{
10+
printf("\nx[%d]=",i);
11+
scanf("%d",&x[i]);
12+
}
13+
printf("Enter the values of y:\n");
14+
for(i=0;i<n;i++)
15+
{
16+
printf("\ny[%d]=",i);
17+
scanf("%d",&y[i]);
18+
}
19+
printf("Enter the values x for which you want to find y:");
20+
scanf("%d",&xr);
21+
for(i=0;i<n;i++)
22+
{
23+
p=1;
24+
for(j=0;j<n;j++)
25+
{
26+
if(i!=j)
27+
{
28+
p=p*(xr-x[j])/(x[i]-x[j]);
29+
}
30+
}
31+
yr=yr+p*y[i];
32+
}
33+
printf("for x=%d then y=%f",xr,yr);
34+
return 0;
35+
}

Newton's_Backward_interpolation.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include<stdio.h>
2+
int fact(int);
3+
void main()
4+
{
5+
int n,i,j,ch=30;
6+
float arr[10][11],px=1,x,y,p,h;
7+
printf("\nEnter the no of data:");
8+
scanf("%d",&n);
9+
printf("\nEnter the data:");
10+
for(i=0;i<n;i++)
11+
{
12+
printf("\nX%d=",i+1);
13+
scanf("%f",&arr[i][0]);
14+
printf("\nY%d=",i+1);
15+
scanf("%f",&arr[i][1]);
16+
}
17+
for(j=2;j<=n;j++)
18+
{
19+
for(i=0;i<n-1;i++)
20+
arr[i][j]=arr[i+1][j-1]-arr[i][j-1];
21+
}
22+
printf("\nThe difference table");
23+
printf("\n\tX \tY");
24+
for(i=0;i<n-1;i++)
25+
printf("\t%c^%d",ch,i+1);
26+
for(i=0;i<n;i++)
27+
{
28+
printf("\n");
29+
for(j=0;j<n+1-i;j++)
30+
printf("\t%.4f",arr[i][j]);
31+
}
32+
printf("\nEnter the value of x for f(x)");
33+
scanf("%f",&x);
34+
h=arr[n-1][0]-arr[n-2][0];
35+
p=(x-arr[n-1][0])/h;
36+
y=arr[n-1][1];
37+
for(i=1;i<n;i++)
38+
{
39+
px=px*(p+(i-1));
40+
y=y+(arr[n-1-i][i+1]*px)/fact(i);
41+
}
42+
printf("\nthe value of f(x) at x=%f is %f",x,y);
43+
}
44+
int fact(int n)
45+
{
46+
int f=1,i;
47+
for(i=1;i<=n;i++)
48+
f=f*i;
49+
return f;
50+
}

0 commit comments

Comments
 (0)