用C++实现线性方程组的求解
具体如下:输入方程组、处理自由变量、计算方程组的基础解系、计算方程组的特解、计算方程组的通解、计算方程组的唯一解等运算。最好给出一些例题。谢谢。虽然我第一次提问,但是,回...
具体如下:输入方程组、处理自由变量、计算方程组的基础解系、计算方程组的特解、计算方程组的通解、计算方程组的唯一解等运算。最好给出一些例题。谢谢。虽然我第一次提问,但是,回答的好的人,我给加100分……
展开
展开全部
哥以前写的Gauss-Seidel迭代和Jacobi迭代的代码:
#include <math.h>
int doIteration(const double A[], // 系数矩阵
const double b[], // 常量矩阵
double *x, // 解
double *x1, // buffer
const int iXNum, // 方程未知数个数
const int iIterTimes, // 最高迭代次,负数为无限制
const double deltaDomain // 精确度,负数为无限制
)
// 当 buffer != x 时为Jacobi迭代
// 当 buffer == x 时为Gauss-Seidel迭代
{
double radius = 1000, temp;
int i = 1;
x==x1?printf("\n\n采用Gauss-Seidel迭代"):printf("\n\n采用Jacobi迭代");
//printf("\n初始值:\n");
//for(int j = 0; j < iXNum; j++)
// printf("\tx[%d] == %lf\n", j, x[j]);
while( radius > deltaDomain){// 迭代循环
if(++i > iIterTimes && iIterTimes >= 0)
break;
//printf("\n\nThe Iteration Number:%3d\n", i);
for(int iFunNum = 0; iFunNum < iXNum; iFunNum++){ // 每个方程的求解
temp = x[iFunNum];
x1[iFunNum] = b[iFunNum];
for(int j = 0; j < iFunNum; j++)
x1[iFunNum] -= x[j]*A[iFunNum*iXNum+j];
for(int j = iFunNum + 1; j < iXNum; j++)
x1[iFunNum] -= x[j]*A[iFunNum*iXNum+j];
x1[iFunNum] /= A[iFunNum*iXNum+iFunNum];
radius = pow(temp - x1[iFunNum], 2); // 累加l-2范数的平方和
// printf("\tx[%d] == %lf\n", iFunNum+1, x1[iFunNum]);
}
for(int j = 0; j < iXNum; j++) // 将结果存至x[]中
x[j] = x1[j];
radius = sqrt(radius); // 得到l-2范数
if( radius <= deltaDomain )
printf("第%3d次迭代精确度达到要求(%lf),停止\n" , i, deltaDomain);
//printf("第%d次迭代差值的范数为:%lf", i, radius);
}
//if( radius <= deltaDomain )
// printf("第%3d次迭代精确度达到要求(%lf),停止\n" , iFunNum+1, deltaDomain);
//else
// printf("超过限制%3d次,停止;\n",iIterTimes);
return 0;
}
int geneEquation1(int n, double delta, int times){
double *x = new double [n];
double *buf = new double [n];
double *b = new double [n];
double *A = new double [n*n];
for(int i = 0; i < n; i++) {
buf[i] = 1;
x[i] = 0;
b[i] = 0;
}
for(int i = 0; i < n; i++) { // row
for(int j = 0; j < n; j++) { // col
A[i*n+j] = (i>j?i:j) + 1;
b[i] += A[i*n+j];
}
}
doIteration(A, b, x, buf, n, times, delta);
for(int i = 0; i < n; i++)
x[i] = 0;
doIteration(A, b, x, x, n, times, delta);
delete [] x;
delete [] b;
delete [] buf;
delete [] A;
return 0;
}
int geneEquation2(int n, double delta, int times){
double *x = new double [n];
double *buf = new double [n];
double *b = new double [n];
double *A = new double [n*n];
for(int i = 0; i < n; i++) {
buf[i] = 1;
x[i] = 0;
b[i] = 0;
}
for(int i = 0; i < n; i++) { // row
for(int j = 0; j < n; j++) { // col
A[i*n+j] = 1.0/((i+1)+(j+1)+1);
b[i] += A[i*n+j];
}
}
//doIteration(A, b, x, buf, n, times, delta);
for(int i = 0; i < n; i++)
x[i] = 0;
doIteration(A, b, x, x, n, times, delta);
delete [] x;
delete [] b;
delete [] buf;
delete [] A;
return 0;
}
int _tmain(int argc, _TCHAR* argv[]){
int times = -1;
// 3.1-(1), n = 10, e = 0.001
//printf("数值试验(1), n = 10, e = 0.001");
//geneEquation1(10, 0.001, times);
// 3.1-(1), n = 20, e = 0.00001
//printf("数值试验(1), n = 10, e = 0.00001");
//geneEquation1(20, 0.00001, times);
// 3.1-(2), n = 10, e = 0.001
printf("数值试验(2), n = 10, e = 0.001");
geneEquation2(10, 0.001, times);
// 3.1-(2), n = 20, e = 0.00001
printf("数值试验(2), n = 10, e = 0.00001");
geneEquation2(20, 0.00001, times);
return 0;
}
#include <math.h>
int doIteration(const double A[], // 系数矩阵
const double b[], // 常量矩阵
double *x, // 解
double *x1, // buffer
const int iXNum, // 方程未知数个数
const int iIterTimes, // 最高迭代次,负数为无限制
const double deltaDomain // 精确度,负数为无限制
)
// 当 buffer != x 时为Jacobi迭代
// 当 buffer == x 时为Gauss-Seidel迭代
{
double radius = 1000, temp;
int i = 1;
x==x1?printf("\n\n采用Gauss-Seidel迭代"):printf("\n\n采用Jacobi迭代");
//printf("\n初始值:\n");
//for(int j = 0; j < iXNum; j++)
// printf("\tx[%d] == %lf\n", j, x[j]);
while( radius > deltaDomain){// 迭代循环
if(++i > iIterTimes && iIterTimes >= 0)
break;
//printf("\n\nThe Iteration Number:%3d\n", i);
for(int iFunNum = 0; iFunNum < iXNum; iFunNum++){ // 每个方程的求解
temp = x[iFunNum];
x1[iFunNum] = b[iFunNum];
for(int j = 0; j < iFunNum; j++)
x1[iFunNum] -= x[j]*A[iFunNum*iXNum+j];
for(int j = iFunNum + 1; j < iXNum; j++)
x1[iFunNum] -= x[j]*A[iFunNum*iXNum+j];
x1[iFunNum] /= A[iFunNum*iXNum+iFunNum];
radius = pow(temp - x1[iFunNum], 2); // 累加l-2范数的平方和
// printf("\tx[%d] == %lf\n", iFunNum+1, x1[iFunNum]);
}
for(int j = 0; j < iXNum; j++) // 将结果存至x[]中
x[j] = x1[j];
radius = sqrt(radius); // 得到l-2范数
if( radius <= deltaDomain )
printf("第%3d次迭代精确度达到要求(%lf),停止\n" , i, deltaDomain);
//printf("第%d次迭代差值的范数为:%lf", i, radius);
}
//if( radius <= deltaDomain )
// printf("第%3d次迭代精确度达到要求(%lf),停止\n" , iFunNum+1, deltaDomain);
//else
// printf("超过限制%3d次,停止;\n",iIterTimes);
return 0;
}
int geneEquation1(int n, double delta, int times){
double *x = new double [n];
double *buf = new double [n];
double *b = new double [n];
double *A = new double [n*n];
for(int i = 0; i < n; i++) {
buf[i] = 1;
x[i] = 0;
b[i] = 0;
}
for(int i = 0; i < n; i++) { // row
for(int j = 0; j < n; j++) { // col
A[i*n+j] = (i>j?i:j) + 1;
b[i] += A[i*n+j];
}
}
doIteration(A, b, x, buf, n, times, delta);
for(int i = 0; i < n; i++)
x[i] = 0;
doIteration(A, b, x, x, n, times, delta);
delete [] x;
delete [] b;
delete [] buf;
delete [] A;
return 0;
}
int geneEquation2(int n, double delta, int times){
double *x = new double [n];
double *buf = new double [n];
double *b = new double [n];
double *A = new double [n*n];
for(int i = 0; i < n; i++) {
buf[i] = 1;
x[i] = 0;
b[i] = 0;
}
for(int i = 0; i < n; i++) { // row
for(int j = 0; j < n; j++) { // col
A[i*n+j] = 1.0/((i+1)+(j+1)+1);
b[i] += A[i*n+j];
}
}
//doIteration(A, b, x, buf, n, times, delta);
for(int i = 0; i < n; i++)
x[i] = 0;
doIteration(A, b, x, x, n, times, delta);
delete [] x;
delete [] b;
delete [] buf;
delete [] A;
return 0;
}
int _tmain(int argc, _TCHAR* argv[]){
int times = -1;
// 3.1-(1), n = 10, e = 0.001
//printf("数值试验(1), n = 10, e = 0.001");
//geneEquation1(10, 0.001, times);
// 3.1-(1), n = 20, e = 0.00001
//printf("数值试验(1), n = 10, e = 0.00001");
//geneEquation1(20, 0.00001, times);
// 3.1-(2), n = 10, e = 0.001
printf("数值试验(2), n = 10, e = 0.001");
geneEquation2(10, 0.001, times);
// 3.1-(2), n = 20, e = 0.00001
printf("数值试验(2), n = 10, e = 0.00001");
geneEquation2(20, 0.00001, times);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询