Loops and Execution Control

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay hello and welcome to the matlab programming for computation course ah are we are in module 1 introducs introduction to matlab programming we are going to do lecture 1 dash 3 over here were we are going to cover loop and execution control specifically we are going to talk about for and while loops. okay the loops the2 main loops in matlabs are ah shown over here for loop and the while loop the command for loop is of the form for i equal to 1 colon 10 a bunch of statements followed by the command end okay so these number of these statements are going to be repeated 10 times lets say we wanted to repeated for values of i equal to 1 3 5 so on and so forth until 10 okay we will use the same column notations as before we will say 1 colon 2 colon 10 and that should work for i equal to 1 to 10 in steps of 2 okay so this is something again what we have seen 1 of the earlier lectures we can use the same concepts in order to use it in the for loop the other loop is called the while loop the while loop commands will be executed as long as the condition that we state over here is true so let say we start with i equal to 1 and we keep repeating as long as the value of i is less the 10 so we will repeated for the value of i equal to 1 i equal to 2 i equal to 3 so on and so forth until i equal to 9 when the value after executing this for the ninth time when we come across this particular statement value of i is going to be equal to 10 so i equal to i plus 1 will be 10 when we then go for this particular statement with the execution going back to the first line right now i equal to 10 which violates the command va which violates the cov condition i less than 10 because i equal to 10 is not less than 10 and it will exit this while loops so if i write this particular loop with starting with i equal to 1 this loop is going to be executed 9 times okay so where to use for loops for loop should be used when a set of operations are to be repeated a specific number of times if we know ah approximately how many number of times the particular operation is going to repeated we we should use for loops for example if we want to find the first 10 terms of fibonacci series we can use for loop for doing this ah find a factorial of a number n we can use a for loop because we know that we need to use this do this for a specific number of times now when to use while loop while loop is used when a set of operations is to be repeated until a certain condition is met for example if we want to find fibonacci series not the first tren 10 terms of the series buz but the while find the values till the value of the last term becomes grey ah ah the until that vale of the last term is less than 200 then we will use a while loop because what we want to do is not repeat a particular statement for a specific number of times but we want to repeated until a condition is met so lets go to matlab and do this fibonacci series example using a for loop so lets say edit a fibo using for okay and its going to prompt me yes okay i am going to write a comment over here fibonacci series using for loop let say n equal to 10 we wanted to repeated 10 times fibo equal to 1 comma 1 okay why 1 comma 1 we need 2 elements in order to start the fibonacci series ah if you recall fibonacci series is any nth element of the fibonacci series is just the sum of n minus 1 and n minus 2th element so the first guy is 1 second guy is 1 the third 1 is 1 plus 1 that is 2 the fourth 1 is 2 plus 1 that is 3 the next guy is 3 plus 2 that is 5 the guy after that is 5 plus 3 equal to 8 so on and so forth okay so now we will use for loop for i equal to 3 to n why 3 because we already have the first 2 guys okay so we are not going from 1 to n we are going from 3 to n and we will say fibo i that is the next element is going to be equal to fibo i minus 1 plus fibo i minus 2 and end okay see you this and run this fibo using for fibo using for and press enter and i am going to get the fibonacii series you can see this in the work space over here i i will type this out by typing fibo and pressing enter so we now have the first 10 terms of the fibonacci series okay so lets say now we wanted to convert it into a fibonacci series using a while loop and we wanted to do this until theelement becomes equal to or le greater than 200 the last element has to be less than 200 and thats the ah condition that we will use so edit fibo using while okay okay so we have fibo equal to 1 comma 1 and let me put ah comment over here fibonacci series until 200 using while loop okay so fibo 1 comma 1 okay while fibo end which is the last element of fibo is less than 200 so as long as the last element of fibo is less than 200 i am going to repeat the step ah fibo new equal to fibo end that is the last term from the previous series and fibo end minus 1 okay now next what we are going to do is we are going to expand the array fibo with the fibo new term and i will say this as fibo comma fibo new okay square brackets not circular brackets over here and end okay and once i do this i am going to be able to run this particular ah ah code until the value of the fibonacci series reaches up to 200 or below 200 okay so this is the same thing that did earlier initializing fibo as 1 comma 1 until the last element of the vector fibo is less than 200 the while loop is going to be repeated the new element which has to be appended at the end of the fibo is going to be equal to the sum of the last 2 elements the last element is extracted using the code word called end end extracts the last element of any vector or an ah ah array so fibo end is going to extract the last element of a fibo and fibo end minus 1 is going to extract the second last element so i am sorry it should be plus and not minus over here okay and then fibo is going to be fibo and the last element that has been extracted and i save this and run this fibo using while and press enter get my fibo i type fibo and this is what i get so 1 1 2 3 5 and so on up to 233 and the reason why i actually got that ah element 233 is because this loop was repeated when the element 233 was already present in the fibo loop and if i did not want that particular element in the in the fibo array i just need to do fibo end equal to blank what this is going to do is it is going to replace the last element with null which basically means thats nothing but killing the last element and i can do that and let let me now try that over here using fibo using while okay and now when i type fibo i am going to get 1 to 144 the 233 which is 89 plus 144 is no longer present the other way to do this instead of using fibo end equal to blank the other way to do this is to do type fibo equal to fibo 1 colon end minus 1 okay that is the other way to type this this is the type of format that will have been using so for is this is the standard colon notations and colons notations is going not from 1 to end but not 1 to end minus 1 so all the elements except the last element is going to be extracted out of the ah array fibo and reassigned with the same array fibo okay lets try this out again i will give the command clear all to clear all the works works space arrays okay and type fibo using while no that i am not typing the command i know that i have recently used that command so i am just going to use up array in order to select the previous command and type enter and type fibo and i am going to get the fibonacci series okay what we have get computed in fibonacci series until 233 and when it comes across this particular command its going to extract the first 12 elements not the last element from that particular array so we will have the array up to 233 and then the last element is going to be removed and the first 12 elements are going to be reassigned to the array fibo okay so that is how we have calculated first 10 terms of an fibonacci series are all the terms of fibonacci series less than 200 okay now lets where think about the example that we saw in the first lecture that was of dhoni hitting a 6 in that example what we did was ah dhoni has hit the ball and we were trying to compute ah ah how the ball trajectory is going to be until ah the ball reaches the ground lets create us another example were the ball is hit vertically instead of at an angle lets say that the ball is the ball is hit vertically with a velocity of v0 okay so the ah when its hit vertically with a velocity of v0 the gravity that is acting against it in the downward direction so the ball is going to go vertically upwards is going to reach a certain height which velocity is going to 0 and then its going to fall downwards under gravity so lets say we wanted to find location of the traject of the ball that is thrown upwards at the end of every 0 point 1 seconds we are going to use this particular calculation in order to get y at every 0 point 1 seconds so lets do that using a while loop okay so we will say edit ball vertical okay and i will say display location of ball hit vertically every at every 0 point 1 seconds okay so let say v0 was equal to 20 metres per second gravity was equal to 9 point 8 metre square per second okay so we will do while y greater than are equal to 0 we are going to repeat this equal to greater than sorry greater than are equal to 0 we are going to repeat this for this we need to predefined y y equal to 0 and t equal to 0 okay so t is equal to t plus 0 point 1 because we want to compute at the end of every 0 point 1 seconds y is going to be equal to a v0 multiply by t minus g multiply by t square divided by 2 okay and we want to display display this at t equal to num2 str t comma location equal to y we have not yet covered how to ah work work with ah a strings this is something that we are going to cover in next lecture okay ah how so just bare with me i will take this particular example again in in the fifth lecture 1 dash 5 lecture and we will go over it and we will say end okay what i am doing over here is i am at each step when whenever we are repeating this particular array okay i am calculating the new time i am calculating the location y at that particular time and i am displaying the time and location y okay i am going to repeat that so long as y is greater than or equal to 0 that means the ball is in the air and has not reached the ground okay so let me do that i will say ball vertical and press enter and this is what i am going to get okay so i started at point 1 at time point 1 we have reached 1 point 9 meters at point 2 seconds we have reached 3 point 8 meters so on and so forth and at time 4 point 1 we have reached below the surface okay the equation is valid only until the ball reaches the surface is no longer valid beyond that so we are getting an incorrect result over here just as before what has happened over here is that particular loop was executed until the value reached a value before a negative value if we dont want that to happen what simply we should we should be doing is we should be displaying before and not after computation of that value and if we do that and we execute i will just clear the screen and i will execute the ball vertical what is going to happen is the location will be displayed until the ball is in the air not when the ball has reached the surface and again you will see that it is starting at t equal to 0 location in is equal to 0 instead of starting at t equal to 0 point 1 okay so this is the thing that i wanted to cover with respect to for loops and while loops so just to recap when to use for loops is for loops is to be used when a set of operations is to be repeated a specific number of times where as the while loops isto be used when a set of operations is to be repeated until a certain as long as sorry certain condition is met okay okay so with this we come to an end of this lecture in this lecture we have covered how to use for and while loop ah in ah a matlab thank you and see you in the next lecture
Info
Channel: MATLAB Programming for Numerical Computation
Views: 163,468
Rating: 4.8712444 out of 5
Keywords: Lec, Loops, and, Execution, Control
Id: y_Fk3uQVJfs
Channel Id: undefined
Length: 17min 43sec (1063 seconds)
Published: Fri Dec 18 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.