Question: C + + Code Diagonal dominance: i : aii > j = i | aij | Solve the linear system by both methods above. How

C++ Code
Diagonal dominance:
i : aii >
j=i
|aij|
Solve the linear system by both methods above. How accurate
is the solution?
Experimment with different matrix sizes and amounts of
#include
using std::cout,std::cin;
#include
#include
using std::string;
#include
using std::ranges::views::iota,
std::ranges::views::filter,
std::ranges::views::transform;
#include
using std::accumulate;
#include
using namespace Eigen;
// ::Matrix, Eigen::Vector;
/*
* Jacobi Iterative method with seq
*/
template< typename MatrixType,typename VectorType >
void JacobiSolve1( MatrixType A,VectorType sol,VectorType rhs ){
auto tmp{sol};
// set initial guess to identically one
int siz = A.rows();
for ( auto& v : sol )
v =1.;
// for a number of iteration
for ( auto it : iota(0,10)){
// one step of Jacobi
cout <<"it: "<< it <<'
'<< sol <<'
';
}
}
/*
* Jacobi Iterative method with ranges
*/
template< typename MatrixType,typename VectorType >
void JacobiSolve2( MatrixType A,VectorType sol,VectorType rhs ){
auto tmp{sol};
// set initial guess to identically one
int siz = A.rows();
for ( auto& v : sol )
v =1.;
// for a number of iteration
for ( auto it : iota(0,10)){
// one step of Jacobi
cout <<"it: "<< it <<'
'<< sol <<'
';
}
}
int main()
{
const int siz=5;
Matrix A;
Vector sol,rhs,tmp;
cout << "Matrix size: "<< A.rows()<<","<< A.cols()<<"("<< A.size()<<")
";
for ( auto& v : rhs )
v =1.;
cout << rhs <<'
';
for ( auto row : iota(0,siz)){
sol(row)= static_cast( row );
A(row,row)= siz;
for ( auto col : iota(0,siz)){
if (row==col) continue;
A(row,col)=-1;
}
}
cout << "sol:
"<< sol <<'
';
rhs = A*sol;
cout <<"rhs:
"<< rhs <<'
';
cout <<"================ Solve 1
";
JacobiSolve1( A,sol,rhs );
cout <<"================ Solve 2
";
JacobiSolve2( A,sol,rhs );
Matrix Af(20,20);
// or: MatrixXf Af(20,20);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!