Option Explicit Function Falsestart(xl, xu, es) 'Initialize Dim xold As Double, xr As Double xold = xr 'check if Function crosses the x- axis If func(xl) * func(xu) >= 0 Then MsgBox "No change in sign! this interval does not contain a solution.Enter new xl and xu" Falsestart = 0 Exit Function End If Do 'False position method xr = (xu - (func(xu) * (xl - xu)) / (func(xl) - func(xu))) If func(xr) = 0 Then Exit Do If func(xr) * func(xl) < 0 Then xu = xr Else xl = xr End If If xr <> 0 Then ' check for tolerance criteria If Abs((xr - xold) / xr) * 100 < es Then Exit Do End If Else xold = xr End If Loop Falsestart = xr End Function Function func(x) func = (x ^ 2) - 1 End Functi
The false position method for finding roots
Page 1 of 13 Replies - 344 Views - Last Post: 08 February 2012 - 11:39 AM
Topic Sponsor:
#1
The false position method for finding roots
Posted 08 February 2012 - 01:18 AM
This thing keeps crashing when I enter xl=0, xl=2. it does not crash for xl=-2,xl=0. any hints on what makes a VBA macro crash every few runs?
Replies To: The false position method for finding roots
#2
Re: The false position method for finding roots
Posted 08 February 2012 - 01:27 AM
Please stop producing duplicate posts....there is an edit button.
#3
Re: The false position method for finding roots
Posted 08 February 2012 - 02:26 AM
I made a mistake. this program works in the inteval xl= -2, xu = 0,and the result is -1; however, when i put xl=0, xu=2, I do not get "1" like it should be. the whole thing freezes you know "excel is not responding" it does the same thing everytime.
where is this edit button? I tried bro.
where is this edit button? I tried bro.
#4
Re: The false position method for finding roots
Posted 08 February 2012 - 11:39 AM
So far so good, I added a max variable for the a maxnumber of iterations. Now the user enters
xl(lower bound), xu(upper bound), es(tolerance), and max(number of iterations).
thank you for the help, and i 'll keeo working on my dremincode protocol.
xl(lower bound), xu(upper bound), es(tolerance), and max(number of iterations).
thank you for the help, and i 'll keeo working on my dremincode protocol.
Option Explicit
Function FalsePosition(xl, xu, es, max)
'Initialize
Dim xold As Double, xr As Double, iter As Double
xold = xr
iter = 0
'check if Function crosses the x- axis
If func(xl) * func(xu) >= 0 Then
MsgBox "No change in sign! "
FalsePosition = 0
Exit Function
End If
Do
'False position method
xr = (xu - (func(xu) * (xl - xu)) / (func(xl) - func(xu)))
If func(xr) = 0 Then Exit Do
If func(xr) * func(xl) < 0 Then
xu = xr
Else
xl = xr
End If
iter = iter + 1
If xr <> 0 Then
' check for tolerance criteria
If Abs((xr - xold) / xr) * 100 < es Or iter >= max Then
Exit Do
End If
Else
xold = xr
End If
Loop
FalsePosition = xr
End Function
Function func(x)
func = x ^ 2 - 1
End Function
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|