2017-05-29 260 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)是如何定义的,但我还没有找到更好的东西。 –