简体   繁体   English

从绘图/二维数组中找到准确的数学函数?

[英]Finding an accurate mathmatical function from a plot/ 2d array?

How would i go about finding the mathmatical function from a graphical plot/ 2d array?我将如何从图形/二维数组中找到数学函数? eg例如

line = [0,1,2,3,4,5,6,7,8,9,10] 
print(findfunction(line))
>y=x

line2 =[5,7,9,11,13,15,17,19] 
print(findfunction(line2))
>y=2x+5

And so on for ploynomials, exponentials and everything in between.对多项式、指数和介于两者之间的所有事物,依此类推。

I understand for some lines there either may be no function**, or i may have to break it down into ranges to get anything that closely resembals a function but i can't think of how to do this, aside brute force but that dosen't seem reliable.我了解某些行可能没有函数**,或者我可能必须将其分解为范围以获得与函数非常相似的任何内容,但我想不出如何做到这一点,除了蛮力但那个剂量似乎不可靠。

**no function? **没有功能? kinda makes sense that there is a function to describe every possible line/ curve, right?有点道理,有一个函数可以描述每条可能的线/曲线,对吧?

For linear equations, you could simply use NumPy's polyfit.对于线性方程,您可以简单地使用 NumPy 的 polyfit。 Assuming the line arrays you mentioned have values x = 1,2,3,4... we could do the following假设您提到的线阵列的值 x = 1,2,3,4... 我们可以执行以下操作

def fit_to_function(x, y):
    """
    fit_to_function(x, y)
    x: list of x values
    y: list of y values
    returns: list of y values that fit the function
    """
    x, b = np.polyfit(x, y, 1)
    return f"y = {x}x + {b}"

Testing:测试:

x = [0,1,2,3,4,5,6,7]
y = [5,7,9,11,13,15,17,19]

print(fit_to_function(x, y))

Output:输出:

y = 1.9999999999999998x + 5.000000000000003

Optional: if you want to round it like in your original question可选:如果您想像原来的问题一样对其进行四舍五入

import numpy as np

def fit_to_function(x, y):
    """
    fit_to_function(x, y)
    x: list of x values
    y: list of y values
    returns: list of y values that fit the function
    """
    x, b = np.polyfit(x, y, 1)
    x, b = round(x, 2), round(b, 2)
    return f"y = {x}x + {b}"

Output:输出:

y = 2.0x + 5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM