clc
clear all
close all

%% APROXIMAÇÃO

x = [0.5:0.05:0.8]'
y = [1.2 1 0.7+0.5 0.4+0.5 0.1+0.5 -0.2 -0.6]'

A = [ ones(7,1) x x.^2 ]

c = inv(A'*A)*A'*y

plot(x,y,'bo');
hold on;
plot(x,c(1)+c(2)*x+c(3)*x.^2,'r');

%% APROXIMAÇÃO (nivel avançado)

##% generate test data
##x = [0.1:0.1:0.8]';
##y = [0.6 1.1 1.6 1.8 2 1.9 1.7 1.3]';
##
##a_0 = [1; 2];
##dFda = [1; 1];
##
##%fcn = @(x) a(1)*sin(a(2).*x)
##
##y_fit = a_0(1)*sin(a_0(2).*x);
##squares_0 = sum((y_fit-y).^2);
##
##for i = 1:100
##     a = a_0 - 0.1*dFda;
##
##     y_fit = a(1)*sin(a(2).*x);
##
##     squares = sum((y_fit-y).^2);
##
##     dFda = [(squares-squares_0)/(a(1)-a_0(1)); (squares-squares_0)/(a(2)-a_0(2))]; % ESTIMATIVA DO GRADIENT
##
##     squares_0 = squares;
##     a_0(1) = a(1);
##     a_0(2) = a(2);
##
##     plot(x,y,'bo');
##     hold on;
##     plot(x,y_fit,'r');
##     hold off;
##     pause(0.05);
##end


