% Notebook to support Exercise 3.4.10, modeling a cooling potato.
% The data, in time/temperature pairs:

times = [0, 2, 4, 8, 10, 13, 17, 20, 24, 30];
temps = [204, 193, 184, 169, 162, 156, 149, 143, 138, 130];

% However, we will operate on the quantities (t, log(u(t)-A)-log(u(0)-A), with u(0)=204 and A = 72.

A = 72;
u0 = 204;
logtemps = log(temps-A)-log(u0-A);

% A plot:

plt1 = plot(times,logtemps,'-r');

% We seek to fit a line y = -k*t to this

syms u(t,k);
u(t,k) = -k*t

% A least-squares function can be formed as

syms SS(k)
SS(k) = sum((u(times,k)-logtemps).^2);

% Now adjust k to minimize this.