Formula to represent temperature at given point of year

80 Views Asked by At

I'm programming a game where the in-game temperature shifts throughout the year, from hottest in the middle of summer to coldest in the middle of winter. I'm trying to create a mathematical function that can tell me, given time T (0 -> 1), what the current temperature is.

The variables involved are be:

TempMax = Highest temperature of the year

TempMin = Lowest temperature of the year

And T is represented as a float between 0 and 1, where 0 is the start of spring, and 1 is the end of winter.

So far I've been using a Sin(x) function to start, and then TempMax and TempMin to create the highest and lowest points on the wave:

Temp(T) = Sin(T) * (ABS(MaxTemp) + Abs(MinTemp))/2 + ABS(MinTemp)

And that looks fine. But the problem is getting it to line up such that the lowest temperature is in the middle of winter (T = .875) and the highest temperature is in the middle of summer (T = .375). I believe that what I must do is shift the wave left or right by adding some addend X to the Sin function, like so:

Temp(T) = Sin(T + X) * (ABS(MaxTemp) + Abs(MinTemp))/2 + ABS(MinTemp)

I get that X itself needs to be calculated somehow based on MaxTemp and MinTemp, but I honestly have no idea how. Or, if I'm even on the right track at all.

I was wondering if someone here might be able to give me a hand?

Thanks so much in advance!

1

There are 1 best solutions below

2
On BEST ANSWER

Given what you've said, you know the formula will need to be in the following form: $A \times sin(B \times T + C) + D$. So:

$A = \frac{MaxTemp - MinTemp}{2}$

This is because $sin$ ranges from $-1$ to $1$ and you need to scale that up to $MinTemp$ to $MaxTemp$.

$D = A + MinTemp$.

This is because when the $sin$ curve is at its lowest, $A \times sin(...) = A \times -1=-A$ So you add $A$ (to bring it zero) and then add $MinTemp$ to bring it up or down to $MinTemp$.

We can check out intutition by looking at the $sin$ curve at its max. Then we get the temperature as

$A \times sin(...) + D=\frac{MaxTemp - MinTemp}{2} \times 1 + \frac{MaxTemp - MinTemp}{2} + MinTemp$$ = MaxTemp - MinTemp + MinTemp = MaxTemp$

Now that we've gotten the min and max, let's work on what's inside the $sin$ function.

Now, we know that a $sin$ curve has a period of $2\pi$ - and we want a period of $1$. So we know $B = 2\pi$. This way, in 1 year, the $sin$ curve will do a full $2\pi$.

Finally, let's get to $C$. You can figure out what to do with $C$ by looking at what you want at $T = 0$. At $T = 0$ you are a quarter of the way from the lowest point to the highest point. For $sin(\theta)$ thesea are that $\theta=\frac{3\pi}{2}$ and at $\theta=\frac{\pi}{2}$ respectively. A quarter of the way between them is $\theta=\frac{7\pi}{2}$. So, $C=\frac{7\pi}{2}$

You plug everything back in...

$Temp(T)=\frac{MaxTemp-MinTemp}{2} \times sin(2\pi \times T + \frac{7\pi}{4}) + \frac{MaxTemp-MinTemp}{2} + MinTemp$

(Assuming I didn't mess up any algebra anywhere).