Today I'd like to write about a little math problem. Pieter came to me asking if I could find a function with some desired properties. To give a simple example of why this could be useful: Suppose you want to make an animation of an object bouncing by simply changing the y position of the object. For this you could use a function that given a certain time in the animation returns you a number which you can use as the y position of the object. You can easily control the animation by adjusting the function and you can also easily skip to any part of the animation.
Now for the problem: Pieter wanted a function f(x) where the following properties hold: f(0) = 1, f(10) = 10 and somewhere between x = 0 and x = 10, let's call this t, the function should reach it maximum point at 12, so f(t) = 12. The image below shows the function he was roughly looking for:
The red line shows the possible function. The blue dots represent f(0) = 1 and f(10) = 10. The green line represent y = 12 where the function should reach it's maximum, we call this value t. |
Now we could of course use a spline that is fitted to the function, but this is not as efficient (or as fun) as a parabola. A parabola is a second degree function of the form: f(x) = ax^2 + bx + c. So how do we find the a, b and c values? We start by substitution of the desired points. For f(0) = 1, this becomes: f(0) = c = 1. So c = 1, that was easy. Now for f(10) = 10: f(10) = 100a + 10b + 1 = 10, simplified to: 100a + 10b = 9. We have 2 variables here and only 1 equation, to solve for a and b we will need another equation. The problem now is that we don't know a third point which would give us another equation. We could try to just guess a point, for example f(6) = 12, but the resulting function will not have it's maximum at y = 12, but higher.
The crucial thing here is to realize that when we reach a maximum the derivative of the function in that point is exactly 0. For a parabola the derivative is of the form: f'(x) = 2ax + b. We can use this to get another equation as we want to reach our maximum at t (it does not matter where t lies as long as it's between 0 and 10). So we want that f'(t) = 0. Using substitution we get: f'(t) = 2at + b = 0. Good, now we have the 2nd equation we were looking for and can solve for a and b you might think. But unfortunately we've added another variable t, so now we have 3 variables and 2 equations! That didn't seem to help us very much then... But wait :) Let us now try to express a and b using t. Using the equations 100a + 10b = 9 and 2at + b = 0, we can express b as b = -2at, plug this into the first equation to get 100a - 20at = 9, from which we get that a = 9 / (100-20t). We can then substitute a into b = -2at to get b = -18t/(100-20t). So now we have our a and b expressed as t to get the following function: f(x) = (9 / (100-20t)) x^2 -18t / (100-20t) x + 1.
Now remember that t is the point where the function has its maximum which we want to be at 12, we can express this as: f(t) = 12. From this we get another equation: f(t) = (9 / (100-20t)) t^2 -18t / (100-20t) t + 1 = 12. We simplify this and get: -9t^2 + 220t - 1100 = 0. We solve this using the abc-formula and obtain two values for t, namely t1 = 7.011 and t2 = 17.43. Clearly t2 is not between 0 and 10, so we pick t = t1 = 7.011. We then plug t in to a and b and get the final function: f(x) = -0.22377 x^2 + 3.13769 x + 1. Which can be seen here:
The final function with the desired properties: f(x) = -0.22377 x^2 + 3.13769 x + 1 |
-- Stijn
Geen opmerkingen:
Een reactie posten