% Test mreg.m with polynomial fit data % Calculate LSQ straight line and quadratic, % use ANOVA to see if quadratic is statistically % better than the straight line fit. % Testmreg.m L Braile 2/28/05 % enter t and y data t = [0 .3 .8 1.1 1.6 2.3]'; y = [0.5 0.82 1.14 1.25 1.35 1.40]'; % set up the function of the form y = a0 + a1*t +a2*t^2 % the a0 term will be determined later, data normalized % by mreg by means X = [t t.^2 y]; % set up X matrix for mreg [b, SR, Tot] = mreg(X); % mreg (mult. linear regression % function is used for ANOVA) t1 = [0:0.1:3]; % set t values for plotting LSQ lines p1 = polyfit(t,y,1); % linear fit p2 = polyfit(t,y,2); % quadratic fit y1 = polyval(p1,t1); % evaluate linear fit y2 = polyval(p2,t1); % evaluate quadratic fit plot(t,y,'or','markersize',12,'linewidth',3) % plot data points set(gca,'fontsize',16,'linewidth',2) hold on plot(t1,y1,'-b','linewidth',2) % plot % least squares lines plot(t1,y2,'-k','linewidth',2) xlabel('t-values','fontsize',16) ylabel('y-values','fontsize',16) title('Plot sample data, straight line and quadratic fits','fontsize',16) hold off