PDA

View Full Version : Problem with declaring variables



amdary
06-27-2013, 03:22 PM
I've been using vba for excel for a few years but I'm self taught so I have some problems with basic programming rules like declaring variable, which I suppose is the problem at hand.
On to my situation I'm running a loop for x from 0 to 3*l step l/5 however it does not finishes for some values of l. For example if l =13 then 3*l=39 and the step is 13/5=2.6 and the loop must run until x=39 however it ends when x=36.4. I've included a message box with the value of xi=x-(l+l) and when x=l+l the result of xi is 3.56e-15 which is obviously my problem since it's supposed to be 0. I'm guessing there's some kind of residual value and when x=39 it's not actually 39 but 39 + 3.56e-15.
Here's my code. It also won't work for l=1,2,16,113, etc.
Thanks in Advance.



Private Sub Calcular_Click()
Dim MR1(4, 4), MR2(4, 4), pt(8), MR3(4,4), MRG(8, 8), desp(8), despord(8), MO1(8, 8), MO2(8, 8), Reax(8), SP(8) As Double
Dim EI, el, ine, Reaxord(8), Xu(), K11(), Du() As Double
Dim inter, x, xi, l, li, l1, l2, l3, P As Double
Dim rct, rc As Double


l = Range("G15").Value
el = Range("C15")
ine = Range("E15")
P = 1
EI = el * ine
l1 = 1 / l
l3 = 1 / l ^ 3
l2 = 1 / l ^ 2
inter = l / 5

Set H1 = Worksheets("LI")

'Creacion de Matriz de Rigidez de cada elemento

MR1(1, 1) = 12 * EI * l3
MR1(1, 2) = 6 * EI * l2
MR1(1, 3) = -12 * EI * l3
MR1(1, 4) = 6 * EI * l2

MR1(2, 1) = 6 * EI * l2
MR1(2, 2) = 4 * EI * l1
MR1(2, 3) = -6 * EI * l2
MR1(2, 4) = 2 * EI * l1

MR1(3, 1) = -12 * EI * l3
MR1(3, 2) = -6 * EI * l2
MR1(3, 3) = 12 * EI * l3
MR1(3, 4) = -6 * EI * l2

MR1(4, 1) = 6 * EI * l2
MR1(4, 2) = 2 * EI * l1
MR1(4, 3) = -6 * EI * l2
MR1(4, 4) = 4 * EI * l1

'Creacion de Matriz De Rigidez De Elementos
For i = 1 To 4
For j = 1 To 4
MR2(i, j) = MR1(i, j)
MR3(i, j) = MR1(i, j)
Next j
Next i


'LLenado de valores 0 en Matriz de Rigidez Global
For i = 1 To 8
For j = 1 To 8
MRG(i, j) = 0
Next j
Next i


'Ensamblaje de Matriz de Rigidez Global
For k = 0 To 5 Step 2
For i = 1 To 4
For m = 1 To 4
MRG(k + i, k + m) = MR1(i, m) + MRG(k + i, k + m)
Next m
Next i

Next k

'Vector de Desplazamientos
cont = 0
For i = 1 To 4
desp(i + cont) = 0
desp(i + 1 + cont) = "ROT" & i
cont = cont + 1
Next i

'Vector de Reacciones
cont = 0
For i = 1 To 4
Reax(i + cont) = "R" & i
Reax(i + 1 + cont) = 0
cont = cont + 1

Next i


lt = 3 * l
inter = l / 5


For x = 0 To lt Step inter

acm = 0
acmz = 0

If 0 <= x And x <= l Then
xi = x

pt(1) = (P * (l - xi) ^ 2 * (l + 2 * xi)) / l ^ 3
pt(2) = (P * xi * (l - xi) ^ 2) / l ^ 2
pt(3) = (P * (xi ^ 2) * (3 * l - 2 * xi) / l ^ 3)
pt(4) = -(P * (xi ^ 2) * (l - xi) / l ^ 2)
For i = 5 To 8
pt(i) = 0
Next i

ElseIf l < x And x <= l + l Then
xi = x - l
pt(3) = (P * (l - xi) ^ 2 * (l + 2 * xi)) / l ^ 3
pt(4) = (P * xi * (l - xi) ^ 2) / l ^ 2
pt(5) = (P * (xi ^ 2) * (3 * l - 2 * xi) / l ^ 3)
pt(6) = -(P * (xi ^ 2) * (l - xi) / l ^ 2)
For i = 1 To 2
pt(i) = 0
Next i
For i = 7 To 8
pt(i) = 0
Next i

ElseIf l + l < x And x <= l + l + l Then
xi = x - (l + l)
MsgBox xi

pt(5) = (P * (l - xi) ^ 2 * (l + 2 * xi)) / l ^ 3
pt(6) = (P * xi * (l - xi) ^ 2) / l ^ 2
pt(7) = (P * (xi ^ 2) * (3 * l - 2 * xi) / l ^ 3)
pt(8) = -(P * (xi ^ 2) * (l - xi) / l ^ 2)
For i = 1 To 4
pt(i) = 0
Next i

End If


'Ordenamiento Vector De Desplazamientos
For i = 1 To 8
If IsNumeric(desp(i)) = False Then
acm = acm + 1
despord(acm) = desp(i)
For j = 1 To 8
MO1(j, acm) = MRG(j, i)
Next j
End If

Next i


For i = 1 To 8
If IsNumeric(desp(i)) = True Then
acmz = acmz + 1
despord(acm + acmz) = desp(i)
For j = 1 To 8
MO1(j, acm + acmz) = MRG(j, i)
Next j
End If
Next i



acm = 0
acmz = 0


'Ordenamiento Vector De Reacciones Y Vector De Solucion Particular
For i = 1 To 8
If IsNumeric(Reax(i)) = True Then
acm = acm + 1
SP(acm) = pt(i)
Reaxord(acm) = Reax(i)
For j = 1 To 8
MO2(acm, j) = MO1(i, j)
Next j
End If

Next i

For i = 1 To 8
If IsNumeric(Reax(i)) = False Then
acmz = acmz + 1
SP(acm + acmz) = pt(i)
Reaxord(acm + acmz) = Reax(i)

For j = 1 To 8
MO2(acm + acmz, j) = MO1(i, j)
Next j
End If

Next i


ReDim K11(acm, acm)
ReDim Du(acm)
ReDim Xu(acm)


'Matriz Reducida Para Eliminacion de Gauss
For i = 1 To acm
For j = 1 To acm
K11(i, j) = MO2(i, j)

Next j
Du(i) = -1 * SP(i)
Next i


'Eliminacion de Gauss
For m = 1 To (acm - 1)


For i = (m + 1) To acm
For j = (m + 1) To acm
K11(i, j) = K11(i, j) - (K11(i, m) / K11(m, m)) * K11(m, j)
Next j

Du(i) = Du(i) - (K11(i, m) / K11(m, m)) * Du(m)

Next i
Next m


Xu(acm) = Du(acm) / K11(acm, acm)

'Sustitucion Hacia Atras




k = acm - 1
For m = k To 1 Step -1
xm1 = 0
xm2 = 0
xm = 0
xmt = 0
xm1 = Du(m) / K11(m, m)

q = m + 1
For j = q To acm
xm2 = K11(m, j) * Xu(j) / K11(m, m)
xmt = xmt + xm2
Next j
xm = xm1 - xmt
Xu(m) = xm
Next m


For i = 1 To acmz
rct = 0
rc = 0
For j = 1 To acm
rc = MO2(acm + i, j) * Xu(j)
rct = rct + rc
Next j

rct = rct + SP(acm + i)
H1.Cells(21 + xco, 2 + i) = rct
Next i


xco = xco + 1



Next x

End Sub

p45cal
06-27-2013, 03:58 PM
I think you're coming up against floating decimal point limitations.
If you step through the loop until x = 36.4, them in the immediate pane type in:
? x-36.4
you get a small number:
7.105427357601E-15
which means that the loop control variable x is a mite big, which means that when you add inter to it it takes it over the 39 so the loop exits.
Solve it by adding a small portion of inter to the upper limit of the loop. I've used 1000th of inter below, but you only need to make it bigger by say a third of the step size inter.:
For x = 0 To lt + inter / 1000 Step inter