[問題] 高斯消去法

作者: won54chan (王54丞)   2014-10-29 09:21:55
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
C++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
與正解不符
餵入的資料(Input):
預期的正確結果(Expected Output):
x=[1 -1 2]
錯誤結果(Wrong Output):
x=[2.5 2.5 1]
程式碼(Code):(請善用置底文網頁, 記得排版)
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<stdio.h>
#include<math.h>
using namespace std;
double A[3][4];
double x[3];
void gaussh(double matrix[3][4]);
int main(void)
{
double A[3][4]={{-1,1,2,2},{3,-1,1,6},{-1,3,4,4}};
gaussh(A);
for (int i=0; i<3; ++i)
cout<<x[i]<<"\n";
system("pause");
return 0;
}
void gaussh(double matrix[3][4])
{
for (int i=0; i<3; ++i)
{
if (matrix[i][i]==0)
for (int j=i+1;j<4;j++)
{
if(matrix[j][i]!=0)
for(int m=i;m<3;m++)
swap(matrix[i][m],matrix[j][m]);
}
double t;
for (int k=i; k<3; ++k)
{
t=matrix[k+1][k]/matrix[k][k] ;
for (int j=i+1; j<4; ++j)
{
matrix[j][k] -= matrix[i][k] * t;
}
}
}
x[3]=matrix[3][4]/matrix[3][3];
for (int i=2; i>=0;
作者: dirkc (3781615)   2014-10-29 11:41:00
一個小建議,好好排版,你的睡眠時間或許可以因此多一些 :)
作者: won54chan (王54丞)   2014-10-29 13:44:00
已解決 謝榭
作者: uorol (′‧ω‧‵)   2014-10-29 14:00:00
這排版太慘烈了...

Links booklink

Contact Us: admin [ a t ] ucptt.com