%% Part 1
B0 = [.1,.2,.3,.4]; % wild guess

B_best = fminsearch('J9',B0)

x = [1,2, 3, 4, 5, 6, 7, 8, 9, 10];
y = [18, 5, 17, 38, 40, 106, 188, 234, 344, 484];

figure
plot(x,y,'bs','markersize',15)
xlabel('X'); ylabel('Y');
hold on

yhat = B_best(1) + ...
    B_best(2)*x + ...
    B_best(3)*(x.*x) + ...
    B_best(4)*(x.*x.*x);

plot(x,yhat,'ro-','markersize',15)

%% Part 2
clear
x = 0:0.01:3;
y = sin(2*pi*x) + randn(size(x))*0.5;
figure
plot(x,y,'bs')
xlabel('X'); ylabel('Y')

J9b = @(b) sum((sin(b*x) - y).^2);
beta = -10:.1:10;
J = zeros(1,length(beta));
for i=1:length(beta)
    J(i) = J9b(beta(i));
end
figure
plot(beta, J, '-')
xlabel('beta'); ylabel('J(beta)')











