怎麼寫一個用高斯消去法求方程式解的程式

Home Home
引用 | 編輯 allenmail
2005-05-28 13:45
樓主
推文 x0
我想應該是用陣列去寫吧
或者是有其他方法
可以請大大交我一下 ..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 唐老鴨
2005-05-28 15:49
1樓
  
用二維陣列或用串列結構去寫.....
用陣列的寫法會比較簡單....

獻花 x1
引用 | 編輯 allenmail
2005-05-29 15:07
2樓
  
可以請樓上大大給我詳細的程式碼嗎?
因為我不太會寫
麻煩你囉

獻花 x0
引用 | 編輯 唐老鴨
2005-05-29 17:48
3樓
  
下面是引用allenmail於2005-05-29 15:07發表的 :
可以請樓上大大給我詳細的程式碼嗎?
因為我不太會寫
麻煩你囉

因為你的題目還算有趣....
所以幫你寫了部分的程式....
主要的消去法已經幫你寫好了...
剩下就是判斷妳題目的解....
我是依照妳的題目來寫的....
所以這題的答案是無解....
後面的判斷寫法很簡單...
就留給你自己寫囉....

PS.//測試宣告和//進度測試的迴圈你可以殺掉...我只是測試用的...記的自己把< i >改成[ i ]....

複製程式
#include <iostream.h>
#include <iomanip.h>

void main()
{
       int i,j,k;
       float temp;
       float arg[3][3] = {{1,2,1},{2,1,-1},{-1,1,2}};
       float sol[3] = {4,-1,3};
       
       //測試宣告
                for (i=0;i<3;i++)
       {
          for (j=0;j<3;j++)
          {
                 cout<<setw(4)<<arg[i][j];
          }
          cout<<setw(4)<<sol[i]<<endl;
       }

       //主要演算法
       for (i=0;i<3;i++)
       {
          for (j=0;j<3;j++)
          {
             if (i!=j)
                {
                   temp = arg[i][i] / arg[j][i];
                                            
                      for (k=0;k<3;k++)
                      {
                             arg[j][k] *= temp;
                             arg[j][k] = arg[i][k] -arg[j][k];
                      }
                      sol[j] *= temp;
                   sol[j] = sol[i] - sol[j];
                
                }
          }
          
          //進度測試
          cout<<endl<<endl;
          for (int x=0;x<3;x++)
          {
                   for(int y=0;y<3;y++)
                 {
                        cout<<setw(6)<<arg[x][y];
                 }
              cout<<setw(6)<<sol[x]<<endl;
          }
       }
               //這邊加入妳判斷的部分
}



獻花 x2
引用 | 編輯 allenmail
2005-05-30 03:54
4樓
  
嗯嗯
謝謝你的熱情分享
把我最想要的消去法給寫了出來
送你一朵花~~
當作答謝
以後有機會再請教你囉!

獻花 x0
引用 | 編輯 allenmail
2005-05-30 03:56
5樓
  
當然我也希望有其他大大能有不同的寫法
多多益善嘛
哈~

獻花 x0
引用 | 編輯 allenmail
2005-06-22 15:14
6樓
  
#include <stdio.h>
main(void) {
int t1=0,t2=0,t3=0,j;
double arg[3][4] = {{1,2,1,4},{2,1,-1,-1},{-1,1,2,3}};
double temp[3],temp2[3];
if(arg[0][0]==0){
    if(arg[1][0]!=0){
    for (j=0;j<4;j++) {
    temp2[j]=arg[1][j];
    arg[1][j]=arg[0][j];
    arg[0][j]=temp2[j];
    }
    }
    if(arg[2][0]!=0){
    for (j=0;j<4;j++) {
    temp2[j]=arg[2][j];
    arg[2][j]=arg[0][j];
    arg[0][j]=temp2[j];
    }
    }
    else{
        t1=1;
    }
}
if(t1==0){
temp[0]=arg[0][0];
for (j=0;j<4;j++) {
arg[0][j]=arg[0][j]/temp[0];
}
for (j=3;j>=0;j--) {
    arg[1][j]=arg[1][j]-arg[1][0]*arg[0][j];
    arg[2][j]=arg[2][j]-arg[2][0]*arg[0][j];
}
}
if(arg[1][1]==0){
    if(arg[2][1]!=0){
    for (j=0;j<4;j++) {
    temp2[j]=arg[2][j];
    arg[2][j]=arg[1][j];
    arg[1][j]=temp2[j];
    }
    }
    else{
        t2=1;
    }
}
if(t2==0){
temp[1]=arg[1][1];
for (j=0;j<4;j++) {
arg[1][j]=arg[1][j]/temp[1];
}
for (j=3;j>=0;j--) {
    arg[0][j]=arg[0][j]-arg[0][1]*arg[1][j];
    arg[2][j]=arg[2][j]-arg[2][1]*arg[1][j];
}
}
if(arg[2][2]==0){
        t3=1;
   
}
if(t3==0){
temp[2]=arg[2][2];
for (j=0;j<4;j++) {
arg[2][j]=arg[2][j]/temp[2];
}
for (j=3;j>=0;j--) {
    arg[0][j]=arg[0][j]-arg[0][2]*arg[2][j];
    arg[1][j]=arg[1][j]-arg[1][2]*arg[2][j];
}
}
printf("the solution is x,y,z = ");
if(t1==1 || t2==1 || t3==1){
printf("not single solution");
}
else{
    for (j=0;j<3;j++) {
    printf("%.3lf ,",arg[j][3]);
}
}
printf("\n");
return(0);
}

獻花 x0
引用 | 編輯 jlookes
2007-12-03 03:11
7樓
  
大大你好 小弟最近因作業要求需要用到這樣的程式

但我試過

X+Y+Z=3
2X+2Y+2Z=6
3X+3Y+3Z=9

答案應該是 X=1 Y=1 Z=1
但答案顯示是 not single solution
想請教問題出在哪?

獻花 x0
引用 | 編輯 a258456
2011-03-20 23:02
8樓
  
這題是無解喔

獻花 x0