数值分析编程求助!!大神快来!! :编写c语言或者c++程序,实现用高斯消元法求解线性方程组。Ax
数值分析编程求助!!大神快来!!:编写c语言或者c++程序,实现用高斯消元法求解线性方程组。Ax=b其中A是24×24的矩阵,i行i列元素全都是6,i行,i±1列的元素全...
数值分析编程求助!!大神快来!! :编写c语言或者c++程序,实现用高斯消元法求解线性方程组。Ax=b 其中A是24×24的矩阵,i行i列元素全都是6,i行,i±1列的元素全是2,i行,i±2列的元素全是1。剩下的元素全是0。 b是向量(1,2,3,......23,24)
求大神给个代码或者格式!记住是高斯消元法。 展开
求大神给个代码或者格式!记住是高斯消元法。 展开
2个回答
展开全部
§说明: 以下代码在VS2010中实现,可以验证结果是对的.
#include<stdio.h>
int main(){
double A[24][24],b[24],x[24];
int i,j;
//给A,b赋初值
for(i=0;i<24;i++){
b[i]=i+1;
for(j=0;j<24;j++){
if(j==i-2)A[i][j]=1;
else if(j==i-1)A[i][j]=2;
else if(j==i)A[i][j]=6;
else if(j==i+1)A[i][j]=2;
else if(j==i+2)A[i][j]=1;
else A[i][j]=0;
}
}
//在屏幕上输出线性方程组
for(i=0;i<24;i++){
for(j=0;j<24;j++){
printf("%2.f",A[i][j]);
}printf(" %2.f\n",b[i]);
}
//高斯消去(★★精髓:注意A中0的分布★★)
for(i=0;i<22;i++){
A[i+1][i]=A[i+1][i]/A[i][i];
A[i+1][i+1]-=A[i][i+1]*A[i+1][i];
A[i+1][i+2]-=A[i][i+2]*A[i+1][i];
b[i+1]-=b[i]*A[i+1][i];
A[i+2][i]=A[i+2][i]/A[i][i];
A[i+2][i+1]-=A[i][i+1]*A[i+2][i];
A[i+2][i+2]-=A[i][i+2]*A[i+2][i];
b[i+2]-=b[i]*A[i+2][i];
}
A[23][22]/=A[22][22];
A[23][23]-=A[23][22]*A[22][23];
b[23]-=b[22]*A[23][22];
//解上三角方程组
x[23]=b[23]/A[23][23];
x[22]=(b[22]-x[23]*A[22][23])/A[22][22];
for(i=21;i>=0;i--)
x[i]=(b[i]-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 6 2 1 0 0 0 19
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 2 1 0 0 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 2 1 0 21
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 2 1 22
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 2 23
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 24
★★★方程组的解(依次递增)★★★
0.0675341 0.1717027 0.2513901 0.3319355 0.4168487 0.5002066
0.5832332 0.6666609 0.7500171 0.8333273 0.9166876 0.9999660
1.0832707 1.1669598 1.2498363 1.3320611 1.4196698 1.5016614
1.5646997 1.6892313 1.8103466 1.6128581 1.9690418 3.0748430
#include<stdio.h>
int main(){
double A[24][24],b[24],x[24];
int i,j;
//给A,b赋初值
for(i=0;i<24;i++){
b[i]=i+1;
for(j=0;j<24;j++){
if(j==i-2)A[i][j]=1;
else if(j==i-1)A[i][j]=2;
else if(j==i)A[i][j]=6;
else if(j==i+1)A[i][j]=2;
else if(j==i+2)A[i][j]=1;
else A[i][j]=0;
}
}
//在屏幕上输出线性方程组
for(i=0;i<24;i++){
for(j=0;j<24;j++){
printf("%2.f",A[i][j]);
}printf(" %2.f\n",b[i]);
}
//高斯消去(★★精髓:注意A中0的分布★★)
for(i=0;i<22;i++){
A[i+1][i]=A[i+1][i]/A[i][i];
A[i+1][i+1]-=A[i][i+1]*A[i+1][i];
A[i+1][i+2]-=A[i][i+2]*A[i+1][i];
b[i+1]-=b[i]*A[i+1][i];
A[i+2][i]=A[i+2][i]/A[i][i];
A[i+2][i+1]-=A[i][i+1]*A[i+2][i];
A[i+2][i+2]-=A[i][i+2]*A[i+2][i];
b[i+2]-=b[i]*A[i+2][i];
}
A[23][22]/=A[22][22];
A[23][23]-=A[23][22]*A[22][23];
b[23]-=b[22]*A[23][22];
//解上三角方程组
x[23]=b[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 0 0 0 0 0 1 2 6 2 1 0 0 0 0 18
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 2 1 0 0 0 19
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 2 1 0 0 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 2 1 0 21
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 2 1 22
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 2 23
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 24
★★★方程组的解(依次递增)★★★
0.0675341 0.1717027 0.2513901 0.3319355 0.4168487 0.5002066
0.5832332 0.6666609 0.7500171 0.8333273 0.9166876 0.9999660
1.0832707 1.1669598 1.2498363 1.3320611 1.4196698 1.5016614
1.5646997 1.6892313 1.8103466 1.6128581 1.9690418 3.0748430
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
2014-03-26
展开全部
客人付款v迷人
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询