I need some help to build a mathematical function, which takes three parameters (min, max, resolution) and returns values (nearly) in this distribution:
5
10.2
13.45
15.4
16.7
17.35
18
In this example, min is 5
, max is 18
and there are 7
steps.
So I calculated the range, which is max - min
(=13) and used these percentages (can be adjusted a bit, if calculation gets easier)
40%
65%
80%
90%
95%
100%
of the range, which is added to the min value.
13 * 0.4 + 5 = 10.2
13 * 0.65 + 5 = 13.45
...
The graph of these values, should be the same if the resolution is higher: If there are 100 instead of 7 steps, the distribution/graph should be the same. But how do I calculate the value of each step?
function calcValues(min, max, steps) {
const data = []
const range = max - min
for (i = 0; i < steps; i++) {
const value = range * whichValueIsNeededHere + min
data.push(value)
}
return data
}
First Approach:
This graph seems close to the y = a(x^3) + b(x^2) + c(x) + d
. I was able to retrieve the values of a, b, c and d with the help of the given (x,y)
pairs. The values are:
a = 0.08125
b = -1.421875
c = 8.896875
d = -2.55625
Now, if you substitute different x
values, you will be getting exact values (or close) to y
. The minimal difference in the y
values could be because of some constant which has not been taken into consideration in the equation (but like I said, the difference is very minimal).
The approach to use this equation is as follows:
Divide the max(x) - min(x)
by steps
to get the stepX
.
Add the stepX
to different values of x
(within the range of x
values i.e. [1,7]).
Substitute x
values in the equation to get different y
values.
Note - stepX is each step that needs to be taken to go from min to max in the X-axis.
This is a manual process for you to test the integrity of the graph for different (x,y) pairs.
Second Approach:
As you confirmed that you need the function to work only for a particular graph, we can use the given (x,y)
pairs as the reference in the program. Well, this could be a trick or hack, whatever you say!!.
function traverseGraph(source, destination, steps) {
// As the scenario is for a particular graph only, we can use the example for the reference
const xValues = [1, 2, 3, 4, 5, 6, 7];
const yPattern = [5, 10.2, 13.45, 15.4, 16.7, 17.35, 18];
const diffX = destination[0] - source[0];
const diffY = destination[1] - source[1];
const stepX = diffX / (steps - 1);
const ratio = (xValues.length - 1) / (steps - 1);
const points = [];
for (let i = 0; i < steps; i++) {
const xIndex = Math.floor(i * ratio);
const yIndex = Math.min(xIndex + 1, xValues.length - 1);
const x = source[0] + stepX * i;
const y = yPattern[xIndex] + (yPattern[yIndex] - yPattern[xIndex]) * (i * ratio - xIndex);
points.push([x, y]);
}
return points;
}
function calcValues(min, max, steps) {
const source = [1, min];
const destination = [7, max];
return traverseGraph(source, destination, steps);
}
console.log(calcValues(5, 18, 20))
I tested the output coming from the program and the equation for the same (x,y) pairs and again the difference is very minimal. So, I am not sure how much this has helped you but you can give it a try at least from your side using the approaches shared above.
PS - You can use this link to test the output from the equation.