Going back to our radioactive decay example: to compute the mass of U238 present after 3 billion years have elapsed from some initial time, we could just type out the sequence of calculations above by hand, one after the other. What we're learning now is at the heart of how computers work. Computers don't get bored - they'll happily do the same thing over and over again. This can be incredibly tedious to do by hand - and errors can creep in, through carelessness or boredom. Many important applications involve doing similar calculations repeatedly, with each step making use of the results of the previous step (that is, using iterations). But there's another way to do the calculation - using the for loop.īefore talking about the for loop, it's worth emphasising the following fact: computers were invented to do exactly this kind of computation. In general, iterative calculations don't admit closed-form expressions for the nth step in terms of the first. In this case, we have a formula predicting U(n) from U(1), so we can just use the exponentiation function - we know how to take the nth power of some factor. To predict U(n), we start with U(1) and iteratively multiply by the factor 1-alpha. Starting with some amount of U238, we know that each billion years we lose a fraction alpha, that is, U(2) = (1-alpha)U(1) U(3) = (1-alpha)U(2) = U(1) U(4) = (1-alpha)U(3) = U(1)Īnd so on. We talked in class about an example of an iterative calculation - radioactive decay. We will now consider our next example of fundamental concepts in computer programming: the for loop (more generally, ``iteration'').
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |