3 Replies - 413 Views - Last Post: 08 February 2012 - 09:23 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

False-Position method for finding function roots

Posted 08 February 2012 - 01:03 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?[Code]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[code/]

Is This A Good Question/Topic? 0
  • +

Replies To: False-Position method for finding function 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: False-Position method for finding function roots

Posted 08 February 2012 - 01:25 AM

A couple of points (that we went through on your previous post).

1. Please post your code in [ CODE] tags.
2. Describe how it "crashes"? What error do you get?
3.

Quote

This thing keeps crashing when I enter xl=0, xl=2. it does not crash for xl=-2,xl=0
Your description says you enter xl twice, is that correct as it doesn't make sense?
4. How are you calling this function, i.e. it accepts 3 values.
5. Consider using datatypes on your functions to match what your working with. Don't rely on VB to do the casting for you.
6. Consider adopting a naming convention for your variables, it will make it easier to read the code, especially when you ask someone else to look at it.
7. Please update your previous post with the solution as requested....it's not a requirement on here, but it helps others and shows that you're happy to contribute to the forum and not just "use" it.

sorry if the above sounds harsh, it's not meant to be, it's just offering some advice to help you along.
Was This Post Helpful? 0
  • +
  • -

#3 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: False-Position method for finding function roots

Posted 08 February 2012 - 03:14 AM

The edit button is usually at the bottom of your original post.

It sounds as though you're in an infinite loop.

You only have one exit point from the loop. If the criteria isn't met, it will loop forever.

Look at putting some kind of limit on the loop so at least it will "timeout" if it hasn't reached a result in a certain time or a certain number of iterations.

You still have told how you're calling it - All the values! Your exit strategy is based on es and we don't know what es is.
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: False-Position method for finding function roots

Posted 08 February 2012 - 09:23 AM

I call it from a cell = (-2,0,0.005) The es is a random decimal number that I pick.
the above values do give "-1", but it's cell = (0,2,0.005) that is making the program loop forever as you said.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1