You are on page 1of 5

Fabio Salazar

Método Secante en Visual Basic

Sub Secante ()

'*************************************

'Declaración de Variables

Dim X2 As Double

Dim Xi As Double

Dim Xi1 As Double

Dim Y0 As Double

Dim Y1 As Double

Dim Aux1 As Double

Dim Aux2 As Double

Dim ConCeldas As Integer

Dim Absoluto As Double

Dim Formula As String

Dim con As Integer

Dim Xn As Double

Dim Xn1 As Double

Dim OK As Boolean

Dim Fun As New clsMathParser

'**************************************

'Inicialización de variables

Formula = Cells(3, 2) 'recibe como parámetro la función

tol = Cells(4, 2) 'recibe como parámetro la Tolerancia

con = 0

Xi = Cells(3, 5) 'recibe como parámetro la aproximación incicial X0


Fabio Salazar

Xi1 = Cells(4, 5) 'recibe como parámetro la la aproximación incicial X1

ConCeldas = 6

Absoluto = 100

OK = Fun.StoreExpression(Formula)

If Not OK Then GoTo Error_Handler

If Err Then GoTo Error_Handler

Error_Handler: Cells(8, 9) = Fun.ErrorDescription 'lanza un mansaje de error si se escribio mal


la función

'*******************************************

'inicio de iteraciones

While Absoluto > tol

Cells(ConCeldas, 1) = con

Cells(ConCeldas, 2) = Xi

Cells(ConCeldas, 3) = Xi1

Y0 = Fun.Eval1(Xi)

Y1 = Fun.Eval1(Xi1)

Cells(ConCeldas, 4) = Y1

If ((Y1 - (Y0)) = 0) Then

Absoluto = tol

Cells(ConCeldas, 4) = ("No se puede aplicar el método")

Else

Aux1 = Y1 * (Xi1 - (Xi))

Aux2 = Y1 - (Y0)

X2 = Xi1 - (Aux1 / Aux2)

Absoluto = Abs(Xi1 - Xi)

End If

Xn = Cells(ConCeldas, 3)
Fabio Salazar

Xn1 = Cells(ConCeldas, 2)

Cells(ConCeldas, 5) = Abs(Xn - Xn1)

Xi = Xi1

Xi1 = X2

Y0 = Y1

Y1 = Fun.Eval1(Xi1)

con = con + 1

ConCeldas = ConCeldas + 1

Wend

End Sub

Método Bisección en Visual Basic

Sub Biseccion()

'*************************************

'Declaración de Variables

Dim a As Double

Dim b As Double

Dim c As Double

Dim Tol As Double

Dim Fa As Double

Dim Fb As Double

Dim Formula As String

Dim OK As Boolean

Dim Fun As New clsMathParser

Dim conResul As Integer

Dim cont As Integer


Fabio Salazar

'**************************************

'Inicialización de variables

conResul = 7

a = Cells(4, 4) 'recibe como parámetro el intervalo a0

b = Cells(4, 6) 'recibe como parámetro el intervalo b0

Tol = Cells(5, 6) 'recibe como parámetro la Tolerancia

cont = 1

Formula = Cells(4, 2) 'recibe como parámetro la función

OK = Fun.StoreExpression(Formula)

If Not OK Then GoTo Error_Handler

If Err Then GoTo Error_Handler

Error_Handler: Cells(11, 11) = Fun.ErrorDescription 'lanza un mensaje de error si se escribío


mal la función

'*******************************************

'inicio de iteraciones

While ((b - a) > Tol)

Cells(conResul, 1) = cont

Cells(conResul, 3) = b

Cells(conResul, 2) = a

c = (a + b) / 2

Cells(conResul, 4) = c

Fa = Fun.Eval1(a)

Cells(conResul, 5) = Fa

Fb = Fun.Eval1(c)

Cells(conResul, 6) = Fb

If ((Fa * Fb) < 0) Then


Fabio Salazar

b=c

Else

a=c

End If

cont = cont + 1

conResul = conResul + 1

Wend

End Sub

You might also like