Uploaded by Cancel Fword

Homework 5 Solutions

advertisement
Homework 5
________________________________________________
____
prob 9:6
(a)
Save as prob9_6a.m
________________________________________________
D=load('xyline.dat');
x=D(:,1);
y=D(:,2);
c=linefit(x,y)
A=[x ones(length(x),1)];
r=norm(y-A*c)
________________________________________________
>> prob9_6a c=
-0.1996
2.0357
r= 0.6073
(b)
>> c1t=c(1)*linspace(0.6,1.4,20);
>> c2t=c(2)*linspace(0.6,1.4,20);
(c)
Save as prob9_6c.m
________________________________________________
____
for i=1:length(c1t)
for j=1:length(c2t)
R(i,j)=norm(y-(c1t(i)*x+c2t(j)));
end
end
________________________________________________
____
>> prob9_6c
(d)
>> meshc(c1t,c2t,R)
>> contour(c1t,c2t,R)
NOTE: It is not necessary to use the meshgrid
function as long as c1t=(n),
c2t=(m), and R=(mXn).
(e)
R is the residual. The goal of the least
squares fit is to minimize the
residual. We want a c(1) and c(2) such that R
will be minimum. The
minimum can be clearly seen in the contour and
mesh plots of R.
________________________________________________
____
prob 9:7
(a)
>> c = (x'*y)/(x'*x)
c = exp(beta), (using [ln(y) = alpha*ln(x) +
beta] and [alpha = 1])
(b)
c = exp(beta), (using [ln(y) = alpha*ln(x) +
beta] and [alpha = 2])
c = -sqrt(y)/(2*A’)
(c)
c = alpha, (using [ln(y) = alpha*ln(x) + beta]
and [beta = ln(1)])
________________________________________________
____
prob 9:8
x = [1 2.5 4 5.5 7];y=[2.2851 1.4176 0.8794
0.5455 0.3384];
plot(x,y)
c = expFit(x,y)
x=linspace(1,7,20)
y=c(1)*x.^(c(2)*x)
hold on;plot(x,y,'r')
________________________________________________
____
prob 9:9
function C=expFit(x,y)
u=x;
v=log(y);
c = linefit(u,v);
c1=exp(c(2));
c2=c(1);
C=[c1 c2];
x=[1 2.5 4 5.5 7];y=[2.2361 10.9329 24.6765
42.8382 65.0486];
plot(x,y)
c=powFit(x,y)
x=linspace(1,7,20)
y=c(1)*x.^(c(2))
hold on;plot(x,y,'r')
________________________________________________
____
prob 9.10
function c= invxFit(x,y)
% Function to use linefit to fit data to y = c1/x + c2
using the
% transformation w = 1/x: y = c1*w + c2
% inputs: x and y (observations)
% output: c (coefficients of linear eqn)
c = linefit(1./x, y);
end
________________________________________________
____
prob 9:11
function C = powFit(x,y)
u=log(x); v=log(y);
c=linefit(u,v);
c1=exp(c(2));
c2=c(1);
C=[c1 c2];
x=[2.25 2.5417 2.8333 3.125 3.4167 3.7083 4];
y=[2.8648 1.4936 1.0823 0.8842 0.7677 0.6910
0.6366];
c=xlinxFit(x,y)
plot(x,y)
x=linspace(2,4,20); y=x./(c(2)*x+c(1))
function C=xlinxFit(x,y)
z=1./y; w=1./x;
c=linefit(w,z);
C=[c(2) c(1)];
________________________________________________
____
prob 9.14
% Load variables and convert °C to K
T = airvisc(:,1); T = T + 273.15;
mu = airvisc(:,2);
% Setup vectors for (T^3/2)/(mu) = T/b + s/b
% c(1)*x + c(2) = y ----> c(1) = 1/b, c(2) = s/b, y =
1/mu * T^3/2
y = (T.^3/2)./mu;
A = [T ones(numel(T),1)];
% Solve using a least-squares approach:
cLS = (A'*A)\(A'*y);
% Now recover b and s
b = 1/cLS(1);
s = cLS(2)*b;
________________________________________________
____
prob 9.15
function fitPlot(x,y,basefun)
% Fits a model to given data using the user-defined
base function
% see fitnorm.m in the nmm tool box
% Call fitnorm to find the fitted coefficients
[c,R2,rout] = fitnorm(x,y,basefun);
% now create the modeled data and plot it
xfit = linspace(min(x), max(x), 100);
Af = feval(basefun,x(:)); % Coefficient matrix of
system
yfit = Af*c;
plot(x, yfit)hold onplot(x, y,'rx')legend('Model','Data')
________________________________________________
____
prob 9:19
[T mu]=loadColData('H2Ovisc.dat',2,3)
T=T+276
T0=T(1)
mu0=mu(1)
y=log(mu/mu0)
x=T/T0
plot(x,y)
P=polyfit(x,y,2)
hold on;plot(x,P(1)*x.^2+P(2)*x+P(3),'r')
c1=P(3)
c2=P(2)
c3=P(1)
________________________________________________
____
prob 9:20
[T mu]=loadColData('glycerin.dat',5,7)
mu=mu(:,2)
T=T+276
T0=276
mu0=mu(1)
y=log(mu/mu0)
x=T/T0
plot(x,y)
P=polyfit(x,y,2)
hold on;plot(x,P(1)*x.^2+P(2)*x+P(3),'r')
c1=P(3)
c2=P(2)
c3=P(1)
________________________________________________
____
prob 9:23
T=[773.5 786 797.5 810 810 820 834]
k=[1.63 2.95 4.19 8.13 8.19 14.9 22.2]
plot(T,k)
v=log(k)
u=T
c=linefit(u,v)
c1=exp(c(2));c2=c(1);
x=linspace(773,830,100);
y=c1.*exp(x*c2)
hold on;plot(x,y,'r')
________________________________________________
____
prob 9:32
load pdxTemp.dat
mo=pdxTemp(:,1)
T=pdxTemp(:,4)
plot(mo,T)
x=(sin((mo-1)*pi/12)).^2
c=linefit(x,T)
MONTH=1:12
TEMP=c(2)+c(1)*sin((MONTH-1)*pi/12).^2
hold on;plot(MONTH,TEMP,'r')
Download