0% found this document useful (0 votes)
3 views

Chapter6_Prob19

The document describes modifications to the MATLAB function LinearRegression to calculate linear coefficients and overall error for given data points. The new function, LinReg, takes two input vectors and outputs the coefficients and error value. Examples are provided to demonstrate the function's application with specific data sets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter6_Prob19

The document describes modifications to the MATLAB function LinearRegression to calculate linear coefficients and overall error for given data points. The new function, LinReg, takes two input vectors and outputs the coefficients and error value. Examples are provided to demonstrate the function's application with specific data sets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1

6.19 Modify the MATLAB user-defined function LinearRegression in Program 6-1. In addition to
determining the constants a 1 and a 0 , the modified function should also calculate the overall error E
according to Eq. (6.6). Name the function [a, Er] = LinReg(x,y). The input arguments x and y are
vectors with the coordinates of the data points. The output argument a is a two-element vector with the val-
ues of the constants a 1 and a 0 . The output argument Er is the value of the overall error.
(a) Use the function to solve Example 6-1.
(b) Use the function to solve Problem 6.2.
Solution
The listing of the user-defined function LinReg is:

function [a,Er] = LinReg(x, y)


% LinReg calculates the coefficients a1 and a0 of the linear
% equation y = a1*x + a0 that best fits n data points, and the overall
% error according to Eq. (5.6).
% Input variables:
% x A vector with the coordinates x of the data points.
% y A vector with the coordinates y of the data points.
% Output variables:
% a Two elements vector with the coefficients a1 and a0.
% Er The overall error.

nx = length(x);
ny = length(y);
if nx ~= ny
disp('ERROR: The number of elements in x must be the same as in y.')
a = 'Error';
Er = 'Error';
else
Sx = sum(x);
Sy = sum(y);
Sxy = sum(x.*y);
Sxx = sum(x.^2);
a1 = (nx*Sxy - Sx*Sy)/(nx*Sxx - Sx^2);
a0 = (Sxx*Sy - Sxy*Sx)/(nx*Sxx - Sx^2);
a=[a1; a0];
% Eq. (5.6)

Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
2

Er=sum((y-(a1.*x+a0)).^2);
end

(a) The user-defined function LinReg is used in the Command Window to solve Problem 6.1.

>> x=[ 1 3 4 6 9 12 14];


>> y=[2 4 5 6 7 9 11];
>> [a,Er] = LinReg(x, y)
a =
0.6214
1.9357
Er =
1.3643

(b) The user-defined function LinReg is used in the Command Window to solve Problem 6.2.

>> x=[-7 -4 -1 0 2 5 7];


>> y=[20 14 5 3 -2 -10 -15];
>> [a,Er] = LinReg(x, y)
a =
-2.5398
2.8685
Er =
1.6295

Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.

You might also like