Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Homework 5 solution
July 18, 2008
10 points each problem, 90 points total.
12.8 Plot using Matlab. For Part b), do 3 iterations by hand. For Part c), do
5 iterations using Matlab (see page 275)
a)
>> y1=@(x)sqrt(5-x^2);
>> y2=@(x)x^2-1;
>> fplot(y1,[0 2]);
>> hold on
>> fplot(y2, [0 2]);
>> hold off
A solution seems to lie at about x = y = 1.6.
b)
The equations can be solved in a number of different ways. For example, the first equation can be
solved for x and the second solved for y. For this case, successive substitution does not work
1
An alternative solution involves solving the second equation for x and the first for y. For this case,
successive substitution does work.
First iteration:
x=sqrt(y+1)=sqrt(1.5+1)=1.581139
y=sqrt(5-x^2)=sqrt(5-1.581139^2)=1.581139
Second iteration:
x=sqrt(1.581139 +1)=1.606592
y=sqrt(5-1.606592^2)=1.555269
Third iteration:
x=sqrt(1.555269+1)=1.598521
y=sqrt(5-1.598521^2)=1.563564
After several more iterations, the calculation converges on the solution of x = 1.600485 and y =
1.561553.
c)
Interation 1
>> x=[1.5;1.5];
>> J=[2*x(1) 2*x(2);2*x(1) -1];
>> f=[x(1)^2+x(2)^2-5;x(1)^2-x(2)-1];
>> x=x-J\f
x=
1.6042
1.5625
Interation 2
>> J=[2*x(1) 2*x(2);2*x(1) -1];
>> f=[x(1)^2+x(2)^2-5;x(1)^2-x(2)-1];
>> x=x-J\f
x=
1.6005
1.5616
Interation 3
>> J=[2*x(1) 2*x(2);2*x(1) -1];
>> f=[x(1)^2+x(2)^2-5;x(1)^2-x(2)-1];
>> x=x-J\f
x=
1.6005
1.5616
Interation 4
>> J=[2*x(1) 2*x(2);2*x(1) -1];
>> f=[x(1)^2+x(2)^2-5;x(1)^2-x(2)-1];
>> x=x-J\f
2
x=
1.6005
1.5616
Interation 5
>> J=[2*x(1) 2*x(2);2*x(1) -1];
>> f=[x(1)^2+x(2)^2-5;x(1)^2-x(2)-1];
>> x=x-J\f
x=
1.6005
1.5616
13.5 Do problem “by hand” by establishing the normal equations. You
could use, however, Matlab to do all the computations (i.e. summations and
the solution of the normal equations.)
a) y versus x:
From matlab,
>> x=[0 2 4 6 9 11 12 15 17 19];
>> y=[5 6 7 6 9 8 7 10 12 12];
>> n=length(x);
>> sumx=sum(x)
sumx =
95
>> sumy=sum(y)
sumy =
82
>> sumx2=sum(x.^2)
sumx2 =
1277
>> sumy2=sum(y.^2)
sumy2 =
728
>> sumxy=sum(x.*y)
sumxy =
911
>> xmean=sumx/n
xmean =
9.5
>> ymean=sumy/n
ymean =
8.2
>> slope1=(n*sumxy-sumx*sumy)/(n*sumx2-sumx^2)
slope1 =
0.3525
>> interpt1=ymean-slope1*xmean
interpt1 =
3
4.8515
> sr1=sum((y-interpt1-slope1*x).^2)
sr1 =
9.0740
>> syx1=sqrt(sr1/(n-2))
syx1 =
1.0650
>> st1=sum((y-ymean).^2)
st1 =
55.6000
>> r1=sqrt((st1-sr1)/st1)
r1 =
0.9148
>> xx=[0:0.1:20];
>> yy=slope1*xx+interpt1;
>> plot(x,y,'o',xx,yy)
b) x versus y
>> slope2=(n*sumxy-sumy*sumx)/(n*sumy2-sumy^2)
slope2 =
2.3741
>> interpt2=xmean-slope2*ymean
interpt2 =
-9.9676
>> sr2=sum((x-interpt2-slope2*y).^2)
sr2 =
61.1187
>> st2=sum((x-xmean).^2)
st2 =
374.5000
>> syx2=sqrt(sr2/(n-2))
4
syx2 =
2.7640
>> r2=sqrt((st2-sr2)/st2)
r2 =
0.9148
>> yy2=slope2*xx+interpt2;
>> plot(x,y,'o',yy2,xx)
We can plot both lines on the same graph:
>> plot(x,y,'o',xx,yy,'-',yy2,xx)
5
Thus, the “best” fit lines and the standard errors differ. This makes sense because different errors
are being minimized depending on our choice of the dependent (ordinate) and independent
(abscissa) variables. In contrast, the correlation coefficients are identical since the same amount
of uncertainty is explained regardless of how the points are plotted.
13.11 It is OK to use Matlab’s built-in commands ( except \ ) or the linregr
function given in the textbook.
>> W=[70 75 77 80 82 84 87 90]';
>> A=[2.1 2.12 2.15 2.2 2.22 2.23 2.26 2.3]';
>> c=polyfit(log10(W),log10(A),1)
c=
0.3799 -0.3821
>> a=10^c(2)
a=
0.4149
>> b=c(1)
b=
0.3799
>> Area=a*95^b
Area =
2.3404
>> xx=70:0.1:90;
>> yy=a*xx.^b;
>> plot(W,A,'o',xx,yy)
6
13.16 Submit your code to the hand-in directory and include hardcopy in
your homework submission. Test it using P13.5 data.
function [a, r2, syx] = linregr2(x,y)
% [a, r2, syx] = linregr(x,y):
% Least squares fit of a straight line to data
% by solving the normal equations.
% input:
% x = independent variable
% y = dependent variable
% output:
% a = vector of slope, a(1), and intercept, a(2)
% r2 = coefficient of determination
% syx = standard error of the estimate
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % convert to column vectors
sx = sum(x); sy = sum(y);
sx2 = sum(x.*x); sxy = sum(x.*y); sy2 = sum(y.*y);
a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2);
a(2) = sy/n-a(1)*sx/n;
syx=sqrt(sum((y-a(1)*x-a(2)).^2)/(n-2));
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;
% create plot of data and best fit line
xp = linspace(min(x),max(x),2);
7
yp = a(1)*xp+a(2);
res=a(1)*x+a(2)-y;
subplot(2,1,1);plot(x,y,'o',xp,yp)
xlabel('x'),ylabel('y'),title('Linear Least-Squares Fit')
subplot(2,1,2);plot(x,res,'o',x,res)
xlabel('x'),ylabel('residual'),title('Residual Plot')
grid on
Test application to data from P13.5,
>> x=[0 2 4 6 9 11 12 15 17 19];
>> y=[5 6 7 6 9 8 7 10 12 12];
>> [a, r2, syx]=linregr2(x,y)
a=
0.3525 4.8515
r2 =
0.8368
syx =
1.0650
8
13.29 Use Matlab to establish (i.e. declare the elements of) the
overdetermined system Ac = y representing this problem. Then solve the
resulting normal equations (A’Ac = A’y) using Matlab’s built-in commands.
>> A=[0 5 10 15 20; 1 1 1 1 1]';
>> y=[100 200 450 950 2000]';
>> y=log(y);
>> c=(A'*A)\(A'*y)
c=
0.1510
4.5841
>> a=exp(c(2))
a=
97.9148
>> b=c(1)
b=
0.1510
>> p25=a*exp(b*25)
p25=
4.2680e+03
14.3 Solve by formulating the problem as Ac = y. Then find r2 and Sy/x.
>> x= [3 4 5 7 8 9 11 12]';
>> y = [1.6 3.6 4.4 3.4 2.2 2.8 3.8 4.6]';
>> A = [x.^0 x.^1 x.^2 x.^3]
A=
1
3
9
27
1
4
16
64
1
5
25
125
1
7
49
343
1
8
64
512
1
9
81
729
1
11
121
1331
1
12
144
1728
>> c=(A'*A)\(A'*y)
c=
-11.4887
7.1438
-1.0412
0.0467
>> sr=sum((y-c(1)*x.^3-c(2)*x.^2-c(3)*x-c(4)).^2)
sr =
1.2997
>> st=sum((y-mean(y)).^2)
st =
7.6000
9
>> syx=sqrt(sr/(size(x,1)-(3+1)))
syx =
0.5700
>> r2=(st-sr)/st
r2 =
0.8290
14.4 It is OK to use the solution to the overdetermined system Ac = y as
the base for your code. Test it using the data for P14.3. Compare to the
solution obtained using polyfit. Submit your code to the hand-in directory
and include a hardcopy in your homework submission.
Approach 1 :
function p = polyreg(x,y,m)
% polyreg(x,y,m):
% Polynomial regression.
% input:
% x = independent variable
% y = dependent variable
% m = order of polynomial
% output:
% p = vector of coefficients
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
for i = 1:m+1
for j = 1:i
k = i+j-2;
s = 0;
for l = 1:n
s = s + x(l)^k;
end
A(i,j) = s;
A(j,i) = s;
end
s = 0;
for l = 1:n
s = s + y(l)*x(l)^(i-1);
end
b(i) = s;
end
p = A\b';
Approach 2:
function p = polyreg(x,y,m)
% polyreg(x,y,m):
10
% Polynomial regression.
% input:
% x = independent variable
% y = dependent variable
% m = order of polynomial
% output:
% p = vector of coefficients
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % column vectors
% A is the matrix of [x^n ... x^1 x^0]
i = 1;
A = ones(length(x), 1);
while i <= m
A = [A x.^i];
i = i + 1;
end
% (A'A)c = A'y
p = (A'*A)\(A'*y);
Test solving Prob. 14.3:
>> x = [3 4 5 7 8 9 11 12];
>> y = [1.6 3.6 4.4 3.4 2.2 2.8 3.8 4.6];
>> polyreg(x,y,3)
ans =
-11.4887
7.1438
-1.0412
0.0467
>> c=polyfit(x,y,3)
c=
0.0467 -1.0412 7.1438 -11.4887
14.8 See Example 14.2. Record your Matlab declarations and session.
The multiple linear regression model to evaluate is
Y=a0+a1*x+a2*x^2
The [Z] matrix can be set up as in
>> x1 = [0 1 1 2 2 3 3 4 4]';
>> x2 = [0 1 2 1 2 1 2 1 2]';
>> y = [15.1 17.9 12.7 25.6 20.5 35.1 29.7 45.4 40.2]';
11
>> o = [1 1 1 1 1 1 1 1 1]';
>> Z = [o x1 x2];
Then, the coefficients can be generated by solving Eq.(14.10)
>> a = (Z'*Z)\[Z'*y]
a=
14.4609
9.0252
-5.7043
The model can then be used to predict values of the unknown at the same values as the
data. These predictions can be used to determine the correlation coefficient and the
standard error of the estimate.
>> yp = Z*a
>> SSR = sum((yp - y).^2)
SSR =
4.7397
>> SST = sum((y - mean(y)).^2)
SST =
1.0587e+003
>> r2 = (SST - SSR)/SST
r2 =
0.9955
>> r = sqrt(r2)
r=
0.9978
>> syx = sqrt(SSR/(length(y)-3))
syx =
0.8888
14.12 See Example 14.5. Record your Matlab declarations and session.
First, an M-file function must be created to compute the sum of the squares,
function f = fSSR(a,xm,ym)
yp = a(1)*xm.*exp(a(2)*xm);
f = sum((ym-yp).^2);
The data can then be entered as
>> x = [.1 .2 .4 .6 .9 1.3 1.5 1.7 1.8];
>> y = [0.75 1.25 1.45 1.25 0.85 0.55 0.35 0.28 0.18];
The minimization of the function is then implemented by
>> a = fminsearch(@fSSR, [1, 1], [], x, y)
a=
9.8974 -2.5319
The fit along with the data can be displayed graphically.
>> yp = a(1)*x.*exp(a(2)*x);
>> plot(x,y,'o',x,yp)
12
13