|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
2. Linear Regression |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
a) Find the best straight line for the
following data: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
b) Find the sum of squares error (ss)
and the sum of squares error per data point (ssdp). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
c) Produce a plot showing the raw
data and the fitted line. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
%Program
to Solve Problem 2 for Linear Curve Fitting Homework %version
2007-10-10 D.W. Donovan clear all; x=[-10.0000;
-8.0000; -6.0000; -4.0000; -2.0000; 0.00; 2.0000; 4.0000; 6.0000; 8.0000;
10.0000]; fx=[122.7106; 96.9288;
80.9006; 62.2326; 35.1397; 19.6305; 5.9876; -17.7030; -20.4644; -54.1783; -71.2149]; A(:,2)=x; A(:,1)=ones(size(x)); c=A\fx; m=c(2); b=c(1); ffx=m*x+b; ss=(fx-ffx)'*(fx-ffx); ssdp=ss/size(fx,1); xc=[min(x):(max(x)-min(x))/100:max(x)]'; fxc=m*xc+b; lm=['slope = ',num2str(m)]; lb=['y-intercept
= ',num2str(b)]; lss=['sum of
squares error = ',num2str(ss)]; lssdp=['sum of
squares per data point = ',num2str(ssdp)]; figure hold on; name='D. W.
Donovan - '; t1='Linear
Curve Fitting Homework Problem #2'; t2='Data Fitted
to F(x) = mx + b'; t=[t1,'\newline',t2,'\newline',name,date]; title(t) xlabel('x in unitless numbers') ylabel('f(x) in unitless numbers') plot(x,fx,'r*') plot(xc,fxc,'b') plot(min(xc),max(fxc),'w') plot(min(xc),max(fxc),'w') plot(min(xc),max(fxc),'w') plot(min(xc),max(fxc),'w') legend('raw data','fitted curve',lm,lb,lss,lssdp,3); legend('boxoff') %{ m = -9.3962 b = 23.6336 ss = 274.5485 ssdp =
24.9590 %} |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|