%% MITpolyfitexa.m
%
% * Evaluate y=x^2 for x=-4:0.1:4.
% * Add random noise to these samples. Use randn. Plot the 
% noisy signal with . markers
% * Fit a 2nd degree polynomial to the noisy data
% * Plot the fitted polynomial on the same plot, using the same 
% x values and a red line
%%
x=-4:.1:4;
y=x.^2;
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
defaultStream.State = savedState;
%%
defaultStream.State = savedState;
randn(1,6)
defaultStream.State = savedState;
randn(1,16)
ynormal=randn(1,1000);
plot(ynormal,'x');shg
hist(ynormal,20);shg
%%
clg
ynoise=y+randn(size(y));
plot(x,ynoise,'O');shg
hold on
plot(x,y,'r')
%%
c=polyfit(x,ynoise,2)
z=polyval(c,x);
plot(x,z,'k')
grid on
%%
max(abs(y-z))


          