clear; clc; close all;

T = [ 313 319 323 328 333 ];
k = [ 0.00043 0.00103 0.00180 0.00355 0.00717 ];


%---------------------------------------------------
% linearized form:

% function to calculate the log of the rate constant
lnk = @(p)( p(1) - p(2)./(8.314*T) );

% function to calculate the sum of the squared errors.
sumerr = @(p)( sum( (log(k)-lnk(p)).^2 ) );

% minimize the sum of the squared errors.
pguess = [ 10 1e4 ];
params = fminsearch( sumerr, pguess );

fprintf('Linear: Ea=%.3e  A=%.3e\n',params(2),exp(params(1)) );
%---------------------------------------------------



%---------------------------------------------------
% nonlinear form

% function to calculate the rate constant:
kfun = @(p)( p(1)*exp(-p(2)./(8.314*T)) );

% function to calculate the sum of the squared errors.
sumerr = @(p)( sum( (k-kfun(p)).^2 ) );

pguess = [ 1e15 1e5 ];
nonlinparams = fminsearch( sumerr, pguess );

A = nonlinparams(1);
Ea = nonlinparams(2);
fprintf('Nonlinar: Ea=%.3e  A=%.3e\n',Ea,A);
%---------------------------------------------------
