I'm doing a project where we are given the size of a Hilbert matrix, and we are to use Hx=b and Gaussian elimination to find b, and to recalculate our exact solution for x with what we had calculated for b. Part of the project is to write a code that keeps track of the multiplication/division operation counts of the program, but I can't seem to figure out how to go about this. The result is supposed to be similar to what the solution would be for when using the equation from my textbook.
Input code:
#Hilbert matrix of size n=11, defined by Hx=b`
#Creating the matrix
n=11
matrix1 = matrix(nrow=n,ncol=n)
for (r in 1:n) {
for (c in 1:n)
matrix1[r,c] = 1/(r+c-1)
}
library(MASS)
H<-fractions(matrix1)
colnames(H)=c("1","2","3","4","5","6","7","8","9","10","11")
x<-c(1,1,1,1,1,1,1,1,1,1,1)
H[,c(1,2,3,4,5,6,7,8,9,10,11)]
b<-rowSums(H*x) #Answer for b
exactsolution<-round((solve(H,b)),digits=4) #Vector solution
Output:
> H
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,] 1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9 1/10 1/11
[2,] 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9 1/10 1/11 1/12
[3,] 1/3 1/4 1/5 1/6 1/7 1/8 1/9 1/10 1/11 1/12 1/13
[4,] 1/4 1/5 1/6 1/7 1/8 1/9 1/10 1/11 1/12 1/13 1/14
[5,] 1/5 1/6 1/7 1/8 1/9 1/10 1/11 1/12 1/13 1/14 1/15
[6,] 1/6 1/7 1/8 1/9 1/10 1/11 1/12 1/13 1/14 1/15 1/16
[7,] 1/7 1/8 1/9 1/10 1/11 1/12 1/13 1/14 1/15 1/16 1/17
[8,] 1/8 1/9 1/10 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18
[9,] 1/9 1/10 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19
[10,] 1/10 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19 1/20
[11,] 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19 1/20 1/21
> b
[1] 3.0198773 2.1032107 1.6801338 1.4182290 1.2348957 1.0973957 0.9895525 0.9022509 0.8298825 0.7687714 0.7163905
> exactsolution
1 2 3 4 5 6 7 8 9 10 11
1.0000 1.0000 1.0000 1.0002 0.9989 1.0040 0.9914 1.0118 0.9902 1.0045 0.9991
Textbook Equation:
[Textbook Equation](https://i.stack.imgur.com/bSi7G.png)