temos uma tabela de pontos: $$ \begin{array}{|c|cccc|}\hline x & 1 & 2 & 4 & 10 \\ \hline y & -2 & 6 & 0 & 10\\ \hline \end{array}$$ e queremos encontrar o polinômio cúbico que passa por estes quatro pontos $p(x) =a_3x^3 + a_2x^2 + a_1x +a_0$ Uma forma para se fazer é usar as condições de ajuste do polinômio em cada ponto da tabela: $$\begin{gather*} p(1) =a_3(1^3) + a_2(1^2) + a_1(1) +a_0.1=-2 \\ p(2) =a_3(2^3) + a_2(2^2) + a_1(2) +a_0.1= 6 \\ p(4) =a_3(4^3) + a_2(4^2) + a_1(4) +a_0.1= 0\\ p(10) =a_3(10^3) + a_2(10^2) + a_1(10) +a_0.1=10 \end{gather*}$$
Este sistema podemos resolver escrevendo na forma matricial:
$$ \begin{pmatrix} 1 & 1& 1^2 & 1^3\\ 1 & 2&2^2 & 2^3 \\ 1 & 4 & 4^2 & 4^3 \\ 1 & 10 & 10^2 & 10^3 \end{pmatrix} \begin{pmatrix} a_0 \\ a_1 \\ a_2 \\ a_3\end{pmatrix}= \begin{pmatrix} -2 \\ 6 \\ 0 \\ 10 \end{pmatrix} $$que é um sistema com uma matriz de Vandermonde.
In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
%matplotlib inline
In [5]:
x=np.array([1, 2, 4, 10])
In [6]:
y=np.array([-2,6,0,10])
In [9]:
plt.plot(x,y, "ro")
plt.grid()
In [13]:
A=np.array([[1,1,1,1],
[1,2,4,8],
[1,4,16,64],
[1, 10,100,1000]])
In [14]:
A
Out[14]:
In [20]:
Ainv=np.linalg.inv(A)
In [38]:
a=np.dot(Ainv,y)
In [39]:
p = lambda x : a[3]*x**3 + a[2]*x**2 + a[1]*x + a[0]
In [42]:
t = np.linspace(0,10,100)
In [50]:
plt.plot(t,p(t))
plt.plot(x,y,"ro")
plt.grid()
In [ ]: