# How to Fix False Position Program

747 views
0

So for my program created, I ran the debugger and it just jumps right over the function I created and am not sure why...

``````import numpy as np
import math
import matplotlib.pyplot as plt

def f1(x):
return x*np.tan(x)

def f2(x):
return np.cos(x)

ub = float(input("Enter a value for the upper bound of the function: "))
lb = float(input("Enter a value for the lower bound of the function: "))
tolf = float(input("Enter the tolerance for this function: "))

def myFP(lb,ub,tolf):
xc = ((lb*f1(ub))-(ub*f1(lb)))/(f1(ub)-f1(lb))
while abs(f1(xc)) > tolf:
if f1(xc)*f1(lb) > 0:
lb = xc
else:
ub = xc
xc = ((lb * f1(ub)) - (ub * f1(lb))) / (f1(ub) - f1(lb))

print ("The root of the function is ", myFP)
``````

Of course, your function never ran. you need to call your function like `myFP(ub, lb, tolf)`, also the format string is wrong.

How would I be able to fix those problems in able for it to work?

As Rocky Li said. You just add ans = myFP(lb,ub,tolf) before print. But before that, you need to add a return xc in you myFP

1

If you talk about `myFP`, I guess that your debugger will go straight to the `print` line.

It is because you have defined your function, but never used it at this point.

You should call it using `myFP(arg1,arg2,arg3)`. However, you really don't wan't to use the name of global variables as arguments when defining your function (the `def` line) since it may be really confusing if it shadows the global variable or not...

Therefore, your function definition should be

``````def myFP(arg1,arg2,arg3):
xc = ((arg1*f1(arg2))-(arg2*f1(arg1)))/(f1(arg2)-f1(arg1))
while abs(f1(xc)) > arg3:
if f1(xc)*f1(arg1) > 0:
arg1 = xc
else:
arg2 = xc
xc = ((arg1 * f1(arg2)) - (arg2 * f1(arg1))) / (f1(arg2) - f1(arg1))
return xc
``````

(Please note the presence of `return` at the end.)

With that said, if you want to use your function within a `print` call, you can do so by using

``````print ("The root of the function is ", myFP(lb,ub,tolf) )
``````

Finally, to show it in a debugger, you should set a breakpoint at the first line of your function and step to the next line while running your `print` line or, better, using a Step Into feature on the `print` line.

Since `myFP` is called whithin the `print` call, the "call order" will be `myFP` -> `print` and you should be able to enter your function without any problem.

By the way, you might read some tutorial about defining functions in python such as this one (Or any other) to be sure about your understanding.

posted this
2

``````import numpy as np
import math
import matplotlib.pyplot as plt

def f1(x):
return x*np.tan(x)

def f2(x):
return np.cos(x)

ub = float(input("Enter a value for the upper bound of the function: "))
lb = float(input("Enter a value for the lower bound of the function: "))
tolf = float(input("Enter the tolerance for this function: "))

def myFP(lb,ub,tolf):
xc = ((lb*f1(ub))-(ub*f1(lb)))/(f1(ub)-f1(lb))
while abs(f1(xc)) > tolf:
if f1(xc)*f1(lb) > 0:
lb = xc
else:
ub = xc
xc = ((lb * f1(ub)) - (ub * f1(lb))) / (f1(ub) - f1(lb))

print ("The root of the function is ", myFP(lb, ub, tolf)) #and this
``````

I think that covers it.

posted this