
De VB.net a VBA
Publicado por Ramon (9 intervenciones) el 06/12/2017 15:53:51
Buenas tardes a tod@s;
Me han pasado una aplicación para resolver sistemas de ecuaciones por el método de solución a traves de iteraciones: Método Jacovi.
Este soft está escrito en VB.net y preciso utilizarlo en VBA para poder ampliarlo y adaptarlo a mis necesidades. Sobre el original:
(https://www.experts-exchange.com/questions/27222713/jacobi-method-in-vb.html)
he hecho todos los cambios posibles que me permite mi conocimiento pero me quedan instrucciones de este lenguaje, que desconozco.
Alguien me puede ayudar ?.
Con gracias anticipadas, aprovecho la ocasión para desearos a tod@s unas:
Muy FELICES FIESTAS DE NAVIDAD Y AÑO NUEVO 2018 !!!!!!!!!!!!!!!!!!!!!!!!!!!
P.D.:
Os pongo el código porque no me deja adjuntarlo ni como .xlsm ni como .pdf
Me han pasado una aplicación para resolver sistemas de ecuaciones por el método de solución a traves de iteraciones: Método Jacovi.
Este soft está escrito en VB.net y preciso utilizarlo en VBA para poder ampliarlo y adaptarlo a mis necesidades. Sobre el original:
(https://www.experts-exchange.com/questions/27222713/jacobi-method-in-vb.html)
he hecho todos los cambios posibles que me permite mi conocimiento pero me quedan instrucciones de este lenguaje, que desconozco.
Alguien me puede ayudar ?.
Con gracias anticipadas, aprovecho la ocasión para desearos a tod@s unas:
Muy FELICES FIESTAS DE NAVIDAD Y AÑO NUEVO 2018 !!!!!!!!!!!!!!!!!!!!!!!!!!!
P.D.:
Os pongo el código porque no me deja adjuntarlo ni como .xlsm ni como .pdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Option Explicit
'https://www.experts-exchange.com/questions/27222713/jacobi-method-in-vb.html
'Class Jacobi
Private Sub Jacobi()
Console.Clear()
Dim n As Integer
' Numero de ecuaciones/variables
Dim a As Double(,)
' Coeficientes de variables (on LHS)
Dim b As Double()
' Valores constantes (on RHS)
Dim x0 As Double()
' Aproximaciones previas a los valores de las variables
Dim x As Double()
' Aproximaciones actuales a los valores de las variables
Dim diff As Double()
' Diferencia absoluta entre aproximaciones
Dim tol As Double
' Tolerancia
Dim max As Integer
' Número máximo de iteraciones
Dim iterations As Integer
' Número real de iteraciones requeridas
Dim withinTol As Boolean
'Si los resultados estan dentro de la tolerancia
Dim isValidNumber As Boolean
Dim temp As String
Dim tempArray As String()
Dim value As Double
Dim finished As Boolean
Dim i, j, iteration As Integer
While True
Console.Write ("Entrar número de ecuaciones/variables (De 2 a 20): ")
isValidNumber = Integer.TryParse(Console.ReadLine(), n)
If isValidNumber And n > 1 And n < 21 Then
Exit While
End If
Console.WriteLine (vbLf & "Número inválido, por favor, volverlo a entrar" & vbLf)
End While
Console.WriteLine (vbLf & "Entrar los coeficientes de las variables, separados por espacios:" & vbLf)
a = New Double(n - 1, n - 1) {}
For i = 0 To n - 1
Do
Console.Write(" Ecuación {0} : ", i + 1)
temp = Console.ReadLine()
tempArray = temp.Split(New Char() {" "C}, StringSplitOptions.RemoveEmptyEntries)
If tempArray.Length <> n Then
Console.WriteLine (vbLf & "Número de coeficientes inválido, por favor, volverlos a introducir" & vbLf)
isValidNumber = False
Continue Do
End If
For j = 0 To n - 1
isValidNumber = Double.TryParse(tempArray(j), value)
If Not isValidNumber Then
Console.WriteLine (vbLf & "La linea contiene un número invalido, por favor, volver a introducir la linea" & vbLf)
Exit For
Else If j = i And value = 0 Then
Console.WriteLine (vbLf & "La diagonal principal no puede contener coeficientes cero, por favor entre de nuevo la línea entera" & vbLf)
isValidNumber = False
Exit For
Else
a(i, j) = value
End If
Next
Loop While Not isValidNumber
Next
Console.WriteLine (vbLf & "Entrar los valores constantes, separados por espacios" & vbLf)
b = New Double(n - 1) {}
Do
Console.Write (" Para todas las ecuaciones: ")
temp = Console.ReadLine()
tempArray = temp.Split(New Char() {" "C}, StringSplitOptions.RemoveEmptyEntries)
If tempArray.Length <> n Then
Console.WriteLine (vbLf & "Invalid number of constant values, please re-enter" & vbLf)
isValidNumber = False
Continue Do
End If
For i = 0 To n - 1
isValidNumber = Double.TryParse(tempArray(i), value)
If Not isValidNumber Then
Console.WriteLine (vbLf & "La linea contiene un número invalido, por favor, volver a introducir la linea entera" & vbLf)
Exit For
Else
b(i) = value
End If
Next
Loop While Not isValidNumber
Console.WriteLine (vbLf & "Entrar las aproximaciones iniciales, separadas por espacios" & vbLf)
x0 = New Double(n - 1) {}
x = New Double(n - 1) {}
diff = New Double(n - 1) {}
Do
Console.Write (" Para todas las variables : ")
temp = Console.ReadLine()
tempArray = temp.Split(New Char() {" "C}, StringSplitOptions.RemoveEmptyEntries)
If tempArray.Length <> n Then
Console.WriteLine (vbLf & "Número inválido de aproximaciones, por favor, volverlas a introducir" & vbLf)
isValidNumber = False
Continue Do
End If
For i = 0 To n - 1
isValidNumber = Double.TryParse(tempArray(i), value)
If Not isValidNumber Then
Console.WriteLine (vbLf & "La linea contiene un número invalido, por favor, volver a introducir la linea entera" & vbLf)
Exit For
Else
x0(i) = value
End If
Next
Loop While Not isValidNumber
Console.WriteLine()
While True
Console.Write ("Entrar la tolerancia ( > 0) para todas las variables : ")
isValidNumber = Double.TryParse(Console.ReadLine(), tol)
If isValidNumber And tol > 0 Then
Exit While
End If
Console.WriteLine (vbLf & "Número Invalido, por favor, volverlo a introducir" & vbLf)
End While
Console.WriteLine()
While True
Console.Write ("Entrar el número maximo de iteraciones (De 5 a 99) : ")
isValidNumber = Integer.TryParse(Console.ReadLine(), max)
If isValidNumber And max > 4 And max < 100 Then
Exit While
End If
Console.WriteLine (vbLf & "Número Invalido, por favor, volverlo a introducir" & vbLf)
End While
Console.WriteLine()
iterations = max
withinTol = False
For iteration = 1 To max
finished = True
For i = 0 To n - 1
x(i) = b(i)
For j = 0 To n - 1
If j = i Then
Continue For
End If
x(i) -= a(i, j) * x0(j)
Next
x(i) /= a(i, i)
diff(i) = Math.Abs(x(i) - x0(i))
If finished And diff(i) > tol Then
finished = False
End If
Next
If finished Then
iterations = iteration
withinTol = True
Exit For
End If
Array.Copy(x, x0, n)
Next
Console.WriteLine ("Los valores aproximados de las variables son:" & vbLf)
For i = 1 To n
Console.Write(" x{0} = {1:F5}", i, x(i - 1))
' display to 5 dp
Console.WriteLine()
Next
Console.WriteLine(vbLf & "Número de iteraciones: {0}", iterations)
Console.WriteLine("Las aproximaciones están dentro de la tolerancia : {0}", withinTol)
Console.Write (vbLf & "Pulsar una tecla para salir de la aplicación" & vbLf)
Console.ReadKey()
End Sub
'End Class
Valora esta pregunta


0