% dplot.m -- MATLAB program to plot load voltage versus load current
%            and provide a least-squares fit of a straight line.
% Rich Kozick, ELEC 101, Spring 1998.

% Enter load resistance values, in ohms
% Example:  rL = [1000, 900, 100];

rL = [];

% Enter load voltage measurements, in volts
% Example:  vL = [1, .9, 0.1];
% Must be same size array as rL.

vL = [];

% Compute load currents, in amps

iL = vL ./ rL;

% Get least-squares fit of a straight line to the data
% Choose a subset of the data to fit.
% Example:  k = 3:7;
%    will fit data points 3, 4, 5, 6, and 7
% Default is to fit all of the data.

k = 1:length(vL);

iLk = iL(k);
vLk = vL(k);
[p,S] = polyfit(iLk, vLk, 1);
vLkfit = polyval(p, iLk, S);

fprintf('Thevenin model: Voc = %f volts, R0 = %f ohms\n', p(2), -p(1));

figure(1)
plot(iL, vL, '*', iL, vL, ':');
xlabel('LOAD CURRENT i_L')
ylabel('LOAD VOLTAGE v_L')
title('ALL DATA FOR 7400 NAND GATE')

figure(2)
plot(iLk, vLk, '*', iLk, vLkfit, '-');
legend('ACTUAL DATA', 'THEVENIN MODEL')
hold on
plot(iLk, vLk, ':');
hold off
xlabel('LOAD CURRENT i_L')
ylabel('LOAD VOLTAGE v_L')
title('SUBSET OF DATA FOR 7400 NAND GATE')
