- Mon Apr 03, 2017 2:48 pm
#64575
Yomero wrote:When I create the arcsin function I am not getting the correct values for the calculated values (-1 to 1) in this function using the series equivalent to the arcsin function of this link https://en.wikipedia.org/wiki/Inverse_t ... _functions.
Hi Yomero!
I copied your code and it seems to work more or less OK, although it converges VERY slowly as you approach 1 or -1. I don't think 60 iterations is going to get you close.
However, I looked at the code and it seems a bit wasteful, so I took the trouble to rewrite it a bit, and now I can get over 35000 iterations before my chip reboots. That's good enough to give the right answer for asin(0.9999) to six decimal places or so. If you need to calculate values near 1 you may be better off with an approximation routine. More "normal" values are fine with just 50 iterations, or less.
Have a tryout, 'n' is the number of iterations, obviously, just hardcode it if there's some value you're happy with.
Code: Select all
function arcsin(x, n, term, sum, a, b, c)
if n==1 then return sum
else
local nextterm = term*x*x*a*a/(b*c)
--print(sum)
return arcsin (x, n-1, nextterm, sum+nextterm, a+2, b+2, c+2)
end
end
function asin(x)
local n
local ax = math.abs(x)
if ax<0.25 then n=10
elseif ax< 0.5 then n=20
elseif ax< 0.6 then n=30
elseif ax< 0.7 then n=40
elseif ax< 0.8 then n=60
elseif ax< 0.9 then n=130
elseif ax< 0.95 then n=250
elseif ax< 0.99 then n=1200
-- probably better to use something else for the cases below
elseif ax< 0.999 then n=10000
else n = 35000
end
return arcsin(x,n,x,x,1,2,3)
end
Last edited by alex_g on Tue Apr 04, 2017 7:30 am, edited 3 times in total.