%LeastSqFit.m (linear)

%Input experimental data

%y axis data
y=zeros(6,1);
y(1)=0.0;%lbs
y(2)=5;%lbs
y(3)=12;%lbs
y(4)=18;%lbs
y(5)=25;%lbs
y(6)=32;%lbs

%x axis data
x=zeros(6,1);
x(1)=0.0;%inches
x(2)=0.01;%inches
x(3)=0.02;
x(4)=0.028;
x(5)=0.039;
x(6)=0.052;

%Plot experimental load vs displacement
plot(x,y,'ko')
ylabel('Load, F (lbs)','fontsize',14)
xlabel('Displacement, \Delta (inches)','fontsize',14)
title('Testing of steel spring, F=K\Delta','fontsize',14)
grid on

%Find least squares fit of the data
p=polyfit(x,y,1);
x1=[min(x);max(x)];
y1=p(1)*x1+p(2);

hold on
%Plot the least squares fit line
plot(x1,y1,'b')
%Create legend
legend('Experimental','Least Squares Fit','location','southeast')

Eexp=strcat("K_{experimental fit}=",num2str(round(p(1)))," lbs/in");
text(0.005,25,Eexp,'fontsize',14);
text(0.005,21,"K_{theoretical}=700 lbs/in",'fontsize',14);
