如何通过已知点数组对某些数据点中的值进行插值? [英] How do I interpolate the value in certain data point by array of known points?

查看:41
本文介绍了如何通过已知点数组对某些数据点中的值进行插值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道数据点及其值,描述为数组, speed power

I have known data points and their values described as arrays, speed and power

speed = [2, 6, 8, 10, 12]
power = [200, 450, 500, 645, 820],

我想估计与未在源数组(2到12)中列出的速度值(例如7)相对应的功率值.

I want to estimate the power value that corresponds to speed value (e.g. 7) that is not listed within source array (2 through 12).

即我正在寻找一种执行插值的方法

I.e. I am seeking a way to perform interpolation

推荐答案

最简单的方法是将功率对速度的依赖关系视为线的集合,并假设目标点位于两个已知点之间.一定的行,计算值.

The simplest way to do that is to perceive your power-from-speed dependency as a collection of lines and assuming your target point falls between two known points of a certain line, calculate the value.

例如您可以执行以下操作:

E.g. you may do the following:

const speed = [2, 6, 8, 10, 12], power = [200, 450, 500, 645, 820]
		
const interpolate = (xarr, yarr, xpoint) => {
	const	xa = [...xarr].reverse().find(x => x<=xpoint),
			xb = xarr.find(x => x>= xpoint),      
			ya = yarr[xarr.indexOf(xa)],      
			yb =yarr[xarr.indexOf(xb)]  
	return yarr[xarr.indexOf(xpoint)] || ya+(xpoint-xa)*(yb-ya)/(xb-xa)
}

console.log(interpolate(speed,power,7))

.as-console-wrapper{min-height:100%}

这篇关于如何通过已知点数组对某些数据点中的值进行插值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆