% ENTER YOUR NAME HERE
% ME3210 Mechatronics II Laboratory
% First-Order Response Lab
%
% Script to plot and analyize Mechatronics 1st Order lab data
% =========================================================================
clear, clf, echo off, clc;

% FIRST DATA SET
%
% Initialize knowns
% -------------------------------------------------------------------------
Cp = 4.184e3;   % J/(kg.K)
m1 = ;          % INSERT THE MASS OF THE FIRST SAMPLE HERE (IN kg)

% Load the First Data Set (Known Mass)
% -------------------------------------------------------------------------
load *.dat      % Replace the * with the filename of your data file 
t1 = *(:,1);
T2 = *(:,2);
Ta = min(T2);   % this is the ambient temperature, Ta, of the room

% Use semi-log linear regression to iterate to find find tau1 and T2f
% -------------------------------------------------------------------------
T2f = max(T2)+20;  % initial guess of the final temperature of the water T2f
dT = 0.05;
sumSqrOld = 1e10;
sumSqr = 1e6;
while(sumSqrOld - sumSqr >= 0),
    sumSqrOld = sumSqr;
    y1 = log(T2f - T2);
    [P] = polyfit(t1, y1, 1);
    Tau1 = -1/P(1);
    mod1 = ; % INSERT THE EXPRESSION FOR T2 FROM YOUR PRE-LAB EXERCISE
    resid = mod1 - T2;
    sumSqr = sum(resid.*resid);
    T2f = T2f-dT;
end

% Plot Data & Model to Check Fit
% -------------------------------------------------------------------------
subplot(2, 1, 1)
plot(t1, T2, t1, mod1);
xlabel('Time [s]'); ylabel('Temperature [C]');
title('Temperature Profile of 1^s^t Sample');
grid on

% Output tau1, C1, T2f, Rl, and qs
% -------------------------------------------------------------------------
tau1
C1 = m1*Cp  % Fluid capacitance of the fist water sample
T2f
Rl = tau1/C1
qs = % INSERT YOUR EXPRESSION FOR qs FROM YOUR PRE-LAB

% =========================================================================
% SECOND DATA SET: Unknown Mass
%
% Load Second Data Set
% -------------------------------------------------------------------------
load *.dat      % Replace the * with the filename of your second data file
t2 = *(:,1);
T2 = *(:,2);
Ta = min(T2);

% Use semi-log regression to iterate to find find tau1 and T2f
% -------------------------------------------------------------------------
T2f = max(T2)+20; % initial guess
sumSqrOld = 1e10;
sumSqr = 1e8;
while(sumSqrOld-sumSqr>=0),
    sumSqrOld = sumSqr;
    y2 = log(T2f-T2);
    [P] = polyfit(t2,y2,1);
    Tau2 = -1/P(1);
    mod2 = ; % INSERT THE EXPRESSION FOR T2 FROM YOUR PRE-LAB EXERCISE
    resid = mod2-T2;
    sumSqr = sum(resid.*resid);
    T2f = T2f-dT;
end

% Plot data & model to check fit
% -------------------------------------------------------------------------
subplot(2,1,2)
plot(t2,T2,t2,mod2);
xlabel('Time [s]'), ylabel('Temperature [C]');
title('Temperature Profile of 2^n^d Sample');
grid on;

% output qs, Tau1, and T2
Tau2
T2f
m2 = % INSERT YOUR EXPRESSION FOR THE MASS m FROM YOUR PRE-LAB EXERCISE