Skip to content

Homework 4 problem 1 issue #24

Open
jlw15102 opened this issue Mar 31, 2020 · 2 comments
Open

Homework 4 problem 1 issue #24

jlw15102 opened this issue Mar 31, 2020 · 2 comments

Comments

@jlw15102
Copy link

For part c of problem 1, I am struggling with finding the newton-raphson roots. I suspect that this issue may be a result of my f_T or dfdT functions, but I am not sure. These are my functions:
def f_T(T):
return (sigmoid(T)-0.5)

def dfdT(T):
return (14.811np.exp(14.811T))/((1+np.exp(14.811*T))**2)

This is my input to the newtraph function and the resulting error:
vr, out = newtraph(f_T,dfdT,65)
print(vr)

nan
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:5: RuntimeWarning: overflow encountered in exp
"""
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:5: RuntimeWarning: invalid value encountered in double_scalars
"""

Any help would be apppreciated!

@ryt16011
Copy link

So this is what i had for my dfDT function:
def dfdT(x, a0=15.043, a1=0.232):
dfdT = (-a1*np.exp(a0-a1*x))/(1+np.exp(a0-(a1*x))**2)
return dfdT

But overall, the code is not supposed to work. The way newtraph works is that it picks a point on a curve and draws a line tangent to that point. It then looks for where that line crosses the x-axis and that point is the estimated root. So if you try that at a local max/min, your line would be horizontal and never cross the x-axis. So in general, it is not suppose to work because dfDT = 0.

@mbs14014
Copy link

The Newton-Raphson method fails to converge if the x0 starting point is incorrectly choosen. This is because the Newton-Raphson method does not account for divergence in some cases when the function's x0 is not close enough to the root we are finding. For example in this case, the root is located at approximately T=64.8 using the Newton-Raphson method and through guess and check I determined that the range of accepted values for x0 is 55-75. If x0 is chosen in this range the function will work properly and return the root of our equation. However, if a number outside of this range is chosen using this method, a nan (not a number) error is given due to an attempt by python
to divide by zero. Rather than converging to the correct root outside of the accepted x0 range our Newton-Raphson method diverges thus causing the divide by zero error. I would recommend playing around with the x0 value in the following line of code when using Newton-Raphson:
'hr, out = newtraph(f_T,dfdT,x0)'

Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants