PROGRAM FOR LINEAR REGRESSION

The MATLAB program, p23best1, finds the parameters,m and b, which in the linear model, y = mx + b, best fit the data of V versus I.  The basic equation for relating the internal resistance of the test cell to V and I is: V = -rI + e.

EXAMPLE OF OUTPUT FROM PROGRAM
» p23best1

  p23best1 - Program to obtain a linear model and fit 
  your data points (V versus I) to the best curve 
  represented by y = mx + b.  The program plots the 
  best-fit line and the data points.  

Enter your username (userid); for example:
  USERNAME: ABName  
  USERNAME: dcwold

Enter number of data point pairs (R and V):  9
Enter one measured value of R at a time
Enter one measured value of V at a time

 Data point number  1 
     Enter resistance (ohm) of load resistor: 150
     Enter voltage (V) across load resistor: 1.467
 Data point number  2 
     Enter resistance (ohm) of load resistor: 100
     Enter voltage (V) across load resistor: 1.466
 Data point number  3 
     Enter resistance (ohm) of load resistor: 60
     Enter voltage (V) across load resistor: 1.465
 Data point number  4 
     Enter resistance (ohm) of load resistor: 30
     Enter voltage (V) across load resistor: 1.457
 Data point number  5 
     Enter resistance (ohm) of load resistor: 15
     Enter voltage (V) across load resistor: 1.442
 Data point number  6 
     Enter resistance (ohm) of load resistor: 10
     Enter voltage (V) across load resistor: 1.428
 Data point number  7 
     Enter resistance (ohm) of load resistor: 8
     Enter voltage (V) across load resistor: 1.416
 Data point number  8 
     Enter resistance (ohm) of load resistor: 6
     Enter voltage (V) across load resistor: 1.401
 Data point number  9 
     Enter resistance (ohm) of load resistor: 4
     Enter voltage (V) across load resistor: 1.369

The slope =  -0.299401 V/A

The intercept on y-axis =   1.470787 V

Summary of Experimental Data: 

 Rload = 150 ohm,  Vexp = 1.467 V,  I = 0.00978 A, 
 Rload = 100 ohm,  Vexp = 1.466 V,  I = 0.01466 A, 
 Rload = 60 ohm,  Vexp = 1.465 V,  I = 0.0244167 A, 
 Rload = 30 ohm,  Vexp = 1.457 V,  I = 0.0485667 A, 
 Rload = 15 ohm,  Vexp = 1.442 V,  I = 0.0961333 A, 
 Rload = 10 ohm,  Vexp = 1.428 V,  I = 0.1428 A, 
 Rload = 8 ohm,  Vexp = 1.416 V,  I = 0.177 A, 
 Rload = 6 ohm,  Vexp = 1.401 V,  I = 0.2335 A, 
 Rload = 4 ohm,  Vexp = 1.369 V,  I = 0.34225 A, 

Summary of Best Fit (Least Squares): 

 Vbest = 1.46786 V,  Vexp = 1.467 V,  Vbest - Vexp = 0.000858439 V 
 Vbest = 1.4664 V,  Vexp = 1.466 V,  Vbest - Vexp = 0.000397364 V 
 Vbest = 1.46348 V,  Vexp = 1.465 V,  Vbest - Vexp = -0.00152379 V 
 Vbest = 1.45625 V,  Vexp = 1.457 V,  Vbest - Vexp = -0.000754312 V 
 Vbest = 1.442 V,  Vexp = 1.442 V,  Vbest - Vexp = 4.19894e-006 V 
 Vbest = 1.42803 V,  Vexp = 1.428 V,  Vbest - Vexp = 3.21709e-005 V 
 Vbest = 1.41779 V,  Vexp = 1.416 V,  Vbest - Vexp = 0.00179267 V 
 Vbest = 1.40088 V,  Vexp = 1.401 V,  Vbest - Vexp = -0.000123464 V 
 Vbest = 1.36832 V,  Vexp = 1.369 V,  Vbest - Vexp = -0.000683279 V 

N = number of data points:   9

M = number of fit parameters:   2

Listing of the Program

% p23best1 - Program to obtain a linear model and fit 
% your data points (V versus I) to the best curve 
% represented by y = mx + b.  The program plots the 
% best-fit line and the data points.  
clear all; % Clear memory 
help p23best1;  % Print header 
%@ Enter your username 
fprintf(' Enter your username (userid); for example:\n'); 
fprintf('  USERNAME: ABName  \n'); 
Username = input('  USERNAME: ','s');  % Read input as a text string 
fprintf('\n'); 
%
% Inputs - 
% x - Independent variable: the load resistance from 
%.which the current is computed. 
% y - Dependent variable: voltage across load resistor. 
% M - Number of parameters used to fit data 
% Outputs - 
% sum_sq - The sum of squares of deviations from the  
% straight line y = mx + b or V = -rI + E. 
% Parameters - -r, E 
%@ Input data 
NData = input('Enter number of data point pairs (R and V):  '); 
disp('Enter one measured value of R at a time'); 
disp('Enter one measured value of V at a time'); 
fprintf('\n');
for i=1:NData 
  fprintf(' Data point number %2.0f \n',i); 
  resist = input('     Enter resistance (ohm) of load resistor: '); 
  voltage = input('     Enter voltage (V) across load resistor: '); 
  R(i) = resist; 
  x(i) = voltage/resist; % Current through the load resistor 
  y(i) = voltage; % Voltage drop across load resistor 
end 
coef = polyfit(x,y,1); % Least-squares fit for a linear model 
m = coef(1); % m = the slope of the curve: y = mx + b 
b = coef(2); % b = the intercept on y-axis 
ybest = m*x + b; % m and b are used to compute the best fit line
%@ Print experimental and best values
fprintf('\n');
M = 2; % Number of fit parameters (M=2 is a linear curve) 
sum_sq = sum((y - ybest).^2); % The sum of squares of deviations 
fprintf('The slope = %10.6f V/A\n', m); 
fprintf('\n'); 
fprintf('The intercept on y-axis = %10.6f V\n', b); 
fprintf('\n');
fprintf('Summary of Experimental Data: \n\n'); 
for i=1:NData 
fprintf(' Rload = %g ohm,  Vexp = %g V,  I = %g A, \n',R(i),y(i),x(i)); 
end 
fprintf('\n'); 
fprintf('Summary of Best Fit (Least Squares): \n\n'); 
for i=1:NData 
fprintf(' Vbest = %g V,  Vexp = %g V,  Vbest - Vexp = %g V \n',ybest(i),y(i),ybest(i)-y(i)); 
end 
fprintf('\n'); 
%@ Statistical summary: 
N = NData; % Number of data points 
fprintf('\n'); 
fprintf('N = number of data points: %3.0f\n',N); 
fprintf('\n'); 
fprintf('M = number of fit parameters: %3.0f\n',M); 
fprintf('\n'); 
%fprintf('Sum of squares of deviations from best fit line = %g V^2\n', sum_sq); 
fprintf('\n'); 
%@ Plot y and ybest versus x 
clf; % Clear figure window 
figure(gcf); % Bring figure window forward 
plot(x,ybest, x, y, 'o'),  
title(['Linear Regression: Data and best fit for',' V versus I', '  (',Username,') ']); 
hold on; 
xlabel('Current [A]'); 
ylabel('Voltage [V]');