Current location - Training Enrollment Network - Mathematics courses - How to create Hilbert matrix with matlab
How to create Hilbert matrix with matlab
Hilbert matrix is a kind of mathematical transformation matrix, which is positive definite and highly ill-conditioned (that is, if any element changes slightly, the value and inverse matrix of the whole matrix will change greatly), and the ill-conditioned degree is related to the order. In linear algebra, Hilbert matrix is a square matrix whose coefficients are all unit fractions.

Methods/steps

Mathematical form

Hilbert matrix is a famous "bad condition" matrix. The mathematical expression of the elements of this matrix is a(i, j)= 1/(i+j- 1). Let's show the mathematical representation of the fifth-order Hilbert matrix.

Generating Hilbert matrix with for loop

Next, we will use the for loop to generate Hilbert matrix according to mathematical expressions. The specific operation code and results are shown in the figure below. In order to compare with the Hilbert matrix in the mathematical representation in the first step, the fifth order is also selected for this calculation.

The pre-configuration of matrix space can improve the running speed.

In the operation of matrix, the pre-configuration of matrix space can improve the running speed, especially the operation speed of high-order matrix. We can verify it by timing functions tic and toc. Tic means timing start, toc means timing end. Graph 1 shows the time spent without matrix space pre-configuration, which is 3.2464 seconds; Fig. 2 shows the calculation time of matrix space pre-configuration, which is 0.072233. It can be clearly seen that the running speed has improved.

Generating Hilbert matrix by vectorization programming

When we use vector programming to generate Hilbert matrix, it can greatly improve the running speed, so we usually use vector programming as much as possible, but we must have a high understanding of matlab. As shown in the figure, the time taken is 0.03 16 16 seconds, which is shorter than the first two.

Calculation with matlab's own function eig

Here we use the function Hilbert (n) generated by matlab to calculate. The time used is 0.003 173 seconds. It can be seen that the time used is the shortest, so when programming, we should try to use the existing corresponding function of matlab, and if we can't find it, we should change it ourselves. This can save calculation time.

Inverse of Hilbert matrix

In addition, matlab has its own function invhilb(n) to find the inverse of Hilbert matrix, and the function is to find the inverse of n-order Hilbert matrix. Let's take a look. The specific code and results are shown below. As can be seen from the figure, the time spent is still relatively short.

end

Matters needing attention

The calculation time in this experience is influenced by computer configuration, matlab version, whether the program runs for the first time and other factors, and the results will change.