tommaso.gastaldi@uniroma1.it wrote:[color=blue]
> It might not have solutions or have multiple (2) solution, depending on
> A,B,C.[/color]
After some deep analysis (hehe) I found that the equation was more like
e^(A*h0)+A*ho = C
[color=blue]
>
> To find 1 solution, if exists, you could see it as :
>
> h0 = Math.Log(C + B * h0) / A
>
> and by iteration solve it.
>
> Sorry not familiar with VBA but can write quickly some VB.NET that you
> can easily adapt.[/color]
Thanks for your iteration algorithm, I however adapted a newton-rapson
algorithm (found C sources) like this: (It *seems* to work..)
Adapted from
http://mathews.ecs.fullerton.edu/n20...rog_lnk_4.html
'---------------------------------------------------------------------------
' Algorithm translated to Visual Basic by: Santiago Zapata
' Based on Algo2-5.c from NUMERICAL METHODS: C Programs, (c) John H.
Mathews 1995,
' Dr. Norman Fahrer C Version
' Algorithm 2.5 (Newton-Raphson Iteration). To find a root
' f(x) = 0 given one initial approximation p_0 and using the
iteration
'
' f (p_(K - 1))
' p_k = p_(k-1) - ----------- for k = 1, 2, ...
' f '(p_(k-1))
'
'---------------------------------------------------------------------------
'User has to supply a function named : ffunction
' and its first derivative : dffunction
'---------------------------------------------------------------------------
Private Function ffunction(ho As Double, A As Double, B As Double) As
Double
ffunction = Math.Exp(A * ho) - A * ho - B
End Function
Private Function dffunction(ho As Double, A As Double) As Double
dffunction = A * Math.Exp(A * ho) - A
End Function
Private Function newtonRapson(P0 As Double, A As Double, B As Double)
Dim Delta As Double
Dim Epsilon As Double
Dim Small As Double
Dim Max As Integer
Dim cond As Integer
Dim K As Integer ' Counter for loop
Dim p1 As Double ' New iterate
Dim y0 As Double ' Function value
Dim y1 As Double ' Function value
Dim df As Double ' Derivative
Dim dp As Double
Dim RelErr As Double
Delta = 0.000001 ' Tolerance
Epsilon = 0.000001 ' Tolerance
Small = 0.000001 ' Tolerance
Max = 99 ' Maximum number of iterations
cond = 0 ' Condition fo loop termination
y0 = dffunction(P0, A)
For K = 1 To Max
If (cond) Then Exit For
df = dffunction(P0, A) ' Compute the derivative
If (df = 0) Then ' Check division by zero
cond = 1
dp = 0
Else
dp = y0 / df
End If
p1 = P0 - dp ' New iterate
y1 = ffunction(p1, A, B) ' New function value
RelErr = 2 * Math.Abs(dp) / (Math.Abs(p1) + Small) ' Relative
error
If ((RelErr < Delta) And (Math.Abs(y1) < Epsilon)) Then '
Check for
If (cond <> 1) Then cond = 2 '
convergence
End If
P0 = p1
y0 = y1
Next
MsgBox "The current " & Str$(K - 1) & "-th iterate is " & Str$(p1)
MsgBox "Consecutive iterates differ by " & Str$(dp)
MsgBox "The value of f(x) is " & Str$(y1)
If (cond = 0) Then MsgBox "The maximum number of iterations was
exceeded!"
If (cond = 1) Then MsgBox "Division by zero was encountered !"
If (cond = 2) Then MsgBox "The root was found with the desired
tolerance!"
newtonRapson = y1
End Function
[color=blue]
>
>
> An example of result:
> Dim A As Double = 12
> Dim B As Double = 8
> Dim C As Double = 30
>
> A solution: 0,289632950361349 converged in 4 iterations
>
> -tommaso
>
>
> '------------------- adapt this and let me know if it works fine (must
> adapt it to find ALL solutions)
>[/color]
SNIP Iteration Algorithm
Thanks for your help, please let me know if you have any observations
about my adaptation.
--
Slash
[
http://www.santiagoz.com]