Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

and lqsfit.m contents that needs to be modified is as follows: disp(\'This progr

ID: 651349 • Letter: A

Question

and lqsfit.m contents that needs to be modified is as follows:

disp('This program performs a least-square fit of an');
disp('input data set to a straight line ');
n_points=input('Enter the number of input points:');
% Read the input data
x=zeros(1,n_points);%preallocate x and y
y=zeros(1,n_points);
for ii=1:n_points
temp=input('Enter [x y] pair: (in brackets) ');
x(ii)=temp(1);
y(ii)=temp(2);
end
%Accumulate Statistics
sum_x =0; %Initialize all sums
sum_x2=0;
sum_y =0;
sum_xy=0;
for ii=1:n_points %Calulate sums using a for loop
sum_x=sum_x+x(ii);
sum_x2=sum_x2+x(ii)^2;
sum_y=sum_y+y(ii);
sum_xy=sum_xy+x(ii)*y(ii);
end

x_bar=sum_x/n_points;
y_bar=sum_y/n_points;
slope=(sum_xy-sum_x*y_bar)/(sum_x2-sum_x*x_bar);
y_int=y_bar-slope*x_bar;
%Tell user
disp('Regression coefficients for the least square line:');
fprintf('Slope (m) =%8.3f ',slope)
fprintf('Intercept (b) =%8.3f ',y_int)
fprintf('No of Points =%8d ',n_points)
%Plot the data points as b;lue circles with no connecting lines
plot(x,y,'bo');
hold on;
%Create line
xmin=min(x); %find smallest data value
xmax=max(x); %find largest data value
ymin=slope*xmin+y_int; %Find corresponding y
ymax=slope*xmax+y_int;%Find corresponding y

%Plot a solid red line with no markers
plot([xmin xmax],[ymin ymax],'r-','Linewidth',2);
hold off;
title('Least-Squares fit');
xlabel('fitx');
ylabel('fity');
legend('input data','fitted line');
grid on;

Modify Iqsfit example seen in lecture 8 to make a function. Take the example given to you in lecture 8 and modify it to create a function to calculate the least square fit (lqsfit.m) and a program that outputs the plots. The function should take in two vectors x and y as input, and output the slope and intercept The caller program (or main program) should first ask the user to input x and y, then call the function and then plot the regression line along with the original data points as in the example from lecture 8.

Explanation / Answer

disp('This program performs a least-square fit of an');
disp('input data set to a straight line ');
n_points=input('Enter the number of input points:');
% Read the input data
x=zeros(1,n_points);%preallocate x and y
y=zeros(1,n_points);
for ii=1:n_points
temp=input('Enter [x y] pair: (in brackets) ');
x(ii)=temp(1);
y(ii)=temp(2);
end
%Accumulate Statistics
sum_x =0; %Initialize all sums
sum_x2=0;
sum_y =0;
sum_xy=0;
for ii=1:n_points %Calulate sums using a for loop
sum_x=sum_x+x(ii);
sum_x2=sum_x2+x(ii)^2;
sum_y=sum_y+y(ii);
sum_xy=sum_xy+x(ii)*y(ii);
end

x_bar=sum_x/n_points;
y_bar=sum_y/n_points;
slope=(sum_xy-sum_x*y_bar)/(sum_x2-sum_x*x_bar);
y_int=y_bar-slope*x_bar;
%Tell user
disp('Regression coefficients for the least square line:');
fprintf('Slope (m) =%8.3f ',slope)
fprintf('Intercept (b) =%8.3f ',y_int)
fprintf('No of Points =%8d ',n_points)
%Plot the data points as b;lue circles with no connecting lines
plot(x,y,'bo');
hold on;
%Create line
xmin=min(x);
xmax=max(x);
ymin=slope*xmin+y_int;
ymax=slope*xmax+y_int;

%Plot a solid red line with no markers
plot([xmin xmax],[ymin ymax],'r-','Linewidth',2);
hold off;
title('Least-Squares fit');
xlabel('fitx');
ylabel('fity');
legend('input data','fitted line');
grid on;