No smilies, no avatars, no flashing gifs. Just discuss the issues of the day, from last night's telly via football to science or philosophy.
Started by LomaxFairchild on Dec 13, 2021 4:48:37 PM
Plotting a curve with a known 'half life'

I'm asking this here as it's a problem that someone may have come across. I need to plot a curve with the following known values:

v1 - a starting value

v2 - an end value

t1 - a start time

t2 - an end time

h - a percentage of the time period by which point the y value is half way between v1 and v2.

E.g. If h = 25, then at time = t1 + (t2 - t1) * 0.25, v would be v1 + (v2 - v1) * 0.5. If h = 50, the line would be straight.

Ideally I need a formula to calculate a value t, between t1 and t2, for any given value of v but my little brain is struggling.

Turtle - 13 Dec 2021 18:15:49 (#1 of 26)

Here is a formula. It may look a little complicated, but should be straightforward enough to implement on a computer.

Given a t between t1 and t2, define n as follows

n = [ln(t2-t)-ln(t2-t1)]/ln(1-h/100)


f(t)=[v1 + (2^n-1)v2]/2^n

I can explain how I derived this expression if you are interested, but for now let's do a few calculations for sanity check.

When t=t1, we get n=0, and f(t)=v1, as required.

When t=t2, then strictly speaking the formula for n is underfined, but taking the limit you can say that n is infinity and f(t)=t2.

When t=t1+h(t2-t1), we get n=1 (assuming I did not make a mistake - check it!) and f(t)=(v1+v2)/2

When h=50, we obtain

n = [ln(t2-t)-ln(t2-t1)]/ln(1/2), which is the same as log to base 2 of (t2-t1)/(t2-t). In this case


and after some simplification we obtain the formula


which is a linear formula interpolating between f(t1)=v1 and f(t2)=v2.

LomaxFairchild - 13 Dec 2021 18:29:17 (#2 of 26)

Fantastic - thanks Turtle - I will plug this in and give it a whirl

LomaxFairchild - 13 Dec 2021 18:48:27 (#3 of 26)

Just to check I've got this right, this (what I think) is your formula in code:

let n = (Math.log(t2-t) - Math.log(t2-t1)) / Math.log(1-h/100)

let y = (v1 + Math.pow(2, n-1) * v2) / Math.pow(2,n)

It looks good (i.e. giving a nice straight line at h=50), but it's giving me values outside the v1 - v2 range. I wasn't quite sure how to transpose (2^n-1)v2 - I've assumed a multiplication?

Atticus - 13 Dec 2021 18:50:31 (#4 of 26)

Half-Life 3 confir.....


Skulks off.

Turtle - 13 Dec 2021 19:13:35 (#5 of 26)

Yes, it is multiplication. But just to be sure, let me emphasize that it is 2^n-1, not 2^{n-1}.

For example, if n=3, (2^n-1)v2 = 7 times v2.

So I think in your code the formula for y should probably be the following (but I am not familiar with this programming language)

y = (v1 + (Math.pow(2, n)-1) * v2) / Math.pow(2,n)

LomaxFairchild - 13 Dec 2021 20:27:00 (#6 of 26)

Excellent! That's done it.

Thank you so much, Turtle. I've been scratching my head for hours over that one.

I would be interested in understanding the logic but aware that you've already given a lot of your time, so feel free to point me at the right terms to Google.

Turtle - 13 Dec 2021 21:54:45 (#7 of 26)

I would not actually know what to Google. The idea is simple enough. Your condition says that


You can rewrite it as follows

f[t2 - (1-0.001h)(t2-t1)]=(v1+v2)/2

Now replace t1 with t2 - (1-0.01h)(t2-t1) and do the same calculation again. That is, write the condition that the value of f at h percentage of the way from the new t1 to t2 is the average of the value of f at the new t1 and at t2. After doing the calculation and simplifying, you get the formula

f[t2 - (1-0.001h)^2(t2-t1)]=(v1+3v2)/4

And by induction on n, you get the following formula for all positive integers n.

f[t2 - (1-0.001h)^n(t2-t1)]=[v1+(2^n-1)v2]/2^n

Now let t=t2 - (1-0.001h)^n(t2-t1). Then


where we need to express n in terms of t. If you take the definition of t above and solve for n, you will get our formula.

Atticus - 13 Dec 2021 22:55:29 (#8 of 26)

Good work Turtle!

Macpaddy - 13 Dec 2021 23:57:58 (#9 of 26)

Now THAT is lending a hand. Superb, Turtle.

CaptainBlack - 14 Dec 2021 00:19:19 (#10 of 26)

Yes, but it won't help you against the Combine.

HomerLoon2 - 14 Dec 2021 00:23:09 (#11 of 26)

Round of applause for Turtle!

Turtle - 14 Dec 2021 06:29:49 (#12 of 26)

Whoa, thanks.

tasselhoff - 14 Dec 2021 09:27:57 (#13 of 26)

round of applause

LomaxFairchild - 14 Dec 2021 09:38:06 (#14 of 26)

Yes, that is brilliant, thank you.

My next problem is reversing it to return a value t for any given value of y.

PeacockinaPearTree - 14 Dec 2021 09:39:43 (#15 of 26)

not sure Turtle has it right, check this out:

monkeymadchick - 14 Dec 2021 09:51:24 (#16 of 26)

I have no idea what any of it means or does but in the internet sea of idiocy and cruelty I’m heartened by the cleverness and willingness to give help so I too applaud Turtle.

or Turtle has just aided the evil Lomax by unwittingly providing the final piece of the puzzle that brings AI to the point of singularity meaning by the end of January we will be hunkered down in burnt out shell holes while the machines hunt us to extinction. Could go either way really.

PeacockinaPearTree - 14 Dec 2021 09:54:05 (#17 of 26)

monkey! welcome back!

tasselhoff - 14 Dec 2021 09:55:17 (#18 of 26)

She makes a good point too. I suggest we hunt turtle down before he can do any more damage.

LomaxFairchild - 14 Dec 2021 10:39:24 (#19 of 26)

t = t2 - Math.exp( Math.log(1- 0.01h) * Math.log2((y1 - y2) / (y - y2)) + Math.log(t2-t1) )

Seems to do the trick. Excellent. Time to instigate Armageddon . . .

Atticus - 14 Dec 2021 10:45:30 (#20 of 26)

Ah but maybe Turtle has hidden a trap in the equation like a modern day Galen Erso.

Check Subscriptions
Home » IT & Computers