Apr-23-2019, 12:54 PM
the following function gives an approximation to the natural log function
and is totally correct:
why so? What's wrong with the second code?
and is totally correct:
def applog(n,x): a0=(1+x)/2 b0=sqrt(x) for i in range(n): a0=(a0+b0)/2 b0=sqrt((a0)*b0)This other code is supposed to make the above function reach convergence faster
def fastln(n, x): a0 =(1 + x) / 2 b0 =x ** (1 / 2) for i in range(n): a0=(a0 + b0) / 2 a0=((a0 * b0) ** (1 / 2)) for i in range(n+1): for j in range(1,n): a0=xx cc=[] cc.append(xx) ((cc[0][1] - 4 ** (-j) * cc[0][1])/(1 - 4 ** (-j))) return (x - 1)/cc[-1][-1] print(fastln(4,4)) print(fastln(3,3)) x = np.linspace(10, 500, 100) nn=range(2,10) colors = mpl.cm.rainbow(np.linspace(0, 1, len(nn))) for c,n in zip(colors,nn): plt.plot(x, (ln(n,x)), label='$n = {nn}$'.format(nn=list(nn) ))however, the graph I obtain is totally unrelated to the log function,
why so? What's wrong with the second code?