After doing this, the value of k is increased by 1. Starting with k = 2, it then computes U(k) from U(k-1). The program begins by defining the parameter alpha and the value of U(1). This sequence of steps can be represented visually as a flow chart: The range of steps to be taken is listed at the top of the for loop - we told MATLAB to run k from 2 to 4 (in increments of 1, the default for the : operator). As we will see, not all iterations involve taking the value from a previous step and using it in the present step - although this will often be the case. This process stops when k takes the value of 4. With k = 3, we get the step from k - 1 = 2 to k = 3 - that is, from U(2) to U(3) - and so on and so forth. If we start with k = 2, then we get the step from k - 1 = 1 to k = 2 - that is, from U(1) to U(2). But what is k? It is a counter of where we are in the iteration. Note the structure of the for loop: we only have to tell MATLAB once how the iteration works, from step k - 1 to step k. We get the same answer, but with a more compact program. The for loop provides a much more efficient way of calculating U(4) calculation: clear all Similarly, U(2) is the amount after the first billion years has elapsed, U(3) is the amount after the second billion years, etc. U(1) corresponds to the amount of U238 at the first time considered - which in this case is the initial amount, at "time" n=0. Remember that the indices of the array must be positive integers. U(1) is the first element of U - corresponding to the time n=0 in the notation of Section 1.1 of the notes. īefore carrying on: note the indices of the array U. This is a nuisance, and impossible in practice for bigger calculations - what if you had to go out to n=50? What are the chances you'd make a typo? Pretty large in fact. Note that we've had to type the same thing over and over: take the result of the previous calculation and multiply by the same factor. 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. ** Doubles the first 5 elements of the array */įor (int i = 0 i < values.We will now consider our next example of fundamental concepts in computer programming: the for loop (more generally, ``iteration''). What will the following code print out? Can you write a similar method called tripleFirstFour() that triples the first 4 elements of the array? Make sure you test it in main. It is created in the constructor and changed or accessed by the methods. Notice that it uses a complex conditional ( &) on line 14 to make sure that the loop doesn’t go beyond the length of the array, because if you had an array that had less than 5 elements, you wouldn’t want the code to try to double the 5th element which doesn’t exist! Notice that in this code, the array is a private instance variable of the class ArrayWorker. The following code doubles the first five elements in an array. You can loop through just some of the elements of an array using a for loop. You don’t have to loop through all of the elements of an array. You must start at the length of the array minus one.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |