In: Computer Science
9.) Write a MATLAB script that will read the Pressure and Temperature columns from the provided Excel file. Use a loop to calculate the linear best fit line for the data. You may only use the built-in functions sum, size, and length. Plot both the individual data points (red) and the fit line (blue) on the same graph. Include a title and axes labels.
clc
clear all
close all
format long
P=[1,5,10,15,20:5:50];
T=[300,315,330,345,360,375,390,405,420,435,450];
Sx=0;
Sx2=0;
Sy=0;
n=length(P);
Sxy=0;
for i=1:n
Sx=Sx+P(i);
Sy=Sy+T(i);
Sxy=Sxy+P(i)*T(i);
Sx2=Sx2+P(i)^2;
end
a=(Sy*Sx2-Sx*Sxy)/(n*Sx2-Sx^2);
b=(n*Sxy-Sx*Sy)/(n*Sx2-Sx^2);
fprintf('Best fit line is T=%f*x+%f\n',b,a);
C=[b,a];
xx=0:0.01:50;
plot(xx,polyval(C,xx),'b',P,T,'or');
xlabel('X');
xlabel('Y');
title('Plot of f and fit');