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

Hi guys,
I am new to programming. Can you please help me with this.

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
Clarify your question.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <array>
#include <vector>
#include <iomanip>

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<T,N>, N > make_tri_diagonal_matrix()
{
    static_assert( N>0, "must be of non-zero size" ) ;

    std::array< std::array<T,N>, 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<T> > make_tri_diagonal_matrix( std::size_t n )
{
    if( n == 0 ) return {} ;

    std::vector< std::vector<T> > mtx( n, std::vector<T>(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<double,6>() ;
    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<int>(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.