Programming is more than simply remembering a few functions and properties and calling them. It’s about thinking, processing, ordering, planning and implementing. Programming, real programming, requires a keen understanding of what you want to do, what you’re doing, and what needs to be done. Sound complicated? It’s not. Here’s the first of a few questions that you as a programmer should be able to do in your sleep. You can leave answers in the comments. The programming language you use doesn’t matter.
Part A
Write a function that returns the height of a bouncing ball above ground in metres after specified time (in seconds) if the ball is taking five seconds to complete each bounce (up and back down) and reaches a maximum height of 3 metres at the peak of each bounce. The initial position of the ball (after 0 seconds) is at ground level (0 metres).
Your function should take as a parameter the time elapsed in seconds (t) and return a numerical value of the height of the ball after that many seconds. Any approximations should be to at least four decimal places.
Bonus points earned if your function uses only one line of code.
Hint: Think periodic / cyclic function and half your work is done. Duoble Hint: You could use Grapher.app to check your answer…
Part B
Expand your function from Part A so that every time the ball hits the ground, the boink() function is called. (Assume boink() is already defined)
Easy? I’m sure many people wouldn’t bother reading past the first sentence, but it just shows how important it is as a programmer to be familiar with math and physics, and to be able to apply your skills. Those application tasks that you hated at school were good for you.
While a lot of this could be done through using or examining readily available source code or libraries, it’s beneficial to be familiar with the basic concepts. Quoting howstuffworks, “it’s good to know”.
21 Comments so far
Leave a comment2.5 is the time the ball takes to travel 1.5 meters.
of course in real life gravity would enter the equation
is it right?
recorded by André Neves on April 26, 2007 8:06 pm | Permalink
hum…somehow the code got lost
let’s try again…
function getHeight(t = 0) {
return ((int)$t / (2.5 / 1.5));
}
stated by André Neves on April 26, 2007 8:07 pm | Permalink
oh…thats PHP by the way
Is it right??
recorded by André Neves on April 26, 2007 8:08 pm | Permalink
ups my bad…forget it…douh!
expressed by André Neves on April 26, 2007 8:13 pm | Permalink
I was wondering if that’s what you thought of:
#includedouble bounce(double seconds)
{
return 1.5cos(1.25seconds+M_PI)+1.5;
}
recorded by Max on April 26, 2007 8:24 pm | Permalink
Well done Max. That works out perfectly.
Andre, in PHP that would be something like
Although this is simple harmonic motion, I initially thought of something more bouncy… with absolute values. Like:
The square and square root are just a complicated way of getting the magnitude or absolute value, so you look cool ;). I’m not sure how that would appear animated, though. Most applications would tend to use plain cos or sin as in the first example (Max’s technique). I think Apple do that too most of the time in their sample code.
That wasn’t much of a challenge, was it? I’ll have to think of something harder next time.
posted by Ankur on April 26, 2007 9:12 pm | Permalink
Man, I was thinking way to physical (is that the right word? Probably not… physicsish?)
x = 1/2 * a * t^2
6 = 1/2 * a * 5^2
a = 0.48, no air resistance -> you must be on the moon!
stated by sphynx on April 26, 2007 9:41 pm | Permalink
yeah, harder please!
uttered by Max on April 26, 2007 10:01 pm | Permalink
I also used the sin function, cos just does not look right.
def bounce(time)
sin((time % 10 * PI) / 10) * 3
end
stated by tako on April 26, 2007 10:07 pm | Permalink
Sphynx, not only air resistance, but gravity, atmospheric pressure, friction… hehe.
Max, the next one’s almost ready, and it’s a little harder; but one of these days I’m going to think of something so twisted…
Tako, you can do it with sin or cos, it doesn’t really matter, but your period is little big, and the ball seems to go underground 3 metres… probably just a simple error.
voiced by Ankur on April 26, 2007 10:51 pm | Permalink
Must have misread the instructions, and I assumed 10 sec. for a full cycle. Replacing the two instances of 10 in the function fixes it.
announced by tako on April 27, 2007 12:06 am | Permalink
“Programming, real programming, requires a keen understanding of what you want to do, what you’re doing, and what needs to be done.”
What if - as is generally the case - bouncing balls isn’t what you want to do, isn’t what you’re doing, and isn’t ever going to be what needs to be done?
voiced by Jon H on April 29, 2007 3:06 pm | Permalink
That’s a specific example. Whether you’re talking about a bouncing ball or a hopping kangaroo or a simple HTTP POST is irrelevant. The principles still apply.
announced by Ankur on April 30, 2007 4:04 pm | Permalink
” The principles still apply.”
What, that you need domain knowledge? Or “how important it is as a programmer to be familiar with math and physics”
Where I work, you’d want to have financial calculation formulas (from NPV to pricing derivatives) down pat.
Even fewer coders are going to come out of school knowing that than come out knowing F=ma, etc.
uttered by Jon H on May 3, 2007 7:30 am | Permalink
Jon, you’ve just proved the point I’m trying to make. Where you work, you need to familiar with formulae regarding finance. For someone else, it might be biochemistry or, as you’ve mentioned, physics. The point I’m making is simply this: that it’s important to be familiar with the underlying concepts of what you’re doing. If you’re not acquainted with the details, you’ll be easily thrown when something more complex comes along.
For instance, you can teach someone a formula, but if they don’t understand how it works or is derived, there’s a good chance they’ll stumble at the smallest sign of change from the standard application.
For example, if someone is able to differentiate without knowing what they’re doing, asking them to differentiate ax is likely to get them confused. I can’t think of a better example right now, but I hope that makes sense.
stated by Ankur on May 3, 2007 4:12 pm | Permalink
I’m not quite sure why all those people are using trig functions. This is mine:
f(t){ return 2.4*(t % 5) - .96*(t % 5)/2 }recorded by Chris on May 13, 2007 5:40 pm | Permalink
I find this really disturbing. Chris was the only one to get close to the right answer (he was off by some reordering and a set of parentheses), which is:
f(t) { return (t%5) ( 2.4 - 0.48 (t%5) ) }If you are trying to model a bouncing ball, you need to use physics. Period. And this is not hard physics. Anybody who has taken one semester of freshman physics, or even a half decent calculus course, should know how to do this.
Guessing that the answer is a trig function and fidding until you get it to look sort of right is not good enough, particularly if you want four decimal places worth of accuracy. Get out a piece of paper and a pencil and actually work out the equations before you start coding.
If this were an assignment in my numerical computation class, I would have failed you all, except for Chris who gets an A-. If I were your boss at a software company, I would probably accept the answer with trig functions, but only because I wouldn’t know better.
I tell my computer science students that if you really know your mathematics, you’ll probably be the only person at the company who does. Then, when the layoffs come, you’ll be indispensable.
I tell my colleagues that even if my students don’t learn the mathematics, hopefully they’ll learn enough to appreciate when they need to hire a mathematician.
disclosed by Professor Landweber on May 26, 2007 12:36 pm | Permalink
Professor Landweber,
That reminds me of a demonstration I heard about. A physicist showed his class how a ball modeled a parabola by sketching one on the blackboard and then tossing a ball. It followed the curve every time.
Not sure if it’s true, since I’ve only heard about, but I imagine it would have taken some practice.
Regarding the parabolic function to model the bouncing ball, you’re absolutely correct that it would much more accurate; though I must confess, I have never seen it done in any application. It could be due to my inexperience, I suppose.
Regarding Chris’ solution, you say:
I thought it was just the square (
^2) missing. ie:return 2.4*(t % 5) - .96*(t % 5)/2becomes
return 2.4*(t % 5) - .96*(t % 5)^2/2Which is the correct answer, albeit the
0.96/2could be simplified to 0.48 as you have done.mentioned by Ankur on May 26, 2007 9:36 pm | Permalink
Ankur,
The difference between
2.4 * (t%5) - .48 * (t%5)^2and
(t%5) * ( 2.4 - .48 * (t%5) )is that the former takes 2 multiplications, a subtraction, and a power operation (which the compiler might optimize as a multiplication), while the latter takes just two multiplications and a subtraction, saving a few processor cycles. This way of expanding polynomials is covered in most textbooks on numerical computation. Incidentally, doing this via polynomals is significantly faster than using trig functions.
If I had more than one line of code, I would probably cache t%5 (which I would hope the compiler would store in a register), since that is the most time consuming operation in the code.
As for the difference in appearance between using trig functions and the actual physics, the trig function version will feel just a little bit off. The human brain has a surprisingly good appreciation for the laws of physics.
– Greg
expressed by Professor Landweber on May 27, 2007 11:46 am | Permalink
That makes sense. Using a trig function does in fact appear like the ’simple harmonic motion’ that it’s meant to be, while the quadratic appears more like the real thing.
abs(sin)is similar too, but as you said, it’s not as accurate.These slight optimizations to code are really intriguing; for example, that
x*xis faster thanx^2etc. I never really think much about what’s happening underneath the code on a compiler / processor level, but I’ve always known that a good knowledge in these details definitely helps a great deal in writing and optimizing. Of course, I’ve still got a long way to go since I’m not out of school yet (did I say that out loud?), but I can appreciate how much improvement can be made by simply understanding what you’re really doing.revealed by Ankur on May 27, 2007 11:58 am | Permalink
Leave a comment