
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.
#HOT TO MAKE FOR LOOP IN MATLAB FOR STEP HOW TO#
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'').
