2017-05-29 255 views
1

考慮線路上的複雜的數學函數[1,15]: F(X)=的sin(x/5)* EXP(X/10)+ 5 * EXP(-x/2)如何解決Python中的函數逼近任務?

enter image description here

度數n(w_0 + w_1x + w_2x^2 + ... + w_nx^n)的多項式由其通過的任何n + 1個不同點唯一地定義。 這意味着其係數w_0,... w_n可以從線性方程組的以下系統來確定:

enter image description here

凡X_1,...,x_n,X_ {N + 1}是點(x_1),...,f(x_n),f(x_ {n + 1}) - 在這些點必須採取的值。

我想要爲第三次多項式形成一個線性方程組(即指定係數矩陣A和自由向量b)的系統,它必須與點1,4上的函數f重合,10和15.使用scipy.linalg.solve函數解決這個系統。

A = numpy.array([[1.,1,1,1,1],[1.,4.8,64。],[1.,10.10,100,1000 ],[1,15,225,3375。]])

V = numpy.array(3.25,1.74,2.50,0.63])

numpy.linalg.solve(A, V)

我答錯了,這是enter image description here

因此問題是:是矩陣正確的嗎?

+0

爲點1,4,10和15(對不起,我不知道如何編輯的通訊,因此在一處山坳顯示) W0 + w1 * 1 + w2 * 1^2 + w3 * 1^3 = sin(1/5)* exp(1/10)+ 5 * exp(-1/2) w0 + w1 * 4 + w2 * 4^2 + w3 * 4^3 = sin(4/5)* exp(4/10)+ 5 * exp(-4/2) w0 + w1 * 10 + w2 * 10^2 + w3 * 10^3 = sin(10/5)* exp(10/10)+ 5 * exp(-10/2) w0 + w1 * 15 + w2 * 15^2 + w3 * 15^3 = 5)* exp(15/10)+ 5 * exp(-15/2) 方程 系統: W0 + W1 * 1 + W2 * 1^2 + W3 * 1^3 = 3.25 W0 + W1 * 4 + W2 * 4^2 + W3 * 4^3 = 1.74 w0 + w1 * 10 + w2 * 10^2 + w3 * 10^3 = 2.50 w0 + w1 * 15 + w2 * 15^2 + w3 * 15^3 = 0.63 – plywoods

回答

3

不,你的矩陣不正確。

最大的錯誤是你的第二個子矩陣A。第三項應該是4**2這是16,但你有8.不太重要,你只有兩個小數位爲常量數組V,但你真的應該有更多的精度。線性方程組有時對提供的值非常敏感,所以儘可能精確地使它們變得精確。另外,最後三項中的四捨五入是很糟糕的:你倒過來了,但你應該四捨五入。如果你真的想保留兩位小數(我不推薦)的值應該是

V = numpy.array([3.25, 1.75, 2.51, 0.64]) 

但更好的是

V = numpy.array([3.252216865271419, 1.7468459495903677, 
       2.5054164070002463, 0.6352214195786656]) 

這些變化對AV我得到的結果

array([ 4.36264154, -1.29552587, 0.19333685, -0.00823565]) 

我得到這兩個sympy圖,第一個顯示你的原始功能,第二個顯示你的原始功能,第二個使用近似的三次多項式。

enter image description here

enter image description here

他們看起來接近我!當我計算1,4,10和15的函數值時,最大絕對誤差爲15,即-4.57042132584462e-6。這比我預期的要大一些,但可能已經足夠好了。

+0

Roy,非常感謝!我真的很感謝你的幫助!你如何做得這麼快? :) – plywoods

+1

@膠合板:不客氣。我在發佈後不到一分鐘就看到了您的問題,並且我已將我的Python副本(在Spyder中)啓動並運行。此外,我現在通過本書*用Python做數學來學習sympy,所以sympy的繪圖機制在我心中是新鮮的。 –

+0

謝謝!感謝你的幫助! – plywoods

0

它來自數據科學課程嗎? :) 這裏幾乎是一個通用的解決方案我所做的:

%matplotlib inline 

import numpy as np; 
import math; 
import matplotlib.pyplot as plt; 

def f(x): 
    return np.sin(x/5) * np.exp(x/10) + 5 * np.exp(-x/2) 

# approximate at the given points (feel free to experiment: change/add/remove) 
points = np.array([1, 4, 10, 15]) 
n = points.size 

# fill A-matrix, each row is 1 or xi^0, xi^1, xi^2, xi^3 .. xi^n 
A = np.zeros((n, n)) 
for index in range(0, n): 
    A[index] = np.power(np.full(n, points[index]), np.arange(0, n, 1)) 

# fill b-matrix, i.e. function value at the given points 
b = f(points) 

# solve to get approximation polynomial coefficents 
solve = np.linalg.solve(A,b) 

# define the polynome approximation of the function 
def polinom(x): 
    # Yi = solve * Xi where Xi = x^i 
    tiles = np.tile(x, (n, 1)) 
    tiles[0] = np.ones(x.size) 
    for index in range(1, n): 
     tiles[index] = tiles[index]**index 
    return solve.dot(tiles) 

# plot the graphs of original function and its approximation 
x = np.linspace(1, 15, 100) 
plt.plot(x, f(x)) 
plt.plot(x, polinom(x)) 

# print out the coefficients of polynome approximating our function 
print(solve) 
+0

如果有人知道如何改進python代碼,請讓我知道,我會更新答案。我不太喜歡A和polinom(x)是如何定義的,但我還沒有找到更好的東西。 –