domingo, 11 de diciembre de 2011

Ecuaciones no lineales

Funciones programadas en VBA para resolver ecuaciones no lineales



'función a resolver
Public Function g(x) As Double
    g = Exp(-x) - x
End Function

'derivada de g(x)
Public Function dg(x) As Double
    h = 0.000001
    dg = (g(x + h) - g(x)) / h
End Function

'bisección

Public Function biseccion(a, b, n)

If g(a) * g(b) > 0 Then
    MsgBox ("la función tiene el mismo signo en ambos extremos")
End If

If g(a) * g(b) = 0 Then

    If g(a) = 0 Then
        s = a
    Else
        s = b
    End If

Else

    For i = 1 To n
        
        s = (a + b) / 2
        
        If g(a) * g(s) < 0 Then
            b = s
        Else
            a = s
        End If
    
    Next i

End If


biseccion = s

End Function


'newton raphson
Public Function newtonraphson(x0, n) As Double

xi=x0

For i = 1 To n

    xi_1 = xi - g(xi) / dg(xi)
    xi = xi_1
    
Next i

newtonraphson = xi_1

End Function

'punto fijo
Public Function ptofijo(x0, n)

xi=x0

For i = 1 To n
    
    x_i = g(xi) + xi
    xi = xi_1

Next i

ptofijo = x_i

End Function

No hay comentarios:

Publicar un comentario