### How to automatically generate and store a Matrix with same diagonal elements

Hi guys,

Okay, so I need to generate a matrix like this:

A•x=b

-2 1 0 0 0 (x1) h^2
1 -2 1 0 0 (x2) h^2
0 1 -2 1 0 (x3) h^2
0 .. .. ... ...
0 0 1 -2 1 (x_n-1) h^2
0 0 0 1 -2 (x_n) h^2

Where h= 1/(n+1)

Question: can you please give me an idea how to generate a matrix in this for for every given n value
so you initialize the matrix to the constant parts, and fill in the last column in a for loop with, roughly:

for (all the rows)
h = 1/(index +1??); //is index correct here? What is n, 1,2,3,...??
matrix[ last column] = x[index]*h*h; //it may be possible that index is the loop counter, if you write it that way

Last edited on

You have written down a matrix (A) AND a vector of unknowns (X) AND a vector right-hand side (b).

The inclusion of a vector of unknowns is nonsense in any programming language. Do your require the matrix alone (A) or the "augmented matrix" (A|b)?

If you want to solve this system use a tri-diagonal matrix solver (Thomas algorithm).
Last edited on
I am trying to generate a matrix where in the the tridiagonal matrix has the same value?

the main diagonal matrix = -2
and the upper and lower diagonal = 1
then the rest of the values are 0.
the equation is equal to h^2

this matrix will be used to solve by Gauss Elimination
Something like this, perhaps:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include #include #include #include template < typename SQUARE_MATRIX > void set_tri_diagonals( SQUARE_MATRIX& mtx ) { if( mtx.empty() || mtx[0].empty() ) return ; using T = decltype(+mtx[0][0]) ; mtx[0][0] = T(-2) ; // top left for( std::size_t i = 1 ; i < mtx.size() ; ++i ) { mtx[i][i] = T(-2) ; // diagonal mtx[i-1][i] = mtx[i][i-1] = T(1) ; // upper, lower diagonals } } template < typename T, std::size_t N > // N known at compile time std::array< std::array, N > make_tri_diagonal_matrix() { static_assert( N>0, "must be of non-zero size" ) ; std::array< std::array, N > mtx {} ; // initialise to all T{} (zeroes) set_tri_diagonals(mtx) ; // set the three diagonal values return mtx ; } template < typename T > // N not known at compile time std::vector< std::vector > make_tri_diagonal_matrix( std::size_t n ) { if( n == 0 ) return {} ; std::vector< std::vector > mtx( n, std::vector(n) ) ; // initialise to all T{} (zeroes) set_tri_diagonals(mtx) ; // set the three diagonal values return mtx ; } template < typename SQUARE_MATRIX > std::ostream& print( const SQUARE_MATRIX& mtx, int width = 6, std::ostream& stm = std::cout ) { for( const auto& row : mtx ) { for( const auto& value : row ) stm << std::setw(width) << value << ' ' ; stm << '\n' ; } return stm ; } int main() { std::cout << std::fixed << std::setprecision(2) ; // size known at compile time auto mtx1 = make_tri_diagonal_matrix() ; print(mtx1) ; // size not known at compile time std::size_t n ; std::cout << "\nsize? " ; std::cin >> n ; std::cout << '\n' ; auto mtx2 = make_tri_diagonal_matrix(n) ; print( mtx2, 2 ) ; // width (for printing each element) == 2 }``````

http://coliru.stacked-crooked.com/a/d04a5f616e259214
Last edited on
Topic archived. No new replies allowed.