function ln(x as float)
n=16
ret#=3.141593/(2*agm(1,4/(x*2^n)))-n*.6931472
endfunction ret#
`arithmetic-geometric mean
function agm(x as float, y as float)
a#=(x+y)/2
g#=sqrt(x*y)
for n=1 to 10
at#=(a#+g#)/2
gt#=sqrt(a#*g#)
a#=at#
g#=gt#
next n
ret#=(a#+g#)/2
endfunction ret#
ln(x). The math is pretty much just what I've read off of wikipedia. I am not this math-savvy, and I think solving this involves a good understanding of calculus, the "arithmetic-geometric mean", pi's relation to e (i is probably involved somewhere in there) and Newtons Method. I know none of these. Don't ask me how it works. I'll definitely be going back and trying to re-derive this, but for now... meh. Here's an example of it:
global pixels_per_unit as float
pixels_per_unit=100
sync on
r1 as float
r1=0
r2 as float
r2=0
m as float
m=0
a as float
a=0
do
cls
from#=-4
ato#=4
for x#=from# to ato# step 0.005
dot x#*pixels_per_unit+screen width()/2,-ln(x#)*pixels_per_unit+screen height()/2
next x#
drawGraphPaper(pixels_per_unit,screen width()/2,screen height()/2,4)
sync
loop
end
function drawGraphPaper(PPU as integer, midx as integer, midy as integer, subUnits as integer)
if subunits<=0 then subunits=1
if PPU<=0 then ppu=40
subDist as float
subDist=PPU*1.0/subUnits
counter1 as float =
counter1=-PPU
counter2 as float = 0
w=screen width()
h=screen height()
ink rgb(0,0,40)
line 0,h/2,w,h/2
ink rgb(0,40,0)
line w/2,0,w/2,h
while (counter1+midx<w) or (midx-counter1>0)
inc counter1, PPU
counter2=0
while (counter2<PPU)
inc counter2, subDist
ink rgb(150,150,150),0
line midx+counter1+counter2,midy-2,midx+counter1+counter2,midy+2
line midx-counter1+counter2,midy-2,midx-counter1+counter2,midy+2
endwhile
ink rgb(255,255,255),0
line midx+counter1,midy-5,midx+counter1,midy+5
line midx-counter1,midy-5,midx-counter1,midy+5
endwhile
counter1=-PPU
counter2=0
while (counter1+midy<w) or (midy-counter1>0)
inc counter1, PPU
counter2=0
while (counter2<PPU)
inc counter2, subDist
ink rgb(150,150,150),0
line midx-2,midy+counter1+counter2,midx+2,midy+counter1+counter2
line midx-2,midy-counter1-counter2,midx+2,midy-counter1-counter2
endwhile
ink rgb(255,255,255),0
line midx-4,midy+counter1,midx+4,midy+counter1
line midx-4,midy-counter1,midx+4,midy-counter1
endwhile
endfunction
function ln(x as float)
n=16
ret#=3.141593/(2*agm(1,4/(x*2^n)))-n*.6931472
endfunction ret#
`arithmetic-geometric mean
function agm(x as float, y as float)
a#=(x+y)/2
g#=sqrt(x*y)
for n=1 to 10
at#=(a#+g#)/2
gt#=sqrt(a#*g#)
a#=at#
g#=gt#
next n
ret#=(a#+g#)/2
endfunction ret#
in the line "dot x#*pixels_per_unit+screen width()/2,-ln(x#)*pixels_per_unit+screen height()/2" , the negative sign is by ln(x#) because pixel 0,0 is at the top left and, w,h is at the bottom right, but we want negatives at the bottom and positives at the top.
[edit]
I've already found a plug-in somewhere with this function, I just wanted to do it myself cuz i was bored.