2. Linear Regression

 

a) Find the best straight line for the following data:

 

x

f(x)

 

 

 

-10.000

122.7106

 

 

 

-8.0000

96.9288

 

 

 

-6.0000

80.9006

 

 

 

-4.0000

62.2326

 

 

 

-2.0000

35.1397

 

 

 

0.0000

19.6305

 

 

 

2.0000

5.9876

 

 

 

4.0000

-17.7030

 

 

 

6.0000

-20.4644

 

 

 

8.0000

-54.1783

 

 

 

10.0000

-71.2149

 

 

 

 

slope  =

-9.3962

 

y-intercept =

23.6336

 

b) Find the sum of squares error (ss) and the sum of squares error per data point (ssdp).

 

ss =

274.5485

 

ssdp =

24.9590

 

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

%}

 

Back To Dr. Donovan's Courses Page

 

Back To Dr. Donovan's Homepage

 

Physics Dept. Homepage

 

NMU Homepage

 

Please send any comments or questions about this page to ddonovan@nmu.edu

This page last updated on October 9, 2015