- Thread starter
- #1

- Apr 13, 2013

- 3,836

Does this mean that I have done something wrong??

- Thread starter evinda
- Start date

- Thread starter
- #1

- Apr 13, 2013

- 3,836

Does this mean that I have done something wrong??

- Admin
- #2

- Jan 26, 2012

- 4,198

- Thread starter
- #3

- Apr 13, 2013

- 3,836

That is my code:

Code:

```
it=1;
x2=-inv(F)*W*x1+inv(F)*b
while ((it<=maxit)&&(norm(x2-x1)<e)&&(norm(b-A*x2))<e))
error=norm(x-D);
x1=x2;
x2=-inv(F)*W*x1+inv(F)*b
it=it+1;
end
```

- Admin
- #4

- Jan 26, 2012

- 4,198

- Thread starter
- #5

- Apr 13, 2013

- 3,836

Code:

```
it=1;
x2=-inv(F)*W*x1+inv(F)*b
while ((it<=maxit)&&(norm(x2-x1)<e)&&(norm(b-A*x2))<e))
error=norm(x-D);
x1=x2;
x2=-inv(F)*W*x1+inv(F)*b
it=it+1;
end
```

W:the Hilbert matrix - F

b:Ax=

it:number of iteration

x2:approximation of solution

x1: previous approximation of solution

maxit:maximum number of iterations

e:a small number given from the user and

error:the error we are looking for

- Admin
- #6

- Jan 26, 2012

- 4,198

1. You could probably speed this code up just a bit by setting Finv = inv(F), and referencing Finv instead of inv(F). As you are not changing F in the loop, it would be faster simply to recall the contents of memory somewhere than to evaluate the inverse of F multiple times in the loop.

2. Refering to your OP, is n the number of iterations? If so, could you please give me a plot of several errors on the y axis versus the corresponding number of iterations on the x axis? I'm wondering if you might not be getting an

3. There might be a logical error in your program: you have the line

error=norm(x-D);

Should you be saying, instead, that

error=norm(x1-D)? Or error=norm(x2-D)? (Probably the latter, due to first-time-through issues.)

I mean, does the computer know what x is? It looks to me like

4. The while statement has unbalanced parentheses. Try enclosing the last test in its own parentheses.

See if one of these things, especially 2 or 3, doesn't help.

- Thread starter
- #7

- Apr 13, 2013

- 3,836

1. You could probably speed this code up just a bit by setting Finv = inv(F), and referencing Finv instead of inv(F). As you are not changing F in the loop, it would be faster simply to recall the contents of memory somewhere than to evaluate the inverse of F multiple times in the loop.

2. Refering to your OP, is n the number of iterations? If so, could you please give me a plot of several errors on the y axis versus the corresponding number of iterations on the x axis? I'm wondering if you might not be getting anunderflow. That is, 250 iterations of the Jacobi method might make your error so small that you run up against the machine epsilon, and the computer cannot represent an error that small.

3. There might be a logical error in your program: you have the line

error=norm(x-D);

Should you be saying, instead, that

error=norm(x1-D)? Or error=norm(x2-D)? (Probably the latter, due to first-time-through issues.)

I mean, does the computer know what x is? It looks to me likeyouonly know what x1 and x2 are (x isn't in your list). That indicates to me that that it's probably a typo in your code.

4. The while statement has unbalanced parentheses. Try enclosing the last test in its own parentheses.

See if one of these things, especially 2 or 3, doesn't help.

1)Ok..And...could I also write it like that: x2=-F\W*x1+F\b ??

2)I wrote this code in a function and with n I mean the maxit I give each time when I call the function...The underflow,has it to do with the Hilbert matrix??Because,when I ran my code,using other matrices,I didn't get Nan as result...

3)Oh sorry,I accidentally wrote it like that. At the code I ran ,I wrote it like that: error=norm(x2-D) ..

- Admin
- #8

- Jan 26, 2012

- 4,198

Code:

```
it=1;
Finv=inv(F);
x2=-Finv*W*x1+Finv*b
while ((it<=maxit)&&(norm(x2-x1)<e)&&(norm(b-A*x2)<e))
error=norm(x2-D);
x1=x2;
x2=-Finv*W*x1+Finv*b
it=it+1;
end
```

- Thread starter
- #9

- Apr 13, 2013

- 3,836

I wrote it like that right now..but I get the same result again(NaN)..Have I done maybe an other error??

Code:`it=1; Finv=inv(F); x2=-Finv*W*x1+Finv*b while ((it<=maxit)&&(norm(x2-x1)<e)&&(norm(b-A*x2)<e)) error=norm(x2-D); x1=x2; x2=-Finv*W*x1+Finv*b it=it+1; end`

- Admin
- #10

- Jan 26, 2012

- 4,198

- Thread starter
- #11

- Apr 13, 2013

- 3,836

I checked if,giving the Hilbert matrix with dimension 250,maxit=250,my code enters the loop,and it does...it makes 250 iterations...Have you an idea what I could have done wrong??

- Thread starter
- #12

- Apr 13, 2013

- 3,836

Also,the method does not converge...Has it maybe to do something with that??

- Thread starter
- #13

- Apr 13, 2013

- 3,836

Why does this happen???

- Thread starter
- #14

- Apr 13, 2013

- 3,836

- Admin
- #15

- Jan 26, 2012

- 4,198

Having more precision of digits isn't necessarily going to help with the determinant being zero.

So... if the determinant is zero, then you most likely have infinite solutions with that particular matrix. Are you sure you are using the correct matrix? Can you double-check all the entries (I know there are a lot of them!)? If you're going to solve $Ax=b$, then having the right $A$ is definitely of the essence.

- Thread starter
- #16

- Apr 13, 2013

- 3,836

How could I check this??? I use the command hilb(n)..Having more precision of digits isn't necessarily going to help with the determinant being zero.

So... if the determinant is zero, then you most likely have infinite solutions with that particular matrix. Are you sure you are using the correct matrix? Can you double-check all the entries (I know there are a lot of them!)? If you're going to solve $Ax=b$, then having the right $A$ is definitely of the essence.

- Admin
- #17

- Jan 26, 2012

- 4,198

- Thread starter
- #18

- Apr 13, 2013

- 3,836

Where could I find such a command??

- Thread starter
- #19

- Apr 13, 2013

- 3,836

- Moderator
- #20

- Feb 7, 2012

- 2,785

The determinant of the Hilbert matrix is not zero, but it tends to zero exponentially fast as the size of the matrix increases. I know nothing about matlab, but I doubt whether it could possibly have enough precision to distinguish $\det H$ from zero for a $250\times 250$ Hilbert matrix. As is mentioned here, the Hilbert matrix is notoriously ill-conditioned for numerical computation.

- Thread starter
- #21

- Apr 13, 2013

- 3,836

I wrote the command det(H) and the result is that it equals to zeroThe determinant of the Hilbert matrix is not zero, but it tends to zero exponentially fast as the size of the matrix increases. I know nothing about matlab, but I doubt whether it could possibly have enough precision to distinguish $\det H$ from zero for a $250\times 250$ Hilbert matrix. As is mentioned here, the Hilbert matrix is notoriously ill-conditioned for numerical computation.

- Thread starter
- #22

- Apr 13, 2013

- 3,836

Hilbert matrices are notoriously ill-conditioned: the computation of the determinant is subject to severe cancellation effects. The following results, both with HardwareFloats as well as with SoftwareFloats, are marred by numerical roundoff:

A := linalg::hilbert(15):

float(numeric::det(A, Symbolic)),

numeric::det(A, HardwareFloats),

numeric::det(A, SoftwareFloats)

How could I apply this,to check the result??Because I get an error message,when I write it...

- Thread starter
- #23

- Apr 13, 2013

- 3,836

Hasn't it maybe to do with the determinant,but with the condition number of the matrix?The determinant of the Hilbert matrix is not zero, but it tends to zero exponentially fast as the size of the matrix increases. I know nothing about matlab, but I doubt whether it could possibly have enough precision to distinguish $\det H$ from zero for a $250\times 250$ Hilbert matrix. As is mentioned here, the Hilbert matrix is notoriously ill-conditioned for numerical computation.

- Thread starter
- #24

- Apr 13, 2013

- 3,836

- Moderator
- #25

- Feb 7, 2012

- 2,785

The Hilbert matrices are positive, so the spectral radius is equal to the norm, which is $\leqslant \pi$. Asymptotically, as the size of the matrix increases, the norm converges to $\pi.$ See Man-Duen Choi's paper "Tricks or Treats with the Hilbert Matrix".Anyway... I also calculated the spectral radius of the hilbert matrix with dimension 250. Is this right that it is 217.3320,using the Jacobi method?