-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguass jacobi.c
147 lines (120 loc) · 2.11 KB
/
guass jacobi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int d,n;
float **p;
float acc;
int flag;
float var[100][100];
float answerarray[100][100];
float error[100][100];
int accuracy(int in)
{
int i;
for(i=0;i<d;i++)
{
if(fabs(error[i][in])<acc)
{ continue;
printf("continuing");
}
else
{
return 0;
}
}
return 1;
}
void solve()
{
int i,j;
int in=1;
do{ printf("%d",in);
for(i=0;i<d;i++)
{ var[i][in]=0;
if(i!=0)
{ for(j=0;j<i;j++)
{
var[i][in]-=p[i][j]*var[j][(in-1)];
}
}
for(j=(i+1);j<d;j++)
{
var[i][in]-=p[i][j]*var[j][(in-1)];
}
j=d;
var[i][in]-=p[i][j];
var[i][in]/=p[i][i];
printf(" %f \t",var[i][in]);
answerarray[i][in]=var[i][in];
error[i][in]=answerarray[i][in]-answerarray[i][in-1];
}
flag=accuracy(in);
in++;
printf("\n");
}while(flag==0);
}
void main()
{ int i,j;
printf("Enter the number of equations");
scanf("%d",&n);
printf("Enter the number of variables");
scanf("%d",&d);
printf("enter the degree of accuracy of the answer");
scanf("%f",&acc);
printf("\n");
p=(float**)malloc(n*sizeof(float));
for(i=0;i<n;i++)
{
*(p+i)=(float*)malloc((d+1)*sizeof(float));
}
for(i=0;i<n;i++)
{
for(j=0;j<(d+1);j++)
{
printf("Enter the coefficient at position %d %d",i,j);
scanf("%f",&p[i][j]);
}
}
printf("The matrix is \n");
for(i=0;i<n;i++)
{
for(j=0;j<d;j++)
{
printf("%f \t",p[i][j]);
}
j=d;
printf("%f \t",-p[i][j]);
printf("\n");
}
float sum;
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<d;j++)
{
if(j!=i)
sum=sum+p[i][j];
}
if(p[i][i]>sum)
continue;
else {printf("matrix is not diagonally dominant hence this method cannot be used.");
return;
printf("\n");
}
}
for(i=0;i<d;i++)
{
printf("x%d th var\t",i);
}
printf("\n");
printf("0 ");
for(i=0;i<d;i++)
{
var[i][0]=0;
answerarray[i][0]=0;
error[i][0]=0;
printf("%f\t",var[i][0]);
}
printf("\n");
solve();
}