%% Decomposition of Butadiene
%% Kurt Bryan and SIMIODE
%
% This is a Matlab script to analyze butadiene decomposition data.
%
% The times at which concentration data was taken are

times = [0,1000,1800,2800,3600,4400,5200,6200];

% The concentration data (moles per liter) is

data = [0.01,0.00625,0.00476,0.0037,0.00313,0.0027,0.00241,0.00208];

% Compute the number of data points

N = length(data);

% Here is a plot of the raw data

figure(1)
plot(times, data,'.r','MarkerSize',20);
xlabel("Time (seconds)");
ylabel("Concentration (moles/liter)");

% Could it be first order?. Perform a logarithmc transformation of the data and plot

log_of_data = log(data);
figure(2)
plot(times, log_of_data,'.r','MarkerSize',20);
xlabel("Time (seconds)");
ylabel("log(concentration) (moles/liter)");

% This is not a straight line, so this is not first order. Still, fit a line and see how it looks

c = polyfit(times,log_of_data,1);

% Evaluate line at "times", plot with log data

fittimes = polyval(c,times);
figure(3)
plot(times, log_of_data,'.r','MarkerSize',20);
hold on
plot(times,fittimes,'-b');
xlabel("Time (seconds)");
ylabel("log(concentration) (moles/liter)");
hold off

% Perhaps it's second order...