本文介绍了在3维中使用np.polyfit拟合多项式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据数组,其中某些整数 N 的维度为(N,3),用于指定3D空间(即每一行)中粒子的轨迹项是粒子的(x,y,z)坐标.该轨迹平滑且简单,我希望能够将多项式拟合到该数据.

I have an array of data, with dimensions (N,3) for some integer N, that specifies the trajectory of a particle in 3D space, i.e. each row entry is the (x,y,z) coordinates of the particle. This trajectory is smooth and uncomplicated and I want to be able to fit a polynomial to this data.

我可以使用 np.polyfit :

I can do this with just the (x,y) coordinates using np.polyfit:

import numpy as np

#Load the data
some_file = 'import_file.txt'

data = np.loadtxt(some_file)
x = data[:,0]
y = data[:,1]

#Fit a 4th order polynomial
fit = np.polyfit(x,y,4)

这给了我多项式的系数,没有问题.

This gives me the coefficients of the polynomial, no problems.

当我想要一个描述 x,y,z 坐标的多项式时,该如何扩展到我的情况?

How would I extend this to my case where I want a polynomial which describes the x,y,z coordinates?

推荐答案

您在这里有几个选择.首先,让我们扩展您的2D情况fit = np.polyfit(x,y,4).这意味着您将粒子的 y 位置描述为 x 的函数.只要它不会在 x 中移回就可以了. (即,每个 x 只能具有唯一的 y 值).由于空间中的运动被分解为三个独立的坐标,因此我们可以独立地拟合这些坐标以获得3D模型:

You have several options here. First, let's expand on your 2D case fit = np.polyfit(x,y,4). This means you describe the particle's y position as a function of x. This is fine as long it won't move back in x. (I.e. it can only have a unique y value for each x). Since movement in space is decomposed into three independent coordinates, we can fit the coordinates independently to get a 3D model:

fitxy = np.polyfit(x, y, 4)
fitxz = np.polyfit(x, z, 4)

现在 y z 都是 x 的多项式函数.如前所述,这样做的缺点是粒子只能在 x 中单调移动.

Now both y and z are a polynomial function of x. As mentioned before, this has the drawback that the particle can only move monotonuously in x.

一个真实的物理粒子不会表现出那样的行为.他们通常会在所有三个维度上弹跳,无论他们喜欢哪种方式.但是,在第四维度中,它们只能向前移动:时间.

A true physical particle won't behave like that. They usually bounce around in all three dimensions, going which ever way they please. However, there is a 4th dimension in which they only move forward: time.

因此,让我们添加时间:

So let's add time:

t = np.arange(data.shape[0])  # simple assumption that data was sampled in regular steps

fitx = np.polyfit(t, x, 4)
fity = np.polyfit(t, y, 4)
fitz = np.polyfit(t, z, 4)

现在,对粒子进行建模以使其随时间自由地在空间中自由移动.

Now the particle is modeled to move freely in space, as a function on time.

这篇关于在3维中使用np.polyfit拟合多项式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:23