如何在Python中将参数方程式拟合到数据点 [英] How to fit parametric equations to data points in Python

查看:331
本文介绍了如何在Python中将参数方程式拟合到数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将
参数方程组拟合到一组方法。

I am looking for a way to fit parametric equations to a set of data points, using Python.

作为一个简单的示例,给出了以下数据点集:

As a simple example, given is the following set of data points:

import numpy as np
x_data = np.array([1, 2, 3, 4, 5])
y_data = np.array([2, 0, 3, 7, 13])

使用 t 作为参数,我想将以下参数方程式拟合到数据点,

Using t as the parameter, I want to fit the following parametric equation to the data points,

t = np.arange(0, 5, 0.1)
x = a1*t + b1
y = a2*t**2 + b2*t + c2

也就是说,让Python查找系数 a1 b1 的值, a2 b2 c2 $ c>(x,y)最适合数据点(x_data,y_data)

that is, have Python find the values for the coefficients a1, b1, a2, b2, c2 that fits (x,y) best to the data points (x_data, y_data).

请注意,上述 y(t) x(t)函数仅作为以下示例参数方程。我想要适合我的数据的实际函数要复杂得多,在这些函数中,将 y 表示为的函数并不容易。 x

Note that the y(t) and x(t) functions above only serve as examples of parametric equations. The actual functions I want to fit my data to are much more complex, and in those functions, it is not trivial to express y as a function of x.

我们将不胜感激-谢谢!

Help will be appreciated - thank you!

推荐答案

您可以使用polyfit,但请注意t的长度必须与数据点的长度匹配

You can use polyfit, but please take care that the length of t must match the length of data points

import numpy as np

tt = np.linspace(0, 5, len(x_data))

x_params = np.polyfit(tt, x_data, 1)
y_params = np.polyfit(tt, y_data, 2)

将第三个参数更改为

要使用该功能,您可以使用

To get the function you can use

y = np.poly1d(y_params)

t = np.arange(0, 5, 0.1)

plot(t, y(t))
plot(tt, y_data, 'o')

这篇关于如何在Python中将参数方程式拟合到数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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