-1

I'm trying to write a numerical method in python, here is the code in full:

# Newton-Raphson iterative scheme
qin_secs = 100 
W = 3
L = 7000
A = W * L
n = 0.04
S = 0.007
b = 100
deltatime = 1

# define the function f(x)
def f(x):
    return qin_secs-(((x-b)/deltatime)*A)-((W*x)/n)*((W*x)/((W+2)*x))**0.66*(S)**0.5
    
# define the derivative of f(x)
def f_prime(x):
    return -((A / deltatime) * x) - ((W*(S)**0.5)/n) * (((W*x)/(W + 2*x))**0.66 + (0.66 * x) * (((1/(x)) + (2/W))**0.33) * (W**2)/((W+(2*x))**2))

# define the initial guess
x0 = 101
# define the tolerance
tol = 1e-6
# define the maximum number of iterations
max_iter = 100
# initialize the iteration counter
n = 0
# compute the initial error
error = abs(f(x0))

# iterate until the error is less than the tolerance
# or the maximum number of iterations is reached
while error > tol and n < max_iter:
    # update the approximation
    x1 = x0 - f(x0) / f_prime(x0)

    # update the error
    error = abs(f(x1))

    # update the iteration counter
    n += 1

    # update the initial guess
    x0 = x1

# print the final approximation
print(x1)

The error appears on the following line:

def f(x):
    return qin_secs-(((x-b)/deltatime)*A)-((W*x)/n)*((W*x)/((W+2)*x))**0.66*(S)**0.5

I'm not 100% sure where the error is but I tried the following to bypass the error:

def f(x):
    if x == 0:
        return 0
    return qin_secs-(((x-b)/deltatime)*A)-((W*x)/n)*((W*x)/((W+2)*x))**0.66*(S)**0.5

But no luck, still getting the error. Thanks for the help.

dinn_
  • 93
  • 1
  • 10
  • You have division three times on that line, and that error indicates that at some point, a 0 is becoming the second operand to one of the calls to `/`. So, either `deltatime`, `n`, or the part at the end is evaluating to 0. Break it up and bit over a few lines, or manually check what `n` and such are. – Carcigenicate Dec 04 '22 at 23:59
  • Adding to the above comment, `W == -2` also leads to a div by zero error. – Loocid Dec 05 '22 at 00:02
  • This is a drawback to having so many calculations on one line. If there is an error, you can't necessarily tell exactly where it is. – John Gordon Dec 05 '22 at 00:09
  • Thanks for advice. I have made a quick edit. I know that none of the variables are zero and W is not -2 – dinn_ Dec 05 '22 at 00:14
  • Can you list what each variable is so we can reproduce the issue? – Loocid Dec 05 '22 at 00:16
  • 1
    We don't believe you. ;) Add `print(x,b,deltatime,A,W,n,S)` just before this. – Tim Roberts Dec 05 '22 at 00:16
  • 1
    @dinn_ If you're getting that error, that's impossible. Keep debugging. – Carcigenicate Dec 05 '22 at 00:17
  • Thanks for help everyone. I have gone into way more detail in the question because after debugging for a bit I am realising the line with the error works fine with the given variables in isolation, so must be something wrong with the rest of the code? – dinn_ Dec 05 '22 at 00:27
  • 2
    `n = 0` this is your problem. – Axe319 Dec 05 '22 at 00:32
  • 1
    @Axe319 More correctly they are trying to re-use the variable `n` in different contexts. That's why using global variables is not a good idea. – Selcuk Dec 05 '22 at 00:58

1 Answers1

1
qin_secs-(((x-b)/deltatime)*A)-((W*x)/n)*((W*x)/((W+2)*x))**0.66*(S)**0.5

There are three denominators here:

deltatime
n
((W+2)*x))**0.66*(S)**0.5

The first two are single variables, so checking them is straightforward.

The third is an expression. Any of W == -2, x == 0, or S == 0 would lead to the entire expression being zero.

So, check those variables.

John Gordon
  • 29,573
  • 7
  • 33
  • 58