我從這個鏈接https://www.dcode.fr/lagrange-interpolating-polynomial實作了一個公式來計算坐標之間的某種分數。
結果值按預期作業,坐標如下
const coordinates = [
[0, 100],
[2.5, 70],
[10, 30],
]
其中 y 軸是偶數,但 y 值如 67、33 未按預期作業。
function getScore (thresholds, macro) {
let value = 0
for (let j = 0; j < thresholds.length; j ) {
let temp = 1
for (let i = 0; i < thresholds.length; i ) {
if (i !== j) {
temp *= (macro - thresholds[i][0]) / (thresholds[j][0] - thresholds[i][0])
}
}
value = thresholds[j][1] * temp
}
return value
}
console.log(
'Expecting Something above 33 but get 31',
getScore(
[
[0, 100],
[2.5, 66],
[10, 33],
],
9
)
)
console.log(
'Expecting Something above 30 but and got 31',
getScore(
[
[0, 100],
[2.5, 70],
[10, 30],
],
9
)
)
我的代碼弄錯了嗎?
謝謝,
uj5u.com熱心網友回復:
該演算法正在正常作業,盡管不像您期望的那樣。
該演算法將多項式擬合到這些點。如果你有3個點,它將是一條拋物線。因為它在前 2 個點上下降得如此之快,所以拋物線將在后兩個點之間具有最小值,因此給出的值低于您給出的數字。
如果這不是您想要的那種插值,我建議您使用非多項式。例如,您可以使用如下所示的加權平均值:
sum(point.y * f(x - point.x) for point in points)
/
sum(f(x - point.x) for point in points)
Makef(x)是一個f(x) = f(-x)在 0 處爆炸的函式。當然,如果你在那個點,只需輸入那個點的值。例如1/x^2。這將使您每個都接近附近點的合理平均值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346805.html
標籤:javascript 算法
上一篇:無交點連接二維多邊形的演算法
下一篇:python中的閾值總和
