Demo entry 6734705

7_1

   

Submitted by anonymous on Apr 22, 2018 at 04:04
Language: C++. Code size: 3.4 kB.

#include<iostream>
#include<cmath>
#include<Windows.h>
using namespace std;
#define N 6
double Form(double F[])//ÏòÁ¿µÄ2·¶Êý
{
	double sum = 0;
	for (int i = 0; i < N; i++)
		sum += (F[i] * F[i]);
	return sqrt(sum);
}
void jacobi(double a[][N],double *b)
{
	double x[N] = { 0,0,0,0,0,0 };
	double y[N];//= { 0,0,0,0,0,0 };
	double sum;
	double e;
	double form[N];
	double k=1;
	while (1)
	{
		for (int i = 0; i < N; i++)
		{
			sum = 0;
			for (int j = 0; j < N; j++)//ÇóºÍ²¿·Ö
			{
				if (j != i)
				sum += (a[i][j] * x[j]);
			}
			y[i] = (1 / a[i][i])*(b[i] - sum);
			
		}
		for (int i = 0; i < N; i++)
			form[i] = y[i] - x[i];
		e = Form(form);
		//cout << e << endl;//Êä³öÿ´ÎµÄeÓÃÀ´²âÊÔ
		if (e < 0.0001) break;
		for(int i=0;i<N;i++)
		x[i] = y[i];
		k=k+1;
	}
	for (int i = 0; i < N; i++)
		cout <<"x"<<"["<<i+1<<"]="<< y[i]<<"  ";
	cout << endl;
	cout << "¹²¼ÆËãÁË " << k << " ´Î" << endl;//ÓÃprintf²»ÐÐcoutÈ´ÐÐ
}
void GaussSeidel(double a[][N],double *b)//ºÍÑſɱÈÀàËÆ
{
	double x[N] = { 0,0,0,0,0,0 };
	double y[N];
	double sum1, sum2;
	double e = 0;
	double form[N];
	double k=0;
	while (1)
	{
		int i, j;
		for (i = 0; i <N; i++)
		{
			//t = y[i];
			sum1 = 0; sum2 = 0;
			for (j = 0; j < i ; j++)
			{
				if (j != i)
					sum1 += a[i][j] * y[j];
			}
			for (j = i+1; j <N; j++)
			{
				if (j != i)
					sum2 += a[i][j] * x[j];
			}
			y[i] = (1 / a[i][i])*(b[i] - sum1 - sum2);
		}
		for (int i = 0; i < N; i++)
			form[i] = y[i] - x[i];
		e = Form(form);
		//cout << e << endl;//Êä³öÿ´ÎµÄeÓÃÀ´²âÊÔ
		k = k + 1;
		if (e < 0.0001) break;
		for (int i = 0; i<N; i++)
			x[i] = y[i];
		
		
	}
		for (int i = 0; i < N; i++)
			cout << "x" << "[" << i + 1 << "]=" << y[i] << "  ";
		cout << endl;
		cout << "¹²¼ÆËãÁË " << k << " ´Î" << endl;//ÓÃprintf²»ÐÐcoutÈ´ÐÐ
}

void SOR(double a[][N],double *b,double w)//ºÍ¸ß˹ÈüµÂ¶û¼¸ºõÏàͬ
{
	double x[N] = { 0,0,0,0,0,0 };
	double y[N];
	double sum1, sum2;
	double e = 0;
	double form[N];
	double k=0;
	while (1)
	{
		int i, j;
		for (i = 0; i < N; i++)
		{
			//t = y[i];
			sum1 = 0; sum2 = 0;
			for (j = 0; j < i; j++)
			{
				//if (j != i)
					sum1 += a[i][j] * y[j];
			}
			for (j = i; j < N; j++)
			{
				//if (j != i)
					sum2 += a[i][j] * x[j];
			}
			y[i] =x[i]+ (w / a[i][i])*(b[i] - sum1 - sum2);
		}
		for (int i = 0; i < N; i++)
			form[i] = y[i] - x[i];
		k = k + 1;
		e = Form(form);
		//cout << e << endl;//Êä³öÿ´ÎµÄeÓÃÀ´²âÊÔ
		if (e < 0.0001) break;
		for (int i = 0; i<N; i++)
			x[i] = y[i];
		if (k > 200) { cout << "²»ÊÕÁ²£¡\n"; return; }
	}
	for (int i = 0; i < N; i++)
		cout << "x" << "[" << i + 1 << "]=" << y[i] << "  ";
	cout << endl;
	cout << "¹²¼ÆËãÁË " << k << " ´Î" << endl;//ÓÃprintf²»ÐÐcoutÈ´ÐÐ
}

int main()
{
	double A[][N] =
	{
	4,-1,0,-1,0,0,
   -1,4,-1,0,-1,0,
	0,-1,4,-1,0,1,
	-1,0,-1,4,-1,0,
	0,-1,0,-1,4,-1,
	0,0,-1,0,-1,4
	};
	double B[N] = { 0,5,-2,5,-2,6 };
	cout << "Ñſɱȷ¨£º" << endl;
	jacobi(A, B);
	cout << endl;
	cout << "¸ß˹-ÈüµÂ¶û·¨£º" << endl;
	GaussSeidel(A, B);
	cout << endl;
	cout << "SOR·¨,w=1.334£º" << endl;
	SOR(A, B,1.334);
	cout << endl;
	cout << "SOR·¨,w=1.95£º" << endl;
	SOR(A, B, 1.95);
	cout << endl;
	cout << "SOR·¨,w=0.95£º" << endl;
	SOR(A, B, 0.95);
	cout << endl;
	system("pause");//ÕâÑùÄÜ×÷Ϊ¶ÀÁ¢.exeÓÃÇÒ²»ÉÁÍË
	return 0;
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).