# Measuring slope of graph

## Recommended Posts

Hi Guru, by now you will know my math is poor!!

I would like to measure the slope of a graph displaying temperature against time for the previous hour and then display it as a variable (Degrees centigrade change/ Hour) until updated an hour later.

As a nicety also hold the maximum change for a 24 hour period starting at midnight..

Can you give me any pointers as to how I would achieve this?

Thanks

Andy

##### Share on other sites

Well, you won't be able to do it directly in the graph, at least not automated, because the graph doesn't have any real script (unless you put it in the OnPaint event). So, I'd use the event of the channel for the trace of your graph. Here, you'll need to capture everything. First, you'll need a startup sequence to init the variables:

global maxslope = -1e99

global lastslope = 0

global lastval = -1e99

Then in the event it'll be something like this (and I'm doing it off the top of my head, so you may need to check the docs on formatdatetime()):

```if (formatdatetime("%H",mychannel.time[0]) != (formatdatetime("%H",mychannel.time[1]))
if (lastval == -1e99)
lastval = mychannel[0]
return
endif
lastslope = (mychannel[0] - lastval) / (mychannel.time[0] - gettime(lastval))
lastval = mychannel[0]
if (lastslope &gt; maxslope)
maxslope = lastslope
endif
endif
if (formatdatetime("%d",mychannel.time[0]) != (formatdatetime("%d",mychannel.time[1]))
maxslope = lastslope
endif```

Note that this doesn't take into account any noise in the signal. Its not doing a linear fit and getting the slope, its just looking at the slope between two points separated by about an hour.