javascript - Find n logarithmic intervals between two end points -
i trying find n logarithmic intervals between 2 numbers.
eg: function logdiv (10, 10000, 3) 10 starting point, 10000 ending point, , 3 number of intervals, following output: (* {10, 100, 1000, 10000} *)
what have tried:
function loginterval(total_intervals, start, end) { var index, interval, result = []; (index = 0; index < total_intervals; index++) { interval = (index/total_intervals * math.log((end - start) + 1) - 1 + start); result.push(interval); } return result; } var intervals = loginterval(5, 1, 500);
https://jsfiddle.net/qxqxwo3z/
this based on (poor) understanding of following solution found in stack exchange mathematica:
logspace [increments_, start_, end_] := module[{a}, ( = range[0, increments]; exp[a/increments*log[(end - start) + 1]] - 1 + start )]
please can me this? not necessary follow of above attempts, explaining tried.
the best way start logarithmic difference of end value , start value, divided intervals.
x = (math.log(end) - math.log(start)) / total_intervals;
for factor, need reverse operation
factor = math.exp(x);
for getting array can multiply start value factor , insert array. next value last value multiplied factor, until items generated.
function loginterval(total_intervals, start, end) { var x = (math.log(end) - math.log(start)) / total_intervals, factor = math.exp(x), result = [start], i; (i = 1; < total_intervals; i++) { result.push(result[result.length - 1] * factor); } result.push(end); return result; } console.log(loginterval(3, 10, 10000)); console.log(loginterval(5, 1, 500)); console.log(loginterval(12, 220, 440)); // frequencies
Comments
Post a Comment