MATLAB While Loop Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is a tutorial on how to use while loops in MATLAB while loops allow the execution of a block of code until a certain condition is met unlike for loops which run for a predetermined number of iterations while loops can continue indefinitely so long as their condition is unmet this can be very useful when the number of iterations is not known beforehand such as when you want to continue to evaluate some expression until there is convergence for example or until you reach a certain state so let's look at the general form we start a while loop with a statement while then there is a logical expression then there is a block of code here and end so this is it basically what it says is that while this logical expression is true we're going to evaluate this code once the logical expression becomes false we exit and move beyond the end statement let's look at some examples so let's say we have a variable a equal to one and we'll say that while a is less than 10 we will increment a by 1 and that's it if we if we do not suppress the output here we can see what's actually happening and what we get is the following first of all I started equal to 1 then the first loop iteration we made a equal to a plus 1 so it starts off at 2 the first time that it displays then it goes 2 3 4 5 6 7 8 9 and finally 10 once a gets incremented beyond 9 it becomes 10 the next time that this while loop looks at a logical expression it's no longer true since 10 is not less than 10 and therefore we exit the while loop notice that something inside the while loop has to change the logical expression otherwise the while loop will either never execute or will never exit if a did not increment throughout this while loop then this expression here would never become false let's look at a more interesting example let's say that we want to put some money in the bank and subject to a yearly interest rate we want to see how long it will take until we reach a million dollars for example and can retire so the structure of this is we'll start off with a given balance let's say $5,000 and we'll start off the year we have to count it ourselves now at zero while balance is less than 1 million balance will equal 8 percent interest times the previous balance and at the beginning of every year we're also going to deposit another $5,000 we have to increment year ourselves to count how many years it will take because unlike a for loop nothing is actually incrementing automatically and that's it we can end it here and at the end we can display the year how long it took how many years it took and we can also display the actual balance that we've accumulated so let's run this code and here is the answer we get that it took 36 years and we have just over a million dollars by the end so what happened was we started off with 5,000 years equal to 0 the first time this while loop looks at this logical expression balance is less than a million it's equal to 5,000 therefore it goes in increments balanced by multiplying by 1.08 and adding 5,000 and the Year gets in as well so now year is equal to 1 and balance is equal to a new value corresponding to this product and sum now the while loop again after everything is completed here looks is balance less than a million it is and it keeps going until let's say year is equal to 35 when year is equal to 35 balanced after this product and sum will be just over a million but that doesn't mean that we exit immediately because there is more code in the body we also increment year by 1 and year now becomes 36 now we go back to the beginning and the while loop again looks at a logical expression and it seems that balance is no longer less than a million therefore we exit and continue with the rest of the code display in the year and the balance which is exactly what we see here in the command window while loops can also be used just like for loops for example let's say that we have a vector 1 through 10 and we want to find the sum of this vector we can have a variable sum a which is going to be keeping our current sum and we'll also have to declare a counter ourselves and we can say that while counter is less than or equal to the length of a we're going to make some a equal to sum a plus a of counter and counter is equal to counter plus 1 will be incrementing that ourselves and at the end we can display the final value in the sum so again I emitted a semicolon here to show what happens and let me actually meet it on the counter as well when we run this let me clear the screen first when we run this we get the following results we see that some a starts off as 1 because we add the first value of a which is one two zero which is what some a starts with and counter increments to two since at first it was one and then we check again is counter less than the length of a yes right now it's two so then we go on to the next value and we add a of two to the previous sum and increment counter again and again we check is counter less than or equal to the length of a and it keeps going in this way we can see these outputs counter keeps increasing by one by one by one and here we go at the end we see that some a is equal to 55 so this is one counter is equal to ten the length of a so it's still less than or equal to so it goes to loop one more time some a becomes 55 counter becomes 11 we perform this check again counter is no longer less than or equal to the length of a because 11 is greater than 10 and so we exit and move on to the end and display the sum of a in general you have to remember that in wild loops we have to change something in the expression in order to make this logical expression false at some point otherwise we will remain in the loop indefinitely the difference of course between this and a for loop is that in a for loop we had a counter that changed automatically on every loop iteration however if we do this ourselves we can emulate the exact function of a for loop using a while loop altogether while loops are an important tool in a programmers Arsenal although for loops make iteration easy they are not as versatile as while loops while loops give you full control over all aspects of the iteration including any indices that you want to increment they also allow you to have indices that increment non-uniformly but rather depend on conditions of variables in the code this versatility makes while loops an extremely important construct in a great programming tool
Info
Channel: Ilya Mikhelson
Views: 198,410
Rating: 4.9115043 out of 5
Keywords: MATLAB (Programming Language), While Loop
Id: t-W_a-yG2ns
Channel Id: undefined
Length: 8min 49sec (529 seconds)
Published: Sat Oct 12 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.