Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
approximation
#1
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.

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
Reply


Messages In This Thread
approximation - by mcgrim - Apr-13-2019, 08:22 PM
RE: approximation - by scidam - Apr-14-2019, 10:14 AM
RE: approximation - by mcgrim - Apr-14-2019, 11:38 AM
RE: approximation - by scidam - Apr-14-2019, 12:06 PM
RE: approximation - by mcgrim - Apr-15-2019, 10:19 AM
RE: approximation - by scidam - Apr-15-2019, 11:13 PM
RE: approximation - by mcgrim - Apr-17-2019, 08:13 PM
RE: approximation - by scidam - Apr-18-2019, 04:13 AM
RE: approximation - by mcgrim - Apr-18-2019, 07:20 AM
RE: approximation - by mcgrim - Apr-19-2019, 07:32 AM
RE: approximation - by mcgrim - Apr-19-2019, 08:08 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020