當前位置:首頁 » 編程語言 » C語言鬆弛法解線性方程組
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

C語言鬆弛法解線性方程組

發布時間: 2022-05-16 00:04:56

Ⅰ 設計一個C程序,解線性方程組

原來的程序為:(那位朋友幫我改一下)

#include "stdlib.h"
#include "math.h"
#include "stdio.h"

#define MAX 255

int Guass(a,b,n)
int n;
double a[],b[];
{
int *js,l,k,i,j,is,p,q;
double d,t;
js=malloc(n*sizeof(int));
l=1;
for (k=0;k<=n-2;k++)
{
d=0.0;
/*下面是換主元部分,即從系數矩陣A的第K行,第K列之下的部分選出
絕對值最大的元,交換到對角線上。*/
for (i=k;i<=n-1;i++)
for (j=k;j<=n-1;j++)
{
t=fabs(a[i*n+j]); /*fabs()定義在math.h中,含義是求一個浮點數的絕對值。*/
if (t>d) { d=t; js[k]=j; is=i;}
}
if (d+1.0==1.0) l=0; /*主元為0*/
/*主元不為0的時候*/
else
{
if (js[k]!=k)
for (i=0;i<=n-1;i++)
{
p=i*n+k; q=i*n+js[k];
t=a[p]; a[p]=a[q]; a[q]=t;
}
if (is!=k)
{
for (j=k;j<=n-1;j++)
{
p=k*n+j; q=is*n+j;
t=a[p]; a[p]=a[q]; a[q]=t;
}
t=b[k]; b[k]=b[is]; b[is]=t;
}
}
if (l==0)
{
free(js); printf("fail\n");
return(0);
}
d=a[k*n+k];
/*下面為歸一化部分*/
for (j=k+1;j<=n-1;j++)
{
p=k*n+j; a[p]=a[p]/d;
}
b[k]=b[k]/d;
/*下面為矩陣A,B消元部分*/
for (i=k+1;i<=n-1;i++)
{
for (j=k+1;j<=n-1;j++)
{
p=i*n+j;
a[p]=a[p]-a[i*n+k]*a[k*n+j];
}
b[i]=b[i]-a[i*n+k]*b[k];
}
}

d=a[(n-1)*n+n-1];
/*矩陣無解或有無限多解*/
if (fabs(d)+1.0==1.0)
{
free(js); printf("該矩陣為奇異矩陣\n");
return(0);
}
b[n-1]=b[n-1]/d;
/*下面為迭代消元*/
for (i=n-2;i>=0;i--)
{
t=0.0;
for (j=i+1;j<=n-1;j++)
t=t+a[i*n+j]*b[j];
b[i]=b[i]-t;
}
js[n-1]=n-1;
for (k=n-1;k>=0;k--)
if (js[k]!=k)
{ t=b[k]; b[k]=b[js[k]]; b[js[k]]=t;}
free(js);
return(1);
}

main()
{
int i,n;
double A[MAX];
double B[MAX];
clrscr();
puts("This is a program to solve N order linear equation set Ax=B.");
puts("\n It use Guass Elimination Method to solve the equation set:");
puts("\n a(0,0)x0+a(0,1)x1+a(0,2)x2+...+a(0,n-1)xn-1=b0");
puts(" a(1,0)x0+a(1,1)x1+a(1,2)x2+...+a(1,n-1)xn-1=b1");
puts(" ......");
puts(" a(n-1,0)x0+a(n-1,1)x1+a(n-1,2)x2+...+a(n-1,-1)xn-1=bn-1\n");
printf(" >> Please input the order n (>1): ");
scanf("%d",&n);
printf(" >> Please input the %d elements of matrix A(%d*%d) one by one:\n",n*n,n,n);
for(i=0;i<n*n;i++)
scanf("%lf",&A[i]);
printf(" >> Please input the %d elements of matrix B(%d*1) one by one:\n",n,n);
for(i=0;i<n;i++)
scanf("%lf",&B[i]);
if (Guass(A,B,n)!=0) /*調用Guass消去,1為計算成功*/
printf(" >> The solution of Ax=B is x(%d*1):\n",n);

for (i=0;i<n;i++) /*列印結果*/
printf("x(%d)=%f ",i,B[i]);
puts("\n Press any key to quit...");
getch();
}



c語言實現doolittle演算法解線性方程組

我以前剛好寫過,在我的博客里,你可以去看看
http://hi..com/ycdoit/blog/item/832586b066955d5d082302ef.html
c++解答齊次方程組的幾種方法——Cramer,Gauss列主元,Gauss全主元,Doolittle演算法/
/解線性方程組

//By JJ,2008
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>

//----------------------------------------------全局變數定義區
const int Number=15; //方程最大個數
double a[Number][Number],b[Number],_a[Number][Number],_b[Number]; //系數行列式
int A_y[Number]; //a[][]中隨著橫坐標增加列坐標的排列順序,如a[0][0],a[1][2],a[2][1]...則A_y[]={0,2,1...};
int lenth,_lenth; //方程的個數
double a_sum; //計算行列式的值
char * x; //未知量a,b,c的載體

//----------------------------------------------函數聲明區
void input(); //輸入方程組
void print_menu(); //列印主菜單
int choose (); //輸入選擇
void cramer(); //Cramer演算法解方程組
void gauss_row(); //Gauss列主元解方程組
void guass_all(); //Gauss全主元解方程組
void Doolittle(); //用Doolittle演算法解方程組
int Doolittle_check(double a[][Number],double b[Number]); //判斷是否行列式>0,若是,調整為順序主子式全>0
void xiaoqu_u_l(); //將行列式Doolittle分解
void calculate_u_l(); //計算Doolittle結果
double & calculate_A(int n,int m); //計算行列式
double quanpailie_A(); //根據列坐標的排列計算的值,如A_y[]={0,2,1},得sum=a[0][ A_y[0] ] * a[1][ A_y[1] ] * a[2][ A_y[2] ]=a[0][0]*a[1][2]*a[2][1];
void exchange(int m,int i); //交換A_y[m],A_y[i]
void exchange_lie(int j); //交換a[][j]與b[];
void exchange_hang(int m,int n); //分別交換a[][]和b[]中的m與n兩行
void gauss_row_xiaoqu(); //Gauss列主元消去法
void gauss_all_xiaoqu(); //Gauss全主元消去法
void gauss_calculate(); //根據Gauss消去法結果計算未知量的值
void exchange_a_lie(int m,int n); //交換a[][]中的m和n列
void exchange_x(int m,int n); //交換x[]中的x[m]和x[n]
void recovery(); //恢復數據

//主函數
void main()
{
int flag=1;
input(); //輸入方程
while(flag)
{
print_menu(); //列印主菜單

flag=choose(); //選擇解答方式
}

}

//函數定義區
void print_menu()
{
system("cls");
cout<<"------------方程系數和常數矩陣表示如下:\n";
for(int j=0;j<lenth;j++)
cout<<"系數"<<j+1<<" ";
cout<<"\t常數";
cout<<endl;
for(int i=0;i<lenth;i++)
{
for(j=0;j<lenth;j++)
cout<<setw(8)<<setiosflags(ios::left)<<a[i][j];
cout<<"\t"<<b[i]<<endl;
}
cout<<"-----------請選擇方程解答的方案----------";
cout<<"\n 1. 克拉默(Cramer)法則";
cout<<"\n 2. Gauss列主元消去法";
cout<<"\n 3. Gauss全主元消去法";
cout<<"\n 4. Doolittle分解法";
cout<<"\n 5. 退出";
cout<<"\n 輸入你的選擇:";

}

void input()
{ int i,j;
cout<<"方程的個數:";
cin>>lenth;
if(lenth>Number)
{
cout<<"It is too big.\n";
return;
}
x=new char[lenth];
for(i=0;i<lenth;i++)
x[i]='a'+i;

//輸入方程矩陣
//提示如何輸入
cout<<"====================================================\n";
cout<<"請在每個方程里輸入"<<lenth<<"系數和一個常數:\n";
cout<<"例:\n方程:a";
for(i=1;i<lenth;i++)
{
cout<<"+"<<i+1<<x[i];
}
cout<<"=10\n";
cout<<"應輸入:";
for(i=0;i<lenth;i++)
cout<<i+1<<" ";
cout<<"10\n";
cout<<"==============================\n";

//輸入每個方程
for(i=0;i<lenth;i++)
{
cout<<"輸入方程"<<i+1<<":";
for(j=0;j<lenth;j++)
cin>>a[i][j];
cin>>b[i];
}

//備份數據
for(i=0;i<lenth;i++)
for(j=0;j<lenth;j++)
_a[i][j]=a[i][j];
for(i=0;i<lenth;i++)
_b[i]=b[i];
_lenth=lenth;
}

//輸入選擇
int choose()
{
int choice;char ch;
cin>>choice;
switch(choice)
{
case 1:cramer();break;
case 2:gauss_row();break;
case 3:guass_all();break;
case 4:Doolittle();break;
case 5:return 0;
default:cout<<"輸入錯誤,請重新輸入:";
choose();
break;
}
cout<<"\n是否換種方法求解(Y/N):";
cin>>ch;
if(ch=='n'||ch=='N') return 0;
recovery();
cout<<"\n\n\n";
return 1;

}

//用克拉默法則求解方程.
void cramer()
{
int i,j;double sum,sum_x;char ch;
//令第i行的列坐標為i
cout<<"用克拉默(Cramer)法則結果如下:\n";

for(i=0;i<lenth;i++)
A_y[i]=i;
sum=calculate_A(lenth,0);
if(sum!=0)
{
cout<<"系數行列式不為零,方程有唯一的解:";
for(i=0;i<lenth;i++)
{ ch='a'+i;
a_sum=0;
for(j=0;j<lenth;j++)
A_y[j]=j;
exchange_lie(i);
sum_x=calculate_A(lenth,0);
cout<<endl<<ch<<"="<<sum_x/sum;
exchange_lie(i);
}
}
else
{
cout<<"系數行列式等於零,方程沒有唯一的解.";
}
cout<<"\n";
}

double & calculate_A(int n,int m) //計算行列式
{ int i;
if(n==1) {
a_sum+= quanpailie_A();
}
else{for(i=0;i<n;i++)
{ exchange(m,m+i);
calculate_A(n-1,m+1);
exchange(m,m+i);
}
}
return a_sum;
}

double quanpailie_A() //計算行列式中一種全排列的值
{
int i,j,l;
double sum=0,p;
for(i=0,l=0;i<lenth;i++)
for(j=0;A_y[j]!=i&&j<lenth;j++)
if(A_y[j]>i) l++;
for(p=1,i=0;i<lenth;i++)
p*=a[i][A_y[i]];
sum+=p*((l%2==0)?(1):(-1));
return sum;
}

//高斯列主元排列求解方程
void gauss_row()
{
int i,j;
gauss_row_xiaoqu(); //用高斯列主元消區法將系數矩陣變成一個上三角矩陣

for(i=0;i<lenth;i++)
{
for(j=0;j<lenth;j++)
cout<<setw(10)<<setprecision(5)<<a[i][j];
cout<<setw(10)<<b[i]<<endl;
}

if(a[lenth-1][lenth-1]!=0)
{

cout<<"系數行列式不為零,方程有唯一的解:\n";
gauss_calculate();
for(i=0;i<lenth;i++) //輸出結果
{
cout<<x[i]<<"="<<b[i]<<"\n";
}
}
else
cout<<"系數行列式等於零,方程沒有唯一的解.\n";
}

void gauss_row_xiaoqu() //高斯列主元消去法
{
int i,j,k,maxi;double lik;
cout<<"用Gauss列主元消去法結果如下:\n";
for(k=0;k<lenth-1;k++)
{
j=k;
for(maxi=i=k;i<lenth;i++)
if(a[i][j]>a[maxi][j]) maxi=i;
if(maxi!=k)
exchange_hang(k,maxi);//

for(i=k+1;i<lenth;i++)
{
lik=a[i][k]/a[k][k];
for(j=k;j<lenth;j++)
a[i][j]=a[i][j]-a[k][j]*lik;
b[i]=b[i]-b[k]*lik;
}
}
}

//高斯全主元排列求解方程
void guass_all()
{
int i,j;
gauss_all_xiaoqu();
for(i=0;i<lenth;i++)
{
for(j=0;j<lenth;j++)
cout<<setw(10)<<setprecision(5)<<a[i][j];
cout<<setw(10)<<b[i]<<endl;
}
if(a[lenth-1][lenth-1]!=0)
{

cout<<"系數行列式不為零,方程有唯一的解:\n";
gauss_calculate();

for(i=0;i<lenth;i++) //輸出結果
{
for(j=0;x[j]!='a'+i&&j<lenth;j++);

cout<<x[j]<<"="<<b[j]<<endl;
}
}
else
cout<<"系數行列式等於零,方程沒有唯一的解.\n";
}

void gauss_all_xiaoqu() //Gauss全主元消去法
{
int i,j,k,maxi,maxj;double lik;
cout<<"用Gauss全主元消去法結果如下:\n";

for(k=0;k<lenth-1;k++)
{

for(maxi=maxj=i=k;i<lenth;i++)
{
for(j=k;j<lenth;j++)
if(a[i][j]>a[maxi][ maxj])
{ maxi=i;
maxj=j;
}

}
if(maxi!=k)
exchange_hang(k,maxi);
if(maxj!=k)
{
exchange_a_lie(maxj,k); //交換兩列
exchange_x(maxj,k);

}

for(i=k+1;i<lenth;i++)
{
lik=a[i][k]/a[k][k];
for(j=k;j<lenth;j++)
a[i][j]=a[i][j]-a[k][j]*lik;
b[i]=b[i]-b[k]*lik;
}
}
}

void gauss_calculate() //高斯消去法以後計算未知量的結果
{
int i,j;double sum_ax;
b[lenth-1]=b[lenth-1]/a[lenth-1][lenth-1];
for(i=lenth-2;i>=0;i--)
{
for(j=i+1,sum_ax=0;j<lenth;j++)
sum_ax+=a[i][j]*b[j];
b[i]=(b[i]-sum_ax)/a[i][i];
}
}

void Doolittle() //Doolittle消去法計算方程組
{
double temp_a[Number][Number],temp_b[Number];int i,j,flag;
for(i=0;i<lenth;i++)
for(j=0;j<lenth;j++)
temp_a[i][j]=a[i][j];
flag=Doolittle_check(temp_a,temp_b);
if(flag==0) cout<<"\n行列式為零.無法用Doolittle求解.";
xiaoqu_u_l();
calculate_u_l();
cout<<"用Doolittle方法求得結果如下:\n";
for(i=0;i<lenth;i++) //輸出結果
{
for(j=0;x[j]!='a'+i&&j<lenth;j++);

cout<<x[j]<<"="<<b[j]<<endl;
}

}

void calculate_u_l() //計算Doolittle結果
{ int i,j;double sum_ax=0;
for(i=0;i<lenth;i++)
{
for(j=0,sum_ax=0;j<i;j++)
sum_ax+=a[i][j]*b[j];
b[i]=b[i]-sum_ax;
}

for(i=lenth-1;i>=0;i--)
{
for(j=i+1,sum_ax=0;j<lenth;j++)
sum_ax+=a[i][j]*b[j];
b[i]=(b[i]-sum_ax)/a[i][i];
}

}

void xiaoqu_u_l() //將行列式按Doolittle分解
{ int i,j,n,k;double temp;
for(i=1,j=0;i<lenth;i++)
a[i][j]=a[i][j]/a[0][0];
for(n=1;n<lenth;n++)
{ //求第n+1層的上三角矩陣部分即U
for(j=n;j<lenth;j++)
{ for(k=0,temp=0;k<n;k++)
temp+=a[n][k]*a[k][j];
a[n][j]-=temp;
}
for(i=n+1;i<lenth;i++) //求第n+1層的下三角矩陣部分即L
{ for(k=0,temp=0;k<n;k++)
temp+=a[i][k]*a[k][n];
a[i][n]=(a[i][n]-temp)/a[n][n];
}
}
}

int Doolittle_check(double temp_a[][Number],double temp_b[Number]) //若行列式不為零,將系數矩陣調整為順序主子式大於零
{
int i,j,k,maxi;double lik,temp;

for(k=0;k<lenth-1;k++)
{
j=k;
for(maxi=i=k;i<lenth;i++)
if(temp_a[i][j]>temp_a[maxi][j]) maxi=i;
if(maxi!=k)
{ exchange_hang(k,maxi);
for(j=0;j<lenth;j++)
{ temp=temp_a[k][j];
temp_a[k][j]=temp_a[maxi][j];
temp_a[maxi][j]=temp;
}
}
for(i=k+1;i<lenth;i++)
{
lik=temp_a[i][k]/temp_a[k][k];
for(j=k;j<lenth;j++)
temp_a[i][j]=temp_a[i][j]-temp_a[k][j]*lik;
temp_b[i]=temp_b[i]-temp_b[k]*lik;
}
}

if(temp_a[lenth-1][lenth-1]==0) return 0;
return 1;
}

void exchange_hang(int m,int n) //交換a[][]中和b[]兩行
{
int j; double temp;
for(j=0;j<lenth;j++)
{ temp=a[m][j];
a[m][j]=a[n][j];
a[n][j]=temp;

}
temp=b[m];
b[m]=b[n];
b[n]=temp;
}

void exchange(int m,int i) //交換A_y[m],A_y[i]
{ int temp;
temp=A_y[m];
A_y[m]=A_y[i];
A_y[i]=temp;
}

void exchange_lie(int j) //交換未知量b[]和第i列
{ double temp;int i;
for(i=0;i<lenth;i++)
{ temp=a[i][j];
a[i][j]=b[i];
b[i]=temp;
}
}

void exchange_a_lie(int m,int n) //交換a[]中的兩列
{ double temp;int i;
for(i=0;i<lenth;i++)
{ temp=a[i][m];
a[i][m]=a[i][n];
a[i][n]=temp;
}
}

void exchange_x(int m,int n) //交換未知量x[m]與x[n]
{ char temp;
temp=x[m];
x[m]=x[n];
x[n]=temp;
}

void recovery() //用其中一種方法求解後恢復數據以便用其他方法求解
{
for(int i=0;i<lenth;i++)
for(int j=0;j<lenth;j++)
a[i][j]=_a[i][j];
for(i=0;i<lenth;i++)
b[i]=_b[i];
for(i=0;i<lenth;i++)
x[i]='a'+i;
a_sum=0;
lenth=_lenth;
}

Ⅲ 急求:直接迭代法 鬆弛迭代法解方程組的C語言或VB程序

哈哈哈,看來還是我無聊啊!哈哈哈哈
'本程序除了系數和初值部分,其餘都很容擴展到不同方程組。
'本程序沒有排除同構方程(既不定解)的情況。

Const Emp = 0.001, Faint = 0.3, n = 3 '允許誤差、鬆弛系數、方程數(元數)
Dim a(n - 1, n) As Single, Order(n - 1) As Integer

Private Sub Command1_Click()
Dim x(n) As Single, Delta(n) As Single, MaxEmp As Single '初始值、迭代結果、本論最大誤差
Dim m As Long
x(0) = 1 '初值
x(1) = 1
x(2) = 1
x(3) = 1 '為常數設置的。

Do
root x(), Delta() '調用鬆弛求根過程
MaxEmp = 0
For i = 0 To n - 1
x(i) = x(i) - Delta(i) '用鬆弛結果來修正當前值
If MaxEmp < Abs(Delta(i)) Then MaxEmp = Abs(Delta(i)) '尋出最大修正值
Next i
If m > 100 Then '循環次數過多,即不收斂
MsgBox "給出的初值不合適,請更換一組較為合適的初值,然後重新計算。", vbInformation
Exit Sub
End If
Loop Until MaxEmp < Emp '誤差控製成功
For i = 0 To n - 1 '輸出結果
Print "X"; Trim(Str(i)); "="; x(i)
Next i
End Sub

Private Sub Form_Load() '這是系數和常數,注意常數要改變符號
a(0, 0) = 2
a(0, 1) = -3
a(0, 2) = 1
a(0, 3) = -8
a(1, 0) = 1
a(1, 1) = 1
a(1, 2) = 6
a(1, 3) = -69
a(2, 0) = 12
a(2, 1) = 1
a(2, 2) = 2
a(2, 3) = -12
'把較大的系數交換到主對角線上,否則會不收斂
For i = 0 To n - 2
For j = i + 1 To n - 1
If Abs(a(i, i)) < Abs(a(j, i)) Then
For k = 0 To n
temp = a(i, k)
a(i, k) = a(j, k)
a(j, k) = temp
Next k
End If
Next j
Next i
End Sub

Private Sub root(x0() As Single, Delta() As Single)
Dim EquVal As Single
For i = 0 To n - 1
EquVal = 0
For j = 0 To n
EquVal = EquVal + a(i, j) * x0(j)
Next j
Delta(i) = EquVal / a(i, i) * Faint
Next i
End Sub

Ⅳ C語言程序設計:線性方程組求解

程序能成功運行,用的高斯消元法
不過沒有給出菜單
輸入提示信息比較清楚
某次的結果如下:
你要解幾元線性方程組:
2
請輸入第1行相應的系數:
a[0][0]: 2
a[0][1]: -1
請輸入第1行相應的常數:
b[0]: 3
請輸入第2行相應的系數:
a[1][0]: 1
a[1][1]: 1
請輸入第2行相應的常數:
b[1]: 9
方程組:
2.000000X1 - X2= 3.000000
X1 + X2= 9.000000
該方程組的解為:
x1 = 4.00
x2 = 5.00
Press any key to continue

程序如下:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>

int GS(int,double**,double *,double);
double **TwoArrayAlloc(int,int);
void TwoArrayFree(double **);

void main()
{
int i,j,n;
double ep,**a,*b;
ep = 1e-4;
printf("你要解幾元線性方程組:\n");
scanf("%d",&n);
a = TwoArrayAlloc(n,n);
b = (double *)calloc(n,sizeof(double));
if(b == NULL)
{
printf("內存分配失敗\n");
exit(1);
}

for(i=0;i<n;i++)
{
printf("請輸入第%d行相應的系數:\n",i+1);
for(j=0;j<n;j++)
{
printf("a[%d][%d]: ",i,j);
scanf("%lf",a[i]+j);
fflush(stdin);
}
printf("請輸入第%d行相應的常數:\n",i+1);
printf("b[%d]: ",i);
scanf("%lf",b+i);
fflush(stdin);
}
printf("方程組:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>0)
{
if(j>0)printf(" + ");
if(a[i][j]!=1)
printf("%lfX%d",a[i][j],j+1);
else
printf("X%d",j+1);

}
if(a[i][j]<0)
{
if(j>0)printf(" - ");
if(a[i][j]!=-1)
printf("%lfX%d",fabs(a[i][j]),j+1);
else
printf("X%d",j+1);
}

}
printf("= %lf\n",b[i]);
}

if(!GS(n,a,b,ep))
{
printf("不可以用高斯消去法求解\n");
exit(0);
}
printf("該方程組的解為:\n");
for(i=0;i<n;i++)
printf("x%d = %.2f\n",i+1,b[i]);
TwoArrayFree(a);
free(b);
}

int GS(int n,double **a,double *b,double ep)
{
int i,j,k,l;
double t;
for(k=1;k<=n;k++)
{
for(l=k;l<=n;l++)
if(fabs(a[l-1][k-1])>ep)
break;
else if(l==n)
return(0);
if(l!=k)
{
for(j=k;j<=n;j++)
{
t = a[k-1][j-1];
a[k-1][j-1]=a[l-1][j-1];
a[l-1][j-1]=t;
}
t=b[k-1];
b[k-1]=b[l-1];
b[l-1]=t;
}
t=1/a[k-1][k-1];
for(j=k+1;j<=n;j++)
a[k-1][j-1]=t*a[k-1][j-1];
b[k-1]*=t;
for(i=k+1;i<=n;i++)
{
for(j=k+1;j<=n;j++)
a[i-1][j-1]-=a[i-1][k-1]*a[k-1][j-1];
b[i-1]-=a[i-1][k-1]*b[k-1];
}
}
for(i=n-1;i>=1;i--)
for(j=i+1;j<=n;j++)
b[i-1]-=a[i-1][j-1]*b[j-1];
return(1);
}

double **TwoArrayAlloc(int r,int c)
{
double *x,**y;
int n;
x=(double *)calloc(r*c,sizeof(double));
y=(double **)calloc(r,sizeof(double*));
if(!x||!y)
{
printf("內存分配失敗\n");
exit(1);
}
for(n=0;n<=r-1;++n)
y[n]=&x[c*n];
return (y);
}

void TwoArrayFree(double **x)
{
free(x[0]);
free(x);
}

Ⅳ C語言求解線性方程組

double a[],b[];是定義了兩個全局數組,可以放在agaus(a,b,n)上面。在C++中才可以把int i放在for()中,c中不可以。

Ⅵ c語言解線性方程組的編程題 【做的好會多給分】

以下演算法的適用條件:A的各階主子式不為零
另外還可以採用
直接法:
消元法:Gauss-Jordan消元法,
分解法:Dolittle分解 (我用的是Courant分解法),追趕法,對稱正定矩陣的LDL『分解
----------
迭代法:
Jacobi迭代
Gauss-Seidel迭代
鬆弛迭代
-----------------
你上網可以搜索一下,或者看看數值計算方面的書

OK, 你看看這個, 另外還加了注釋 :
Courant分解演算法:
aX = b, 作 A=LU, L是下三角矩陣, U是上三角矩陣
即L =
| L11
| L21 L22
| L31 L32 L33
| ..............
| Ln1 Ln2 ........Lnn

U =
| 1 U12 ..... U1n
| 空格 1 ..... U2n
| 空格 空格 ........
| 空格 空格 空格 空格 空格1
---------------------------------------------------
aX = b -----> LUX = b
記 UX = y,
由Ly = b得到
因為無法輸出數學符號,以下採用[i, j]Ai 表示對Ai從i到j求和
yi = (bi - [j=1, i-1]Lij yj) / Lii i = 1, 2, ..., n
由UX = y得到
xi = yi - [j=i+1, n]uij xj j = n, n-1, ..., 2, 1
你在紙上驗證一下就明白了
--------------------------------------------------------------

以下採用Courant分解 解 aX = b, 經檢查,程序運行正確
這是運行結果:
--------------------------------------------------------------
Input n value(dim of Ax=b): 3
Now input the matrix a(i, j), i, j = 0, ..., 2:
1 2 1 -2 -1 -5 0 -1 6
Now input the matrix b(i), i = 0, ..., 2:
24 -63 50
Solve...x_i =
7.000000
4.000000
9.000000
--------------------------------------------------------------
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#define MAX_N 20
int main(int argc, char* argv[])
{
int n; // 未知數個數
int i, j, k;
static double a[MAX_N][MAX_N], b[MAX_N], x[MAX_N], y[MAX_N];
static double l[MAX_N][MAX_N], u[MAX_N][MAX_N];
printf("\nInput n value(dim of Ax=b): ");
scanf("%d", &n);
if(n >MAX_N)
{
printf("The input n is larger than MAX_N, please redefine the MAX_N.\n");
return 1;
}
if(n <= 0)
{
printf("Please input a number between 1 and %d.\n", MAX_N);
return 1;
}
// {{ 程序輸入
printf("Now input the matrix a(i, j), i, j = 0, ..., %d:\n", n-1);
for (i=0; i<n; i++)
for (j=0; j<n; j++)
scanf("%lf", &a[i][j]);
printf("Now input the matrix b(i), i = 0, ..., %d:\n", n-1);
for(i=0; i<n; i++)
scanf("%lf", &b[i]);
// }} 程序輸入
for(i=0; i<n; i++)
u[i][i] = 1; //
for(k=0; k<n; k++)
{
for(i=k; i<n; i++) // 計算L的第k列元素
{
l[i][k] = a[i][k];
for(j=0; j<=k-1; j++)
l[i][k] -= (l[i][j]*u[j][k]);
}
for(j=k+1; j<n; j++) //計算U的第k行元素
{
u[k][j] = a[k][j];
for(i=0; i<=k-1; i++)
u[k][j] -= (l[k][i]*u[i][j]);
u[k][j] /= l[k][k];
}
}
for(i=0; i<n; i++) // 解Ly = b
{
y[i] = b[i];
for(j=0; j<=i-1; j++)
y[i] -= (l[i][j]*y[j]);
y[i] /= l[i][i];
}
for(i=n-1; i>=0; i--) // 解UX = Y
{
x[i]=y[i];
for(j=i+1; j<n; j++)
x[i] -= (u[i][j]*x[j]);
}
printf("Solve...x_i = \n"); // 輸出結果
for(i=0; i<n; i++)
printf("%f\n", x[i]);
return 0;
}

Ⅶ c語言程序設計 n維線性方程組的求解程序

#include<iostream>

usingnamespacestd;

voidmain()

{

inti=0,j=0,k,n,sum=0;

cout<<"輸入未知數個數:";

cin>>n;

double**a=newdouble*[n+1];

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

{

a[i]=newdouble[n+1];

}

double*b=newdouble[n+1];

cout<<"輸入線性方程的系數矩的每一行和等號右側結果:"<<endl;

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

cin>>a[i][j];

}

cin>>b[i];

}

k=1;

while(1)

{

for(i=k+1;i<=n;i++)

{

a[i][k]=a[i][k]/a[k][k];

}

for(i=k+1;i<=n;i++)

{

for(j=k+1;j<=n;j++)

{

a[i][j]=a[i][j]-a[i][k]*a[k][j];

}

b[i]=b[i]-a[i][k]*b[k];

}

if(k!=(n-1)) k++;

elsebreak;

}

b[n]=b[n]/a[n][n];

for(i=n-1;i>=1;i--)

{

for(j=i+1;j<=n;j++)

{

b[i]-=a[i][j]*b[j];

}

b[i]=(1/a[i][i])*b[i];

sum=0;

}

for(i=1;i<=n;i++)

{

cout<<"x"<<i<<"="<<b[i]<<endl;

}

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

{

delete[]a[i];

}

delete[]a;

delete[]b;

}

如果你要計算方程組:

2x1-x2+3x3=1

4x1+2x2+5x3=4

x1+2x2+0x3=7