Тёмный
No video :(

MATLAB While Loop Tutorial 

Ilya Mikhelson
Подписаться 8 тыс.
Просмотров 234 тыс.
50% 1

Опубликовано:

 

19 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 117   
@anonymousnyjreporting4898
@anonymousnyjreporting4898 3 года назад
I have a great teacher but I only have him once a weekl. You are saving my grade right now.
@IlyaMikhelson
@IlyaMikhelson 3 года назад
Glad to help!
@brianevans4975
@brianevans4975 9 лет назад
Thanks for the great tutorial, you're much better than my lecturer!
@greekfox21
@greekfox21 9 месяцев назад
i study naval engennering and matlab is a big part of the first years, thank you so much for the expalnations and all this confusion
@IlyaMikhelson
@IlyaMikhelson 9 месяцев назад
My pleasure!
@xujiaqi6520
@xujiaqi6520 7 лет назад
got midterm tomorrow(literally today), this video just give me 5 more points!
@IlyaMikhelson
@IlyaMikhelson 7 лет назад
I hope it goes well!
@elcanaldegaming6677
@elcanaldegaming6677 3 года назад
Did it go well?
@xujiaqi6520
@xujiaqi6520 3 года назад
@@elcanaldegaming6677 it went great
@candicerusso7110
@candicerusso7110 3 года назад
Thanks! This is the kind of example I've been looking for.
@IlyaMikhelson
@IlyaMikhelson 3 года назад
You're welcome! Glad I could help!
@babbababbalala46
@babbababbalala46 6 лет назад
Your presentation and explanation is great.
@IlyaMikhelson
@IlyaMikhelson 6 лет назад
Glad to help!
@mpiana3
@mpiana3 6 лет назад
We all know how to become millionaires now!
@IlyaMikhelson
@IlyaMikhelson 6 лет назад
I know, right? It's so simple :)
@rjc3343
@rjc3343 6 лет назад
Well done! Very clear and concise, thank you.
@IlyaMikhelson
@IlyaMikhelson 6 лет назад
My pleasure!
@barbaracarnauba1507
@barbaracarnauba1507 4 года назад
Excellent explanation!! Thanks for posting this video!!!
@IlyaMikhelson
@IlyaMikhelson 4 года назад
You're very welcome!
@leulassamenew1104
@leulassamenew1104 10 лет назад
Thanks this helped me finish my homework.
@shakedmani6899
@shakedmani6899 2 года назад
So useful. thanks alot buddy!
@IlyaMikhelson
@IlyaMikhelson 2 года назад
My pleasure!
@xiaojunwang3534
@xiaojunwang3534 7 лет назад
explains very well
@marieflauto
@marieflauto 9 лет назад
thanks for the helpful and clear explanation!
@amrit6755
@amrit6755 7 лет назад
Write the JavaScript to read a positive number from the user and then print to the console the numbers from 1 to the user entered number. Ex. If the user enters 50, you should print the numbers 1 to 50 in the console.
@IlyaMikhelson
@IlyaMikhelson 7 лет назад
This video is about MATLAB, not JavaScript. I could help you solve the problem in MATLAB, although I would use a for loop, not a while loop. For example, you could use: x = input('Please enter a number: '); for ii = 1:x disp(ii); end
@theflash2873
@theflash2873 3 года назад
Thanks!
@IlyaMikhelson
@IlyaMikhelson 3 года назад
My pleasure!
@audunlode9798
@audunlode9798 10 лет назад
Two questions: 1. At 1:17 you make the 3 lines "%" how do you do that? 2. how do you get that big letters? Btw thanks for the video. really helped - this is actually easypeasy
@IlyaMikhelson
@IlyaMikhelson 10 лет назад
I'm glad the video was helpful! To answer your questions: 1) In the editor, there is a button (under the "Edit" menu) called "Comment". You can highlight text and press the button, and it will comment the highlighted text out, or you can use a shortcut (Ctrl+r on Windows), which is what I did. To uncomment, there is also a button for that, or a shortcut (Ctrl+t on Windows), which is what I use. 2) To get big letters, or to change the font in general, in the command window, under the "Environment" menu, click on "Preferences". A new window will open, and in the left menu, select "Fonts". You should then see options for font type, text style, and text size. I used size 24 in the videos.
@mohammadnazmulhossain8805
@mohammadnazmulhossain8805 6 лет назад
There is a formula for compound interest C=P(1+r)^n; where P= Principal/ Initial Amount; r= Rate of Interest; n= # of year. In this case we have to find when our balance will reach 1,000,000? While it is given that initial amount is 5,000 and rate of interest is 8%. Ans. In this case 5,000*(1+.08)^n >=1,000,000 => (1.08)^n >=200. So n >=68.84. So you need 69 years to surpass 1,000,000. After 69 years your balance will become $1012066.69. But your program calculated it 36 years and 1,015,400. Make me correct if I am wrong.
@mohammadnazmulhossain8805
@mohammadnazmulhossain8805 6 лет назад
Correct program should be bal=5000; year=0; while (bal
@IlyaMikhelson
@IlyaMikhelson 6 лет назад
Don't forget that I am also depositing $5000 each year!
@aldogama999
@aldogama999 5 лет назад
best classes
@IlyaMikhelson
@IlyaMikhelson 5 лет назад
Thanks!
@isurusenevirathne
@isurusenevirathne 6 лет назад
it is good for Basic While loop in Mathlab
@joshuarichards6442
@joshuarichards6442 7 лет назад
Thanks, helped a lot
@saidteacher3331
@saidteacher3331 Год назад
Cool 😎 thanks
@aymanalzahrani7193
@aymanalzahrani7193 5 лет назад
man I really love u . u make the life easier with Matlab , but if u don't mind, what's the difference between for loop and while I know if statments only used for bigger, smaller or qual to
@TheCardmaster99
@TheCardmaster99 9 лет назад
Thanks for the help man, much appreicated :)
@Techformative557
@Techformative557 6 лет назад
Your For loop video and this saved my ass man! thanks soo much!!
@IlyaMikhelson
@IlyaMikhelson 6 лет назад
I'm very glad to help!
@Techformative557
@Techformative557 6 лет назад
subscribed...I got an A on the exam thanks to your vids..much appreciated man
@IlyaMikhelson
@IlyaMikhelson 6 лет назад
That's awesome to hear!
@sitinurbaya6157
@sitinurbaya6157 3 года назад
Let's say I want to store the balance in example 2 in a matrix - thus it would be a 36 x 1 matrix - how can I do that?
@IlyaMikhelson
@IlyaMikhelson 3 года назад
The easiest (though not the most proper) way to do this would be to declare an empty matrix before the loop, and to concatenate the next result to this matrix after every loop iteration. For example, something like this: bal = 5000; year = 0; bal_vec = []; while bal
@Mikej1592
@Mikej1592 9 лет назад
I wondered in real world terms ie. getting half a percent interest as in real bank accounts savings, and able to deposit 1000 a year to see how long it would take to get to a million dollars. Just have to live 359 years and I would have 1.0045e+06 dollars... while that is seriously depressing, I did find this a very helpful tutorial.
@IlyaMikhelson
@IlyaMikhelson 9 лет назад
Mikej1592 Real life can be depressing, but the world of MATLAB is always happy :) Go ahead, set the interest rate to 20%!
@morganfudgsiclemonster9106
@morganfudgsiclemonster9106 9 лет назад
I love you. Thank you so much.
@bootsnoot6510
@bootsnoot6510 10 лет назад
this is fantastic. I am going to show this to my students, i am a lecturer at a university. Could i save the video and show it later, offline??
@IlyaMikhelson
@IlyaMikhelson 10 лет назад
I'm glad you found it helpful! Please feel free to use it for educational purposes.
@jointcc2
@jointcc2 7 лет назад
hi ive got a question, what is a(counter) and does sum_a equal 0 in every new loop and a(counter) just becomes 1,3,6,10... respectively to make the sum equal to 1,3,6,10...etc.?
@IlyaMikhelson
@IlyaMikhelson 7 лет назад
The variable "a" is a vector, and "counter" is a scalar. Therefore, "a(counter)" is a given value of "a". For example, if "counter" = 5, then "a(counter)" = 5, since the 5th value of "a" is 5. The variable "sum_a" is keeping track of the current sum of the elements of "a". In every loop iteration, the next value of the vector "a" gets added to the running total in "sum_a". In general, no variable will become 0 at the start of a loop iteration unless it is explicitly declared to do so. Here, "sum_a" is only declared to be 0 once outside of the loop, and therefore its value will not be reset.
@jointcc2
@jointcc2 7 лет назад
Thanks a lot! I actually find this tutorial really helpful. Before I saw this tutorial I was very confused about the structure of the while loop. Now I finally understand what is going on here...appreciate your help doc!
@jigarpatel6931
@jigarpatel6931 7 лет назад
awesome!
@IlyaMikhelson
@IlyaMikhelson 7 лет назад
Thanks!
@spartan159
@spartan159 9 лет назад
thanks for the help
@edwardchida2563
@edwardchida2563 7 лет назад
A BIG THANK YOU
@IlyaMikhelson
@IlyaMikhelson 7 лет назад
My pleasure!
@edwardchida2563
@edwardchida2563 7 лет назад
By the way, do you know how i can get a vector of the results(the balance after every increment) using the while loop
@IlyaMikhelson
@IlyaMikhelson 7 лет назад
The easiest method would be to declare an empty matrix, and to increase its size at each iteration by appending the current balance. An example is shown below: bal = 5000; year = 0; bal_vec = []; while bal < 1000000 bal = 1.08*bal + 5000; year = year + 1; bal_vec = [bal_vec;bal]; end This is not the best practice, since the size of the "bal_vec" vector changes at each loop iteration. This likely will not cause any noticeable differences with small computations, but is not ideal. A better, slightly more complicated approach would be to preallocate a large vector, and then to trim it at the end. An example is shown below: bal = 5000; year = 0; bal_vec = zeros(1000,1); while bal < 1000000 bal = 1.08*bal + 5000; year = year + 1; bal_vec(year) = bal; end bal_vec = bal_vec(1:year); This makes it so that the space in memory is allocated, and therefore the vector is not changing size at each iteration. We also use the "year" as a natural counter (and index) to fill in the "bal_vec" vector. However, at the end, we have to trim the vector down to contain only the results. Otherwise, it would contain a lot of zeros. I hope this helps!
@edwardchida2563
@edwardchida2563 7 лет назад
I got the vector with the 2nd method, Thanks a lot...do you have videos on root finding using the newton-rhaphson and the bisection method
@edwardchida2563
@edwardchida2563 7 лет назад
ohh and least squares fitting and interpolation
@kunukunu8154
@kunukunu8154 7 лет назад
excellent
@rafab.3308
@rafab.3308 4 года назад
The explanation was very clear, thank you! However, I have a question about the example with money investment. Is there a possibility to get exactly the value < 1 mln dollars? I mean - how to make a while loop that wouldn't count the last, "excess" value. In this example, one can see that the final amount of money is above 1 mln dollars despite the condition for the loop. Is it only possible to, e .g. display the n-1 value to do it easily?
@IlyaMikhelson
@IlyaMikhelson 4 года назад
There are many ways to accomplish this, but I think that the following is more-or-less straightforward. bal_old = 0; bal_new = 5000; year = 0; while bal_new < 1000000 bal_old = bal_new; bal_new = 1.08*bal_old + 5000; year = year + 1; end disp(year-1) disp(bal_old) The idea is that we now have two "balance" variables, one to keep track of the new balance, and one to keep track of the previous balance. This way, when the new balance passes the threshold, we can always reference the previous balance. I hope this makes sense!
@rafab.3308
@rafab.3308 4 года назад
Thank you for your answer! It's easy and it works very well.
@SalmanKhan-ep6do
@SalmanKhan-ep6do 3 года назад
Thanks for the video, it helped me lot. I am curious to know, how you put a percentage sign to more than one line in a single click, can you tell me command you used??
@IlyaMikhelson
@IlyaMikhelson 3 года назад
I'm glad I could help! To comment multiple lines, you first highlight them. Then, there is a button at the top of the editor called "comment". Or if you are using Windows, you can click CTRL+r (and CTRL+t to uncomment). I hope this helps!
@SalmanKhan-ep6do
@SalmanKhan-ep6do 3 года назад
@@IlyaMikhelson Yes! Perfectly working, Thanks a lot. I'm new to MATLAB, in case if I need any help I'll ask you, if you don't have any problem. 😊
@leosteinman3057
@leosteinman3057 3 года назад
how do you put an or function in the while loop? specifically im trying to have the loop continue until the inputted value for a variable (a) is equal to one of three values. so for example until a is equal to 1 2 or 3 it will continue to prompt the user to input a valid value for a
@IlyaMikhelson
@IlyaMikhelson 3 года назад
The easiest way to do this is to use the OR operator (||). So you would have something like "while ~(a==1 || a==2 || a==3)". For more details on logical operators, check out my Logical Expression video. I hope this helps!
@FairFuse
@FairFuse 7 лет назад
Hey I have a question: how can you make a while loop that repeats until a certain variable, vector L, does not change? I though about starting with "while K ~= L" and then put "K = L;" before the for-loop that changes L, but then the while-loop, of course, stops immediately. (To be more specific, I have an assignment for which I need to write a function that implements the k-means algorithm.)
@IlyaMikhelson
@IlyaMikhelson 7 лет назад
When you use a vector for the condition of a while loop, ALL elements must be true in order for it to execute. For example, "while [1 1 1]" will execute, whereas "while [1 0 1]" will not. Therefore, in your condition, ALL elements of K and L must have been different for the loop to keep running. Instead, you could use the "norm" function. You can then analyze the norm of the difference of the two vectors. For example, you could use "while norm(K-L) > 0", since you know that if the norm is not zero, then the vectors are not the same.
@hrnoh8005
@hrnoh8005 7 лет назад
I have a question. Why do need there '+5000' next to '1.08*bal'? I think '1.08*bal' contains deposit.
@IlyaMikhelson
@IlyaMikhelson 7 лет назад
Multiplying by 1.08 only accounts for the interest rate, since it adds 8% of the current balance. Then, since we also want to include an independent deposit, we must add a separate value. I hope this clears things up!
@hrnoh8005
@hrnoh8005 7 лет назад
+Ilya Mikhelson Okay, I see. Thanks!
@Antonio-ms3gb
@Antonio-ms3gb 3 года назад
What's a(counter) ?
@feyisolaadejokun8013
@feyisolaadejokun8013 9 лет назад
i still don't understand this line of code - sum_a = sum_a + a(counter). specifically the a(counter) part. Sorry, if its a stupid question. But i have seen you do this in your for loops video. why can't i just use ''sum_a=sum_a + counter''. I get the same answer with you if i do this. And this makes more sense to me or maybe I still don't understand the a(counter) part.
@IlyaMikhelson
@IlyaMikhelson 9 лет назад
+sola ade That's an excellent question. The reason I use a(counter) is to keep things as general as possible. The vector "a" can contain any values (not necessarily just 1, 2, 3, etc.), and the variable "counter" is a counter, which increments by 1 every loop iteration. The point, in general, is to use "counter" as an index into the vector "a" and use the next value in "a" for whatever operation you want to perform. In the example at 5:34, the vector "a" is just composed of incrementing values, so in effect, "a(counter)" is equal to "counter" itself. However, imagine if you want to add the values in the vector "a = [2 5 -4 3]". Then, "sum_a = sum_a + a(counter)" would give you very different results than "sum_a = sum_a + counter". This is because "sum_a = sum_a + counter" will always add "counter" to the running total, whereas "sum_a = sum_a + a(counter) will add the "counter"th value of the vector "a". I hope this makes sense!
@feyisolaadejokun8013
@feyisolaadejokun8013 9 лет назад
+Ilya Mikhelson Thank you so so much!. You couldn't have explained it better. You are a great tutor.
@yvonnechang6373
@yvonnechang6373 8 лет назад
hello, can matlab do infinite loop? like always show receive data from the input , thx
@IlyaMikhelson
@IlyaMikhelson 8 лет назад
+Yvonne Chang You can absolutely do that. The exact structure would depend on the application. The easiest way would be to have something like "while 1", but then the only way to get out of the loop would be a forced exit. Alternatively, you can keep looping until the input is a particular value, for example the letter "q". I hope this helps!
@yvonnechang6373
@yvonnechang6373 8 лет назад
+Ilya Mikhelson Thanks! i'll ask you later if i still don't understand, if you don't mind
@benh5351
@benh5351 7 лет назад
Just curious how to use MATLAB to find the values of two symbols? a+c=30 and 2
@IlyaMikhelson
@IlyaMikhelson 7 лет назад
You could use a for loop and go through the possible values of a and c, keeping the results when both criteria are satisfied. For example: a_vals = []; for a = -10:40 for c = -10:40 if a+c==30 && c-a>=2 && c-a
@benh5351
@benh5351 7 лет назад
It greatly helps, thank you. Where do I put the function which a and c are calculated from?
@benh5351
@benh5351 7 лет назад
The function works but give me integers from 10-14 (the interval that is set)
@harjinderooney
@harjinderooney 10 лет назад
Thank You. Just wanted to ask how do you store results in a vector for while loop? Because you wouldn't know what size zero vector to create before the while loop.
@IlyaMikhelson
@IlyaMikhelson 9 лет назад
Harjinder Singh I apologize this response is so late. For a while loop, you could create a vector of zeros that would be large enough to accommodate any output your while loop could create (e.g. zeros(10000000,1)). Then, you can keep track of your iterations with a counter and trim off the end beyond the value of the counter. Alternatively, you could use dynamic allocation. For example, you could start with an empty vector (e.g. x=[]). Then, every time you want to store a new value y, use x = [x;y]. This concatenates the running x vector with the new value, and thus actively grows it as the program runs. I hope that helps!
@HotDaffodil
@HotDaffodil 9 лет назад
How do turn what you've marked into "explanation format" so easy?(the green text with a % in front of it)
@IlyaMikhelson
@IlyaMikhelson 9 лет назад
HotDaffodil In the editor, there is a button that says "Comment". You can highlight text and click it. In the video, I used the shortcut for it, which is "Ctrl + r" on a Windows computer.
@HotDaffodil
@HotDaffodil 9 лет назад
Thanks! :D
@Jonas_s
@Jonas_s 5 лет назад
In Example 2 you have a mistake: The formula is only bal = 1.08*bel Otherwise you would earn 8% plus 5000 each year. That's why your money grows so fast. Actually it takes 69 years... Nevertheless thank you for the example.
@IlyaMikhelson
@IlyaMikhelson 5 лет назад
At 3:21 I specify that we also deposit $5000 each year.
@Jonas_s
@Jonas_s 5 лет назад
@@IlyaMikhelson Oh sorry, I missed that!
@IlyaMikhelson
@IlyaMikhelson 5 лет назад
@@Jonas_s No worries!
@taiwobabatunde9047
@taiwobabatunde9047 8 лет назад
I love your teaching, how can I get or download 34 bus system cos I have search not get it all throughout but I could not get it.thanks sir
@IlyaMikhelson
@IlyaMikhelson 8 лет назад
+Taiwo Babatunde Thank you! I do not know what the "34 bus system" is, though. Could you please clarify?
@taiwobabatunde9047
@taiwobabatunde9047 8 лет назад
i have a code for harmonic flow and power flow but i dont understand it.i want to know if you can be of help to run it the matlab because anytime i run it brings NaN
@IlyaMikhelson
@IlyaMikhelson 8 лет назад
+Taiwo Babatunde I can help with general MATLAB concepts, but not with individual problems. If you have a general "while loop" question, I'd be happy to help.
@dhruvo100
@dhruvo100 9 лет назад
at 8:50 could u explain a(counter)??
@IlyaMikhelson
@IlyaMikhelson 9 лет назад
I am assuming you are referring to the example at 6:05. Here, the variable "a" is a vector of values. The variable "counter" is our index. We go through each value in "a", one by one (by incrementing "counter" by 1 every iteration), and add that value to the running total ("sum_a"). I hope that makes sense.
@mB-mp6io
@mB-mp6io 3 года назад
you put the interest rate of 108% not 8% though
@IlyaMikhelson
@IlyaMikhelson 3 года назад
Don't forget that: 1.08*bal = (1 + 0.08)*bal = bal + 0.08*bal. Hence, we are adding 0.08*bal to the previous bal.
@mB-mp6io
@mB-mp6io 3 года назад
@@IlyaMikhelson you mean balance + interest plus saving of 5000 each year. I don’t think this will hit million dollar by 37 years
@IlyaMikhelson
@IlyaMikhelson 3 года назад
@@mB-mp6io Compounding returns truly are amazing :)
@amrit6755
@amrit6755 7 лет назад
plz help
@ShaunJW1
@ShaunJW1 9 лет назад
I'm using Octave GNU. My fuck is it a piece of shit....
@ser7ser7i
@ser7ser7i 5 лет назад
Thank you for the tutorial. Faiz haram.
@IlyaMikhelson
@IlyaMikhelson 5 лет назад
You're very welcome!
@filipivanovic9556
@filipivanovic9556 3 года назад
dado i sako polumenta
@islomkhodjaev9653
@islomkhodjaev9653 4 года назад
Can you not chew your gum while recording?
@IlyaMikhelson
@IlyaMikhelson 4 года назад
I have never chewed gum while recording...
@au313
@au313 4 года назад
wtf is with your = sign?!!
Далее
"While Loops" and "For Loops" in MATLAB
9:24
Просмотров 21 тыс.
Basics of While Loops
13:14
Просмотров 37 тыс.
How This New Battery is Changing the Game
12:07
Просмотров 44 тыс.
MATLAB Debugging Tutorial
10:40
Просмотров 54 тыс.
MATLAB Branch (IF/ELSE) Tutorial
12:46
Просмотров 31 тыс.
Scammed selling my MacBook, almost
15:09
Просмотров 34 тыс.
MATLAB Logical Indexing Tutorial
9:14
Просмотров 26 тыс.
Lesson 6.1 for-loops in MATLAB
36:51
Просмотров 133 тыс.