Apr-13-2019, 08:22 PM
I am writing a function that approximates the natural logarithm function by using the following formula
under iteration:
a sub 0= (1+x)/2 b sub 0= sqrt(x)
a sub (i+1)= (a sub i + b sub i)/2
b sub (i+1) = sqrt(a sub (i+1) + b sub i)
and the approximation to ln(x) is equal to ((x-1)/a sub i)
this is the code I came up with but I am not sure I am getting the right results,
since when I increase n, the approximation does not get closer to ln(x) (shouldn't it? I'm not sure, if you know,
please share), and the graphs I am getting are empty.
under iteration:
a sub 0= (1+x)/2 b sub 0= sqrt(x)
a sub (i+1)= (a sub i + b sub i)/2
b sub (i+1) = sqrt(a sub (i+1) + b sub i)
and the approximation to ln(x) is equal to ((x-1)/a sub i)
this is the code I came up with but I am not sure I am getting the right results,
since when I increase n, the approximation does not get closer to ln(x) (shouldn't it? I'm not sure, if you know,
please share), and the graphs I am getting are empty.
def applog(n,x): a0=(1+x)/2 b0=sqrt(x) for i in range(n): a0=(a0+b0)/2 b0=sqrt((a0+b0/2)*b0) return (x-1)/a0 print("the log approximation is: " , ((applog(1,4)))) print("log value-------------------:" , (math.log(4)) ) #of n and get strange result error=(abs( applog(4,4)- (math.log(4)) )) print("the error is : ", error) plt.plot((applog(1,4)) ) plt.plot((math.log(4))) plt.show() #should plot both funtctions in the same graph plt.plot(error ) plt.show() # should plot the difference of both functions