 # Thread: [RESOLVED] NaN ... how come?

1. ## [RESOLVED] NaN ... how come?

I am using the Gauss Seidel Method to solve the momentum equations as part of a colocated grid cfd code (but that's not what my question is about). The following code resolves x(i) as NaN. Maybe I've just been staring at this so long that I just can't see it. Could someone with fresh eyes tell what's wrong?

Code:
```    Public Sub GaussSeidel(A As Double(,), B As Double())

Dim y(2), x(2) As Double
Dim i, j, iterGS, niterGS As Integer

niterGS = 6

A(0, 1) = 16.0
A(0, 2) = 3.0
A(1, 1) = 7.0
A(1, 2) = -11.0

B(0) = 11.0
B(1) = 13.0

' Results expected B(0) = .8122 and B(1) = -.6650

'Initial values of x
For i = 0 To 1
x(i) = 0.1
Next

For iterGS = 1 To niterGS
For i = 0 To 1
y(i) = (B(i) / A(i, i))
For j = 0 To 1
If j <> i Then
y(i) = y(i) - ((A(i, j) / A(i, i) * x(j)))
x(i) = y(i)
End If

Console.Write(" iter = " & iterGS)
Console.WriteLine()
Console.Write(" i = " & i)
Console.Write(" j = " & j)
Console.Write(" x(i) =" & x(i))
Console.WriteLine()

Next
Next
Next

End Sub```  Reply With Quote

2. ## Re: NaN ... how come?

The value of x(i) is based on the value of y(i), which is initially set like this:
Code:
```            For i = 0 To 1
y(i) = (B(i) / A(i, i))```
...so the value will either be 13.0/7.0 (at 1) or 11.0/unpredictable (at 0) because A(0,0) isn't set in your routine (but might be in the caller).

I don't know about Gauss Seidel etc, but I suspect that you may have wanted to set the values of other elements of A (starting at 0,0).  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured