3 Replies - 344 Views - Last Post: 08 February 2012 - 11:39 AM Rate Topic: -----

Topic Sponsor:

#1 sokerfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 10-July 11

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?
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


Is This A Good Question/Topic? 0
  • +

Replies To: The false position method for finding roots

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 211
  • View blog
  • Posts: 1,249
  • Joined: 26-March 09

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.
Was This Post Helpful? 0
  • +
  • -

#3 sokerfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 10-July 11

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.
Was This Post Helpful? 0
  • +
  • -

#4 sokerfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 10-July 11

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.

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1