My Problem In Jacobi Iterative Method

Hi All.I'm Very Very Glad To Join This Lovely Site & Speaking With You.
In This Term At My University I Have Numerical Methods & My Teacher Asks Me To Write 1 Program About Jacobi Iteration Method.I Have Write It But In Iteration Step I Have Problem & So I Want You Help Me To Improve This Or Write Another Iteration.Tnx All...
Here Its My Program :


#include<math.h>
#include<stdio.h>
#include<iostream>
using namespace std;


int sum(int n , int * pn)
{
int sum = 0;

for(int i = 0 ; i<n ; i++)
{sum = sum + pn[i];}

return sum;
}



double Power(double x , int n)
{
int i;
double pow = 1;
for(i = 0 ; i<n ; i++)
{
pow = pow * x ;
}
return pow ;
}


int main()
{
cout<<"Welcome To Robertoplusplus Jacobi Iterative Method Solver\n\n";

int n;
cout<<"Please Enter n:\n";
cin>>n;

cout<<"\n\n";



double ** A;
A = new double * [n];
A[0] = new double [n*n];


for(int i = 0 ; i<n ; i++)
{A[i] = &A[0][n*i];}



for(int i = 0 ; i<n ; i++)
{

for(int j = 0 ; j<n ; j++)
{A[i][j] = Power(((i+1+j+1.)/((2*(i+1)))),j+1);
printf("%lf ",A[i][j]);
}

cout<<endl;

}




double * b;
b = new double [n];


for(int i = 0 ; i<n ; i++)
{

b[i] = Power(i+1,2);
cout<<b[i]<<" ";


cout<<endl;

}

double * x;
x = new double[n];
double * k;
k = new double[n];






for(int j = 0 ; j < n ; j++)
{

cout<<"Please EnTer x["<<j<<"]:\n\n";
cin>>x[j];
}



double * seri;
seri = new double [n];

int * shomareshgar = new int[n];
int y = -2;


while(!(y==n))
{


for(int i = 0 ; i < n ; i++)
{k[i] = x[i];}


for(int i = 0 ; i < n ; i++)
{


for(int j = 0 ; j < n ; j++)
{
seri[i] = 0;


if(!(j == i))
{
seri[i] = seri[i] + A[i][j]*x[i];

}
}

x[i] = (1./A[i][i])*(b[i]-seri[i]);

if(fabs(x[i] - k[i])<1e-10)
{shomareshgar[i] = 1;}
else
{shomareshgar[i] = 0;}

}



for(int i = 0 ; i<n ; i++)
{y = y + shomareshgar[i];}


cout<<"y = "<<y<<endl;
system("pause");



}




cout<<x[0]<<endl;

system("pause");
return 0;
}
1 - write the code between the [code] tags because I don't understand anything
2 - don't use system("pause") - it will destroy your CPU
3 - what is the problem with the code? what doesn't work?
Tnq U For Notice This.I Retype My Program..My Problem Is In Iteration Step.I Have A Suitable Condition For While Case.
My While In Program Dosent Work Properly.


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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

include<math.h>
#include<stdio.h>
#include<iostream>
using namespace std;


int sum(int n , int * pn)
{
int sum = 0;

for(int i = 0 ; i<n ; i++)
{sum = sum + pn[i];}

return sum;
}



double Power(double x , int n)
{
int i;
double pow = 1;
for(i = 0 ; i<n ; i++)
{
pow = pow * x ;
}
return pow ;
}


int main()
{
cout<<"Welcome To Robertoplusplus Jacobi Iterative Method Solver\n\n";

int n;
cout<<"Please Enter n:\n";
cin>>n;

cout<<"\n\n";



double ** A;
A = new double * [n];
A[0] = new double [n*n];


for(int i = 0 ; i<n ; i++)
{A[i] = &A[0][n*i];}



for(int i = 0 ; i<n ; i++)
{

for(int j = 0 ; j<n ; j++)
{A[i][j] = Power(((i+1+j+1.)/((2*(i+1)))),j+1);
printf("%lf ",A[i][j]);
}

cout<<endl;

}




double * b;
b = new double [n];


for(int i = 0 ; i<n ; i++)
{

b[i] = Power(i+1,2);
cout<<b[i]<<" ";


cout<<endl;

}

double * x;
x = new double[n];
double * k;
k = new double[n];






for(int j = 0 ; j < n ; j++)
{

cout<<"Please EnTer x["<<j<<"]:\n\n";
cin>>x[j];
}



double * seri;
seri = new double [n];

int * shomareshgar = new int[n];
int y = -2;


while(!(y==n))
{


for(int i = 0 ; i < n ; i++)
{k[i] = x[i];}


for(int i = 0 ; i < n ; i++)
{


for(int j = 0 ; j < n ; j++)
{
seri[i] = 0;


if(!(j == i))
{
seri[i] = seri[i] + A[i][j]*x[i];

}
}

x[i] = (1./A[i][i])*(b[i]-seri[i]);

if(fabs(x[i] - k[i])<1e-10)
{shomareshgar[i] = 1;}
else
{shomareshgar[i] = 0;}

}



for(int i = 0 ; i<n ; i++)
{y = y + shomareshgar[i];}


cout<<"y = "<<y<<endl;
system("pause");



}




cout<<x[0]<<endl;

system("pause);
return 0;
} 
Last edited on
I Change My Program But It Doesent Work Yet!Plz Help Me!

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
148
149
150
151
152
153
154
155
156
157
158
159




#include<math.h>
#include<stdio.h>
#include<iostream>
using namespace std;


int sum(int n , int * pn)
{
	int sum = 0;

	for(int i = 0 ; i<n ; i++)
		{sum = sum + pn[i];}

	return sum;
	}



double Power(double x , int n)
{
	int i;
	double pow = 1;
	for(i = 0 ; i<n ; i++)
	{
		pow = pow * x ;
	}
	return pow ;
}


int main()
{
	cout<<"Welcome To MY Jacobi Iterative Method Solver\n\n";

	int n;
	cout<<"Please Enter n:\n";
	cin>>n;

	cout<<"\n\n";



	double ** A;
	A = new double * [n];
	A[0] = new double [n*n];


	for(int i = 0 ; i<n ; i++)
	{A[i] = &A[0][n*i];}



	for(int i = 0 ; i<n ; i++)
	{

		for(int j = 0 ; j<n ; j++)
		{A[i][j] = Power(((i+1+j+1.)/((2*(i+1)))),j+1);
		printf("%lf	",A[i][j]);
		}

		cout<<endl;

	}




	double * b;
	b = new double  [n];
	

	for(int i = 0 ; i<n ; i++)
	{

		b[i] = Power(i+1,2);
		cout<<b[i]<<"		";
		

		cout<<endl;

		}

	double * x;
	x = new double[n];
	double * k;
	k = new double[n];

	

	
	

	for(int j = 0 ; j < n ; j++)
	{
		
		cout<<"Please EnTer x["<<j<<"]:\n\n";
		cin>>x[j];
	}

	

		double * seri;
		seri = new double [n];

		int * shomareshgar = new int[n];
		int y = 0;

		
		while(y==0)
		{
			


			for(int i = 0 ; i < n ; i++)
	{
		

		for(int j = 0 ; j < n ; j++)
		{
			seri[i] = 0;
			

			if(!(j == i))
		{
			seri[i] = seri[i] + A[i][j]*x[j];

		}
		}

		x[i] = (1./A[i][i])*(b[i]-seri[i]);;

		
		
			}

			
		for(int i = 0 ; i < n ; i++)
		{cout<<"x["<<i<<"] = "<<x[i]<<endl<<endl;}

		cout<<"For Continue Press 0 ; For Exit Press Any Number :\n\n";
		cin>>y;
			


		
		}
	


		

	system("pause");
	return 0;
}
Last edited on
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
while(y==0)
{
			
	for(int i = 0 ; i < n ; i++)
	{
		
  
		for(int j = 0 ; j < n ; j++)
		{
			seri[i] = 0;
			

			if(!(j == i))
		           seri[i] = seri[i] + A[i][j]*x[j];

		        
		}

		x[i] = (1./A[i][i])*(b[i]-seri[i]);;

		
		
	}

			
	for(int i = 0 ; i < n ; i++)
	{cout<<"x["<<i<<"] = "<<x[i]<<endl<<endl;}

	cout<<"For Continue Press 0 ; For Exit Press Any Number :\n\n";
	cin>>y;
			
}

This is your loop. Try to run it in another new project and you'll see what's wrong. I didn't run it but I can say that this loop is working properly because your condition implies a variable that is manipulated in that loop so the condition can be false. Your problem is not in that loop.
Thanks A Lot My Dear..But Do U Have Idea For A New Loop Can Does Iteration Proper?
Can you be more exact? I don't really understand what are you saying in your last post.
Topic archived. No new replies allowed.