hksite.blogg.se

Indexing values in for loop in matlab
Indexing values in for loop in matlab










indexing values in for loop in matlab

In the above example, that's a single calculation - but it doesn't have to be. When k exceeds 4, the iteration stops and the array is written to the screen.Īt each iteration, MATLAB does everything between the "for" and "end" statements in the loop. 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.

indexing values in for loop in matlab

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.

indexing values in for loop in matlab indexing values in for loop in matlab

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.

#Indexing values in for loop in matlab 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'').












Indexing values in for loop in matlab