0

Let me begin by saying that I am a lousy programmer and I am hacking away at something that interests me. I havent used these forums before so I hope the code I post doesnt get corrupted. I am working on a logarithm solver using a method explained by napier and euler. Starting with the numbers 1 and 10 the logs base 10 are 0 and 1. The arithmetic mean of the logs (0 + 1)/2 = 0.5 is the log of the geometric mean of the the two numbers math.sqrt(1*10) = value
Suppose my target number is log(5)
The code below works for the first pass but I want to find a way to pass
the indexes as variables to the gmean function via a loop and probably
exit with something like if (target - lastvalue) < 0.00000005

#!/usr/local/bin/python3.0

import math

a = 1.0
b = 10.0
target = 5.0

def gmean(x,y) :
        return math.sqrt(x*y)

mylist = []

mylist.append(a)
mylist.append(b)
n = len(mylist)
np = n - 1
npp = n - 2
print ("index var",np, npp)
ta = float(mylist[npp])
tb = float(mylist[np])
print (ta, tb)
tc = gmean(ta,tb)
mylist.append(tc)
print (mylist)

if  (target <  tc < tb) :
        td = gmean(ta, tc)
        print ("from upper",td)
        mylist.append(td)
        print (mylist)
elif (ta < tc < target) :
        td = gmean(tc, tb)
        print ("from lower",td)
        mylist.append(td)
        print (mylist)
else:
        print("comp failed")
1
Contributor
1
Reply
2
Views
8 Years
Discussion Span
Last Post by oystercatcher
0

well here is my second attempt, next need to write to a file with the
generated values as laid out by euler

#!/usr/local/bin/python3.0

import math

a = 1.0
b = 10.0
target = 5.0
variance = 0.0000001

def amean(t,u) :
  return ( (t + u)/2.0 )
        
def gmean(x,y) :
  return math.sqrt(x*y)

mylist = []
myvar = []

mylist.append(a)
mylist.append(b)

def antilogserfirstpass( la,lb ) :
  mylist.append(gmean(la,lb))
  mylist.sort()
#  print (mylist)
  del mylist[0]

def antilogser( la,lb ) :
  mylist.append(gmean(mylist[0],mylist[1]))
#  print ("in funct logser",mylist)

  myvar = []
  for z in mylist :
    myvar.append( abs(z - target) )
#  print ("myvar array",myvar)
  mlbye = myvar.index(max(myvar))
#  print ("max from target index",mlbye)
  del mylist[mlbye]
#  print ("in for loop myvar",z)
  print ("-----------------------------------------")
#

antilogserfirstpass(a,b)
print (mylist)  
#print (myvar)

for i in range(20) :
  antilogser(mylist[0],mylist[1])  
  print (mylist)
  diff = abs(mylist[1] - mylist[0] )
  if ( diff < variance ) :
    print ("difference less than variance", diff)
  
#print (myvar)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.