A=({3 -1 -1},{-1 3 -1},{-1 -1 3})の3×3の行列とb=({1},{1},{1})のベクトルに対して前進消去を行い、後退代入によってAx=bの解を計算したいです。
//交代代入のアルゴリズム
//前進消去のアルゴリズム
のところ(【】の部分)を自分で書きましたがエラーが多すぎて何を直せばよいのかわかりません。その他のところは講義で公開されたものです。
また、講義でLinearAlgebra.cpp,LinearAlgebra.hppファイルを追加するとなっていたのですが、どういうことでしょうか。こちらもわかりましたら教えて頂きたいです。恐らくそれがないとprintMatrixなどが動かないのだろうとは予測しているのですが…
以下はコードです。
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
//後退代入
vector<double> Backward_sub(vector<vector<double>> A, vector<double> b){
//Backward substiction Ax = b
int N = int(b.size());
vector<double> x<N>;
【//後退代入のアルゴリズム
for(int k = N; k > 0; k--){
double sigma = 0;
for(int j = k+1; j <= N; j++){
sigma += a[k][j]*x[j];
}
x = (b[k] - sigma) / a[k][k];
}】
return x;
}
//Gauss消去法
vector<double> GaussianElimination(vector<vector<double>> a, vector<double> c){
unsigned long N = c.size();
vector<Vector<double>> A(N,vector<double>(N));
vector<double> b(N);
A = a; b = c;
//前進消去
int i = 0;
int j = 0;
int k = 1; double alpha = 0;
【//前進消去のアルゴリズム
for(k = 1; k < N; k++){
for(i = k+1; i < N; i++){
alpha = a[i][k] / a[k][k];
for(j = k+1; j = N; j++){
a = a[i][j] - alphaa[k][j];
c = c[i] - alphac[k];
}
}
}】
cout<<"前進消去\n";
cout<<"A = "<<endl;
printMatrix(A);
cout<<"b = "<<endl;
printVector(b);
//後退代入
cout<<"後退代入\n";
vector<double> x(N);
x = Backward_sub(A, b);
return x;
}
int main(int argc, const char * argv[]){
vector<vector<double>> A(3, vector<double>(3));
A = {{3,-1,-1},{-1,3,-1},{-1,-1,3}};
vector<double> b(3);
b = {1,1,1};
cout<<"A = "<<endl;
printMatrix(A);
cout<<"b = "<<endl;
printVector(b);
vector<double> x(3);
x = GaussianElimination(A, b);
cout<<"x = "<<endl;
printVector(x);
return 0;
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/16 03:50
2019/06/16 05:13