Using the least square fit for linear regression, the required
straight line fit for the given data is obtained using MATLAB. The
required mass vs fuel consumption graph is also plotted and the
standard deviation is computed as well. All the results are
attached below: (MATLAB codes are also attached)
MATLAB code:
function linear_fit(phi,M)
M1=sum(M);
M2=sum(M.^2);
phi1=sum(phi);
phi1M1=sum(phi.*M);
n=numel(M);
A=[n M1;M1 M2];
b=[phi1;phi1M1];
ab=A\b;
fprintf('\nRequired straight line fit is: phi=(%g)+(%g)
M\n\n',ab(1),ab(2));
f=@(ab,M) ab(1)+ab(2)*M;
x=min(M)-100:1:max(M)+100;
y=f(ab,x);
y1=f(ab,M);
y2=(y1-phi).^2;
SD=sqrt(sum(y2)/n);
fprintf('\nRequired standard deviation of the fit is:
%g\n\n',SD);
plot(x,y,M,y1,'o',M,phi,'*');
xlabel('Mass(kg)')
ylabel('Fuel consumption(km/litre)')
legend('Regression line','Data using regression line','Actual
data')
title('Mass vs Fuel consumption')
AA=[M' y1'];
VarNames = {'Mass','Fuel_consumption_from_regression_line'};
TT=table(AA(:,1),AA(:,2), 'VariableNames',VarNames);
disp(TT);
end
USE MATLAB FOR ALL ANSWERS of motor vehicles 2. The table displays the mass M and...