0

I was trying to solve an equation for the catenary and wanted to use the Newton-Raphson method.

enter image description here

from math import sinh, cosh
y = 0.4                #Has taken to initiate the iteration.
k = 3/2
for _ in range(5):    #Iterations for Newton-Raphson Method
    y = y - (sinh(y)-y*k)/(cosh(y)-k)
    print(y)
print(y)
  • Output
-0.05174312094834577
9.262910138898434e-05
-5.298477449974456e-13
0.0
0.0
0.0

The unexpected output I was expecting 1/0.6164729394

  • why isn't it `x + (sinh....`? – depperm Oct 05 '21 at 10:52
  • comments, make a good version history, that other users can track. What is the expected output? – depperm Oct 05 '21 at 11:00
  • Either the mathematical operation is incorrect or the number was so small it got a rounding error. https://pythonhosted.org/bigfloat/ – Paul Bekaert Oct 05 '21 at 11:12
  • What I mean is your output has 6 lines, what is the expected output that has 6 lines, I'm assuming the other values aren't matching up – depperm Oct 05 '21 at 11:15
  • I'm aware, but if you're debugging, why not start from the beginning. Is the first output (`-0.0517...`) correct? – depperm Oct 05 '21 at 11:19
  • 1
    Your solution is also "valid", in the sense that it leads to 0/0, which is pretty much undefined, and could therefore just as well be equal to 3/2. So your solution is not necessarily technically (w.r.t. the algorithm implementation) wrong, or perhaps not even mathematically wrong, but practically, you have to provide a good starting point for minimisation routines. – 9769953 Oct 05 '21 at 11:25

1 Answers1

2

Your curve has 3 roots:

curve of sinh(y) - ky

Your solution (y = 0) is one solution. There is a positive solution at 1.622, and a symmetrical negative one at -1.622.

If you don't know your formula, best is to actually view it (when possible; here it is easy to do), to gain some insight.

Further, Newton-Raphson's result, the root, will depend on your starting point, and how it converges towards a root (with big jumps or small jumps, depending on function and derivative value). Be aware of that.


Related: https://math.stackexchange.com/questions/3472880/solving-sinh-x-kx

9769953
  • 10,344
  • 3
  • 26
  • 37