Métodos dos minimos quadrados

$ f(x) = c_0 * f_0(x) + c_1 * f_1(x) + ... + c_n * f_n(x) $

A = | $f_0(x_0)$ $f_1(x_0)$ ... $f_n(x_0)$ |
| $f_0(x_1)$ $f_1(x_1)$ ... $f_n(x_1)$ |
| ... ... ... ... |
| $f_0(x_n)$ $f_1(x_n)$ ... $f_n(x_n)$ |

C = |C0|
|C1|
|...|
|Cn|

Y = |Y0|
|Y1|
|...|
|Yn|


In [2]:
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

In [6]:
def f0(x):
    return x

def f1(x):
    return 1

f = np.array([[f0], [f1]])
def F(C, x):
    ans = 0
    for i in range(C.shape[0]):
        ans += C[i] * f[i](x)
    return ans

In [11]:
# definir pontos
x = np.linspace(0, 10, 100).reshape(100, 1)
y = x + 5 * np.random.random((100,))
plt.plot(x[0], y[0], 'o')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-e4d10c06ebc6> in <module>()
      2 x = np.linspace(0, 10, 100).reshape(100, 1)
      3 y = x + 5 * np.random.random((100,))
----> 4 plt.plot(x[0], y[0], 'o')

/Users/DaniloBarros/.virtualenvs/metodos/lib/python2.7/site-packages/matplotlib/pyplot.pyc in plot(*args, **kwargs)
   3316                       mplDeprecation)
   3317     try:
-> 3318         ret = ax.plot(*args, **kwargs)
   3319     finally:
   3320         ax._hold = washold

/Users/DaniloBarros/.virtualenvs/metodos/lib/python2.7/site-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1890                     warnings.warn(msg % (label_namer, func.__name__),
   1891                                   RuntimeWarning, stacklevel=2)
-> 1892             return func(ax, *args, **kwargs)
   1893         pre_doc = inner.__doc__
   1894         if pre_doc is None:

/Users/DaniloBarros/.virtualenvs/metodos/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in plot(self, *args, **kwargs)
   1404         kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
   1405 
-> 1406         for line in self._get_lines(*args, **kwargs):
   1407             self.add_line(line)
   1408             lines.append(line)

/Users/DaniloBarros/.virtualenvs/metodos/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in _grab_next_args(self, *args, **kwargs)
    405                 return
    406             if len(remaining) <= 3:
--> 407                 for seg in self._plot_args(remaining, kwargs):
    408                     yield seg
    409                 return

/Users/DaniloBarros/.virtualenvs/metodos/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in _plot_args(self, tup, kwargs)
    383             x, y = index_of(tup[-1])
    384 
--> 385         x, y = self._xy_from_xy(x, y)
    386 
    387         if self.command == 'plot':

/Users/DaniloBarros/.virtualenvs/metodos/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in _xy_from_xy(self, x, y)
    242         if x.shape[0] != y.shape[0]:
    243             raise ValueError("x and y must have same first dimension, but "
--> 244                              "have shapes {} and {}".format(x.shape, y.shape))
    245         if x.ndim > 2 or y.ndim > 2:
    246             raise ValueError("x and y can be no greater than 2-D, but have "

ValueError: x and y must have same first dimension, but have shapes (1,) and (100,)

In [12]:
A = np.ones((x.shape[0], f.shape[0]))

for i in range(x.shape[0]):
    for j in range(f.shape[0]):
        A[i][j] = f[j](x[i])


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-11a76c834fca> in <module>()
      3 for i in range(x.shape[0]):
      4     for j in range(f.shape[0]):
----> 5         A[i][j] = f[j](x[i])

TypeError: 'numpy.ndarray' object is not callable

In [10]:
C = np.array([1, 0])
x = np.linspace(0, 10, 100)
y = F(C, x)
plt.plot(x, y)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-b67e1def2ea9> in <module>()
      1 C = np.array([1, 0])
      2 x = np.linspace(0, 10, 100)
----> 3 y = F(C, x)
      4 plt.plot(x, y)

<ipython-input-6-84183f70b5fa> in F(C, x)
      9     ans = 0
     10     for i in range(C.shape[0]):
---> 11         ans += C[i] * f[i](x)
     12     return ans

TypeError: 'numpy.ndarray' object is not callable

In [ ]: