How can I map linear data points to logarithmic data points?
This is different from Map a linear scale to a logarithmic one with both scales having variable min/max and Convert Linear scale to Logarithmic in that they are converting to exponential scale, not a logarithmic one (they are converting the axes scales to logarithmic, but data points to exponential. I want the scales to stay the same, and data points to be logarithmic).
This questions asks to map from the red line to the blue line (Desmos).
Note: this is a self-answered question since the linked posts do not do what I want, and that there is no code (as expected on Stack Overflow).
Since this is Stack Overflow and not https://math.stackexchange.com/, I expect a programming solution.
I find it easier to reason about by first converting it to a 0 to 1 scale.
const val_lin = 450;
const min_lin = 100;
const max_lin = 1000;
const min_log = 10;
const max_log = 200;
// input: a value between lin_min and lin_max (both inclusive)
// output: a value between 0 and 1 where 0.5 corresponds to middle of the input range
function normalize_lin(lin_val, lin_min, lin_max) {
return (lin_val - lin_min) / (lin_max - lin_min);
}
const val_lin_normalized = normalize_lin(val_lin, min_lin, max_lin);
function lin_to_log(lin_val_normalized, log_min, log_max) {
const log_val_normalized = Math.log10(9 * lin_val_normalized + 1);
return log_val_normalized * (log_max - log_min) + log_min;
}
const val_log = lin_to_log(val_lin_normalized, min_log, max_log);
console.log(`on a linear scale from ${min_lin} to ${max_lin}, ${val_lin} maps to ${val_log} on a logarithmic scale from ${min_log} to ${max_log}`);