簡體   English   中英

從繪圖/二維數組中找到准確的數學函數?

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

我將如何從圖形/二維數組中找到數學函數? 例如

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

對多項式、指數和介於兩者之間的所有事物,依此類推。

我了解某些行可能沒有函數**,或者我可能必須將其分解為范圍以獲得與函數非常相似的任何內容,但我想不出如何做到這一點,除了蠻力但那個劑量似乎不可靠。

**沒有功能? 有點道理,有一個函數可以描述每條可能的線/曲線,對吧?

對於線性方程,您可以簡單地使用 NumPy 的 polyfit。 假設您提到的線陣列的值 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}"

測試:

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

print(fit_to_function(x, y))

輸出:

y = 1.9999999999999998x + 5.000000000000003

可選:如果您想像原來的問題一樣對其進行四舍五入

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}"

輸出:

y = 2.0x + 5.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM