4. Turn the algorithm into MATLAB statements The final MATLAB program is as follows: Purpose: To perform a least-squares fit of an input data set to a straight line, and print out the resulting slope and intercept values. The input data for this fit comes from a user-specified input data file.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

5.14

Program lsqfit from Example 5.6 required you to specify the number of input
data points before entering the values. Modify the program so that it reads an
arbitrary number of data values using a while loop, and stops reading input
values when the user presses the Enter key without typing any values. Test your program using the same two data sets that were used in Example 5.6. (Hint: The
input function returns an empty array ([]) if you press “Enter” without supplying any data. You can use function isempty to test for an empty array, and
stop reading data when one is detected.) 

Using MATLAB

&
응
응
응
| Chapter 5 Loops and Vectorization
%
%
%
%
%
%
%
%
%
&
&
&
&
%
&
%
Purpose:
To perform a least-squares fit of an input data set
to a straight line, and print out the resulting slope
and intercept values. The input data for this fit
comes from a user-specified input data file.
Record of revisions:
Date
4. Turn the algorithm into MATLAB statements
The final MATLAB program is as follows:
end
====
01/30/18 S. J. Chapman
Define variables:
ii
n_points
slope
sum_x
sum x2
sum_xy
sum_y
temp
X
x_bar
y
y_bar
y_int
=
--
--
Programmer
==========
--
--
--
-- Sum of all input x values
--
Sum of all input x values squared
Sum of all input x*y yalues
Sum of all input y values
Variable to read user input
Array of x values.
Average x value
Array of y values
Average y value
y-axis intercept of the line.
--
--
Description of change
Loop index
Number in input [x y] points
Slope of the line
disp('This program performs a least-squares fit of an');
disp('input data set to a straight line.');
n_points
input('Enter the number of input [x y points: ');
Read the input data
1:n_points
==‒‒‒‒‒‒
Original code
for ii
temp = input ('Enter [x y] pair: ');
x(ii)
temp (1);
y (ii) = temp (2);
Transcribed Image Text:& 응 응 응 | Chapter 5 Loops and Vectorization % % % % % % % % % & & & & % & % Purpose: To perform a least-squares fit of an input data set to a straight line, and print out the resulting slope and intercept values. The input data for this fit comes from a user-specified input data file. Record of revisions: Date 4. Turn the algorithm into MATLAB statements The final MATLAB program is as follows: end ==== 01/30/18 S. J. Chapman Define variables: ii n_points slope sum_x sum x2 sum_xy sum_y temp X x_bar y y_bar y_int = -- -- Programmer ========== -- -- -- -- Sum of all input x values -- Sum of all input x values squared Sum of all input x*y yalues Sum of all input y values Variable to read user input Array of x values. Average x value Array of y values Average y value y-axis intercept of the line. -- -- Description of change Loop index Number in input [x y] points Slope of the line disp('This program performs a least-squares fit of an'); disp('input data set to a straight line.'); n_points input('Enter the number of input [x y points: '); Read the input data 1:n_points ==‒‒‒‒‒‒ Original code for ii temp = input ('Enter [x y] pair: '); x(ii) temp (1); y (ii) = temp (2);
Accumulate statistics
sum_x = 0;
sum_y = 0;
sum_x2 = 0;
sum_xy = 0;
for ii = 1:n_points
end
sum_x = sum_x + x(ii);
sum_y = sum_y + y(ii);
sum x2 = sum_x2 + x(ii)^2;
sum_xy = sum_xy + x(ii) * y(ii);
Now calculate the slope and intercept.
x_bar = sum_x / n_points;
y bar = sum_y / n_points;
slope
y_int =
(sum_xy
sum_x * y_bar) / (sum_x2
y_bar slope * x_bar;
Tell user.
disp('Regression coefficients for the least-squares line: ');
= $8.3f\n', slope);
fprintf( Slope (m)
fprintf(
Intercept (b) = 8.3f\n', y_int);
fprintf(' No. of points = %8d\n', n_points);
Create the fitted line.
xmin = min(x);
xmax = max (x);
ymin = slope* xmin+y_int;
ymax = slope xmax+y_int;
sum_x * x_bar);
Plot the data points as blue circles with no
connecting lines.
plot (x, y, 'bo');
hold on;
5.5 Additional Examples | 239
Add a title and legend
title ('\bfLeast-Squares Fit');
Plot a solid red line with no markers
plot ( [xmin xmax], [ymin ymax], 'r-', 'LineWidth', 2);
hold off;
xlabel('\bf\itx');
ylabel('\bf\ity');
legend (Input data', 'Fitted line');
grid on
Transcribed Image Text:Accumulate statistics sum_x = 0; sum_y = 0; sum_x2 = 0; sum_xy = 0; for ii = 1:n_points end sum_x = sum_x + x(ii); sum_y = sum_y + y(ii); sum x2 = sum_x2 + x(ii)^2; sum_xy = sum_xy + x(ii) * y(ii); Now calculate the slope and intercept. x_bar = sum_x / n_points; y bar = sum_y / n_points; slope y_int = (sum_xy sum_x * y_bar) / (sum_x2 y_bar slope * x_bar; Tell user. disp('Regression coefficients for the least-squares line: '); = $8.3f\n', slope); fprintf( Slope (m) fprintf( Intercept (b) = 8.3f\n', y_int); fprintf(' No. of points = %8d\n', n_points); Create the fitted line. xmin = min(x); xmax = max (x); ymin = slope* xmin+y_int; ymax = slope xmax+y_int; sum_x * x_bar); Plot the data points as blue circles with no connecting lines. plot (x, y, 'bo'); hold on; 5.5 Additional Examples | 239 Add a title and legend title ('\bfLeast-Squares Fit'); Plot a solid red line with no markers plot ( [xmin xmax], [ymin ymax], 'r-', 'LineWidth', 2); hold off; xlabel('\bf\itx'); ylabel('\bf\ity'); legend (Input data', 'Fitted line'); grid on
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 8 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY