# Documentation

## Hilbert Matrices and Their Inverses

This example shows how to compute the inverse of a Hilbert matrix using Symbolic Math Toolbox™. Symbolic computations give accurate results for these ill-conditioned matrices, while purely numerical methods fail.

Create a 20-by-20 numeric Hilbert matrix.

```H = hilb(20); ```

Find the condition number of this matrix. Hilbert matrices are ill-conditioned, meaning that they have large condition numbers indicating that such matrices are nearly singular. Note that computing condition numbers is also prone to numeric errors.

```cond(H) ```
```ans = 1.5316e+18 ```

Therefore, inverting Hilbert matrices is numerically unstable. When you compute a matrix inverse, `H*inv(H)` must return an identity matrix or a matrix close to the identity matrix within some error margin.

First, compute the inverse of `H` by using the `inv` function. A warning is thrown due to the numerical instability.

```H*inv(H) ```
```Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.549910e-19. ans = Columns 1 through 7 1.0000 0.0000 -0.0002 -0.0024 -0.0312 0.0781 -0.3750 -0.0655 1.0000 0 -0.0020 -0.0391 0.0469 0 -0.0663 -0.0186 1.0001 -0.0020 -0.0039 0.0781 -0.0625 -0.0442 -0.1489 0.1479 0.9976 0.0117 -0.0156 0.7500 -0.0168 -0.3296 0.4384 -0.1108 0.9766 0.1250 0.2500 0.0094 -0.5118 0.7648 -0.2402 -0.0195 0.8906 0.3438 0.0321 -0.6741 1.0660 -0.3518 -0.0879 0.0312 0.9062 0.0510 -0.8102 1.3214 -0.4307 -0.1074 -0.3281 0.7188 0.0665 -0.9207 1.5263 -0.4705 -0.2461 0.0156 -0.2500 0.0790 -1.0082 1.6862 -0.4924 -0.2969 0.0078 0.3750 0.0889 -1.0763 1.8076 -0.4924 -0.3672 0.0469 -0.1562 0.0969 -1.1283 1.8975 -0.4800 -0.4238 -0.1172 0.0938 0.1031 -1.1672 1.9616 -0.4561 -0.4688 -0.0312 -0.2500 0.1080 -1.1954 2.0052 -0.4294 -0.5059 -0.0469 -0.0625 0.1117 -1.2149 2.0326 -0.3992 -0.5586 -0.0156 -0.3125 0.1145 -1.2275 2.0471 -0.3650 -0.6152 -0.0156 -0.3125 0.1166 -1.2344 2.0513 -0.3323 -0.6230 -0.0859 -0.1875 0.1180 -1.2367 2.0473 -0.2937 -0.6660 0.0469 -0.1562 0.1190 -1.2354 2.0370 -0.2600 -0.7031 0.1016 -0.5938 0.1195 -1.2312 2.0216 -0.2209 -0.7793 0.2812 -0.5781 Columns 8 through 14 -0.7500 1.4336 -0.6250 0.6309 0.5000 1.0000 -0.3438 -1.1250 3.1836 -0.7500 0.6387 -1.0000 0.6250 -0.4219 0.5625 1.6055 -0.6875 -0.3848 0.1250 -2.8750 0.6250 -0.7500 0.7637 -0.1875 0.2422 0.3750 -0.1250 -0.3906 -0.1875 0.7441 -0.8750 -0.0625 1.1250 -2.0000 -1.7188 -0.1250 0.7910 -1.5000 -0.4980 -0.8750 -1.6250 -0.4688 0.6250 0.7461 0.7500 -0.0723 0.5000 -1.6250 -0.6562 0.0625 1.5820 -0.6250 0.1641 -1.1250 2.0000 -1.0000 0.7812 0.0801 2.1875 -0.6699 1.7500 -4.5000 2.4922 0.8125 0.6875 -0.0625 0.0039 0.3750 -4.2500 -1.1953 0.3438 0.1074 1.0000 1.6348 -0.6250 -1.5000 -0.4766 -0.2188 0.2461 -0.5312 0.0469 0 -1.5000 -1.3672 0.5000 -0.2578 0.7500 0.2344 0.5000 -3.2500 -1.7578 0.5312 -0.0977 -0.6875 0.3066 -1.2500 -1.0000 -0.7500 1.1250 -0.4238 1.1562 -0.4531 0.2500 -4.1250 -0.6328 0.9375 -0.7305 0.9375 -0.7578 1.8750 -3.1250 -0.4062 0.6562 -0.0312 1.0000 0.5684 -0.1250 -1.3750 0.3125 -0.0625 0.4316 -0.5000 0.3516 0.7500 -0.5625 0.4922 1.0000 0.0352 -0.5312 0.1055 0.1250 0.4375 -0.0469 0.6562 -0.3203 0.0312 -0.2920 0.8750 0.8125 -0.5781 Columns 15 through 20 -0.8750 -0.5000 0 0 -1.2500 -0.1875 0.5000 -0.1250 -2.0000 1.5000 -1.7500 -0.3125 0.1250 -2.8750 -2.0000 -0.5000 0.7500 0.8125 -0.8750 0.5000 -3.0000 1.0000 3.5000 0 0.3125 -2.0000 -3.5000 -2.0000 1.5000 -0.4375 -0.6875 -3.5000 0.2500 0.5000 0 0.5625 0.9375 -2.7500 0 2.0000 1.5000 -0.2500 -1.4375 2.0000 -5.5000 2.7500 0.2500 0.5625 2.8750 -5.0000 3.0000 -1.2500 1.7500 0.1562 2.0625 -2.5625 2.2500 -1.7500 0.5000 -0.0625 0.8125 -2.4375 0.2500 2.0000 -0.6250 0 1.0625 -4.2500 -0.5000 -1.0000 0 -0.0625 1.3750 -4.6875 1.5000 -1.0000 2.6250 -0.4062 1.2500 -0.1250 0.2500 -2.0000 1.8750 -0.3438 2.0625 -2.0000 0.7500 -0.5000 0.2500 0 1.6562 -3.0000 2.7500 0 0.7500 0.0312 0.2812 -1.0000 0.2500 1.0000 -0.3750 0.1562 -0.1562 -1.2500 0.2500 2.7500 1.1250 -0.1250 0.0938 -0.8125 0 1.7500 1.0000 0.1875 1.1250 0.0625 1.5000 -1.5000 1.7500 0.6875 ```

Now, use the MATLAB® `invhilb` function that offers better accuracy for Hilbert matrices. This function finds exact inverses of Hilbert matrices up to 15-by-15. For a 20-by-20 Hilbert natrix, `invhilb` finds the approximation of the matrix inverse.

```H*invhilb(20) ```
```ans = 1.0e+10 * Columns 1 through 7 0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0003 0.0027 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0001 -0.0015 0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0003 -0.0005 0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0012 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0001 0.0007 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0002 0.0003 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0001 -0.0003 0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0003 0.0024 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0006 0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0001 0.0001 -0.0000 0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0001 -0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.0004 0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0008 0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0002 0.0008 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0004 0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0001 0.0012 0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0001 0.0002 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0011 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0001 -0.0005 -0.0000 0.0000 0.0000 0.0000 0.0000 -0.0001 -0.0004 Columns 8 through 14 -0.0113 -0.0526 -0.1057 0.3087 0.3288 -0.3020 -7.1001 0.0114 -0.0749 0.2441 -0.2819 3.6574 -4.3487 -1.2214 -0.0029 0.0180 -0.1460 -0.5184 1.0100 0.2483 -6.2948 0.0152 -0.0434 -0.1653 -0.2097 2.0099 -2.7850 -3.4360 0.0127 -0.1088 0.1946 -0.4312 1.4630 -2.5032 -2.9998 -0.0044 0.0231 0.0562 0 0.5838 -0.5302 -3.7447 0.0170 -0.0938 0.0721 -0.5335 3.4326 -5.1540 -1.0536 -0.0078 0.0091 -0.2278 0.5872 0.3792 0.7046 -4.3151 0.0113 -0.0708 0.1850 -0.7147 2.1643 -1.7851 -3.5970 0.0023 -0.0149 -0.0679 0.3087 0.1074 -0.1745 -4.3218 0.0064 -0.0428 -0.0382 -0.3406 1.1207 -2.1877 -0.7248 -0.0015 0.0029 -0.0155 -0.3976 0.1913 -1.6576 -1.9797 -0.0053 -0.0272 -0.0822 -0.4647 0.8892 0.1879 -1.2012 0.0001 -0.0568 -0.0960 -0.3590 0.5939 -1.6408 -1.7985 0.0010 -0.0072 0.0075 -0.0210 0.9664 -3.4762 -1.4294 0.0055 -0.0112 0.1124 0.2491 1.4496 -1.4831 -2.2213 -0.0014 -0.0200 -0.0348 -0.5830 0.3657 -1.6878 -2.9125 0.0002 -0.0081 0.0143 -0.1745 -0.2416 -1.7650 -3.3219 0.0006 -0.0358 0.0730 0.1300 -0.2416 -1.6475 -0.1544 0.0015 -0.0058 -0.0273 -0.0503 1.1593 -1.1442 -0.7583 Columns 15 through 20 1.5435 4.8989 -0.1242 -0.0319 0.1770 0.0029 -4.3218 7.4826 -2.6407 0.8623 -0.0776 0.0029 -4.8050 5.7982 -1.0939 0.3758 0.0495 -0.0033 0.9328 5.2613 -1.8220 0.6610 -0.0126 -0.0050 1.2885 6.0666 -1.9898 0.3238 -0.0839 0.0045 4.9325 2.3287 -1.3220 -0.1242 0.1323 -0.0138 0.5570 7.6974 -1.5234 0.2382 -0.1527 -0.0038 3.8319 -0.5369 0.1745 -0.0042 0.1141 -0.0141 -0.0201 4.1607 -1.3925 0.5637 -0.0497 0.0036 0.8925 3.9259 -0.7986 0.2533 0.0455 -0.0091 0.2819 3.9594 -1.3892 0.6677 0.0174 0.0055 1.2751 1.4093 -0.9865 0.4974 -0.0023 -0.0033 -0.1879 4.7513 -1.4663 0.2265 -0.0168 0.0013 0.5570 2.4159 -0.5771 0.4521 0.0346 -0.0028 -0.1812 2.0569 -1.8690 0.3523 0.0289 0.0052 -1.1006 4.4695 -1.1409 0.2374 -0.0023 -0.0069 -1.8254 3.8118 -1.5586 0.3121 0.0185 -0.0032 -1.0536 2.6978 -1.3908 0.3397 -0.0524 -0.0014 0 1.0570 -1.0888 0.3003 -0.0088 -0.0015 -2.6642 2.3354 -1.7381 0.3448 -0.0078 0.0014 ```

To avoid round-off errors, use exact symbolic computations. For this, create the symbolic Hilbert matrix.

```Hsym = sym(H) ```
``` Hsym = [ 1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20] [ 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21] [ 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22] [ 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23] [ 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24] [ 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25] [ 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26] [ 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27] [ 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28] [ 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29] [ 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30] [ 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30, 1/31] [ 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30, 1/31, 1/32] [ 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30, 1/31, 1/32, 1/33] [ 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30, 1/31, 1/32, 1/33, 1/34] [ 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30, 1/31, 1/32, 1/33, 1/34, 1/35] [ 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30, 1/31, 1/32, 1/33, 1/34, 1/35, 1/36] [ 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30, 1/31, 1/32, 1/33, 1/34, 1/35, 1/36, 1/37] [ 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30, 1/31, 1/32, 1/33, 1/34, 1/35, 1/36, 1/37, 1/38] [ 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29, 1/30, 1/31, 1/32, 1/33, 1/34, 1/35, 1/36, 1/37, 1/38, 1/39] ```

Get the value of the condition number. It has been derived by symbolic methods and is free of numerical errors.

```vpa(cond(Hsym)) ```
``` ans = 24521565858153031724608315432.509 ```

Although its condition number is large, you can compute the exact inverse of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