# How to find time when temperature crossed certain threshold?

I have a data from experimental system concerning temperature of molten lead. In this system, temperature is increased in one place and then is measured when this increase of temperature travels to a different place by natural convection.

Data are pairs time vs. measured temperature. The time have steps of one second (sometimes two, due to the measuring system) and temperatures have steps of 0.1 degree of Celsius.

I would like to find when temperature crossed a certain temperature threshold. Let´s say I have something like:

• Time [s] - Temperature [°C]
• 1 - 440.8
• 2 - 440.9
• 3 - 441.1
• 4 - 441.2

I tried linear fit on two points (right below and right above threshold e.g. 441.0) but I would like to try something more "advanced". I know that Excel can do fitting quite well and it is possible to find root by Wolfram Alpha, but it is not an option since I have to evaluate several experiments done each day (thus I have prepared program for it in C# programming language).

Thus I would like to know if there is some plan like saying "take 2 points below threshold and two above, do [something] and you will get time when temperature crossed your chosen threshold". It can use of course more data points. I am not interested in the fitting equation itself, just its root(s).

Thank you for your hints and ideas! Michal

EDIT: My data are somewhat noisy at constant temperature, but when rise of temperature takes place, it is not a problem.

• You can try a the option best-fit curve of the a computer algebra software like Matlab, Octave or wxMaxima. Jul 14 '15 at 10:03
• Thank you for idea, but since I need to do it in my program, I can use this only to verify my solution, not to find it.
– Rao
Jul 14 '15 at 11:55

If you want to take 4 points, I would still stick with linear approximation. It mostly depends on the noisiness of your data and rate of change of the temperature. You want higher order fit (like Floris described) in case you have low noise and time between observation is big. Your sample data do not seem as the case.

The linear fit is analytical and easy. You just take whatever amount $N$ of points around the threshold you deem appropriate, and compute slope $a$ and intercept $b$ of the best fitting line with the classic formula (bar denotes mean): $$a = \frac{\sum_N (x_i-\bar{x})(y_i-\bar{y})}{\sum_N(x_i-\bar{x})^2}$$

$$b = \bar{y}-a\bar{x}$$

Your root $x_{root}$ for threshold $T$ is then trivially: $$x_{root} = \frac{T-b}{a}$$

If you still choose the quadratic fit you can find quadratic coefficients like this and then compute root of quadratic function.

I would take two points below and two point above the point where the threshold is crossed and fit a parabola. If the points are equally spaced the equations are quite simple. It is a little bit more work if they are not.

You will be solving three equations with three unknowns, where the coefficients of the equation are a function of the $x_i, y_i$ data points.

The equations you need are described in http://mathworld.wolfram.com/LeastSquaresFittingPolynomial.html

Basically you will create the Vandermonde matrix (see link) and solve the set of equations.

To make things work more robustly you should subtract the mean from the data before you start - otherwise, since you are taking the sum of a bunch of numbers to the sixth power, numerical errors will creep in.

When I get near a laptop I may write the C code to show how this is done.

• A parabolic spline needs three points. If you have four points, they describe a cubic. You're not doing least squares unless you are fitting more points than your spline has degrees of freedom. Jul 14 '15 at 12:14
• @MikeDunlavey - I am recommending using a parabolic fit through four points - not a cubic spline. Hence the link to "least squares fitting". It takes a little bit of the noise out... Jul 14 '15 at 12:17