MATLAB While Loop Tutorial

Поділитися
Вставка
  • Опубліковано 20 гру 2024

КОМЕНТАРІ • 120

  • @anonymousnyjreporting4898
    @anonymousnyjreporting4898 3 роки тому +2

    I have a great teacher but I only have him once a weekl. You are saving my grade right now.

  • @brianevans4975
    @brianevans4975 10 років тому +5

    Thanks for the great tutorial, you're much better than my lecturer!

  • @Ivanwu-0324
    @Ivanwu-0324 3 місяці тому +2

    At 2:37 how did you put % in the same time opal the beginning of the 4 lines?

    • @IlyaMikhelson
      @IlyaMikhelson  3 місяці тому

      @@Ivanwu-0324 on windows, I pressed ctrl+r to comment everything that is highlighted. There is also a key at the top of the screen that will do the same. It is labeled Comment.

  • @greekfox21
    @greekfox21 Рік тому +1

    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

  • @babbababbalala46
    @babbababbalala46 7 років тому +2

    Your presentation and explanation is great.

  • @barbaracarnauba1507
    @barbaracarnauba1507 4 роки тому +1

    Excellent explanation!! Thanks for posting this video!!!

  • @xujiaqi6520
    @xujiaqi6520 8 років тому +8

    got midterm tomorrow(literally today), this video just give me 5 more points!

  • @audunlode9798
    @audunlode9798 11 років тому +4

    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  11 років тому +4

      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.

  • @rjc3343
    @rjc3343 6 років тому +1

    Well done! Very clear and concise, thank you.

  • @mpiana3
    @mpiana3 7 років тому +28

    We all know how to become millionaires now!

  • @darkshrine_5573
    @darkshrine_5573 2 місяці тому

    How would I use a while loop to check if the input is an integer?

  • @candicerusso7110
    @candicerusso7110 4 роки тому

    Thanks! This is the kind of example I've been looking for.

  • @leulassamenew1104
    @leulassamenew1104 10 років тому +1

    Thanks this helped me finish my homework.

  • @sitinurbaya6157
    @sitinurbaya6157 4 роки тому +1

    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  4 роки тому +1

      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

  • @amrit6755
    @amrit6755 8 років тому

    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 років тому +1

      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

  • @shakedmani6899
    @shakedmani6899 2 роки тому +1

    So useful. thanks alot buddy!

  • @jointcc2
    @jointcc2 7 років тому +1

    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 років тому +1

      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 років тому +1

      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!

  • @SalmanKhan-ep6do
    @SalmanKhan-ep6do 4 роки тому +1

    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  4 роки тому +1

      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 4 роки тому +1

      @@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. 😊

  • @rafab.3308
    @rafab.3308 4 роки тому +1

    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 роки тому +1

      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.

  • @FairFuse
    @FairFuse 7 років тому +1

    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.

  • @dhruvo100
    @dhruvo100 9 років тому

    at 8:50 could u explain a(counter)??

    • @IlyaMikhelson
      @IlyaMikhelson  9 років тому +2

      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.

  • @bootsnoot6510
    @bootsnoot6510 10 років тому +1

    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 років тому +3

      I'm glad you found it helpful! Please feel free to use it for educational purposes.

  • @leosteinman3057
    @leosteinman3057 4 роки тому

    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  4 роки тому

      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!

  • @isurusenevirathne
    @isurusenevirathne 6 років тому

    it is good for Basic While loop in Mathlab

  • @xiaojunwang3534
    @xiaojunwang3534 7 років тому +4

    explains very well

  • @marieflauto
    @marieflauto 9 років тому +1

    thanks for the helpful and clear explanation!

  • @aymanalzahrani7193
    @aymanalzahrani7193 6 років тому

    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

  • @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  10 років тому

      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!

  • @yvonnechang6373
    @yvonnechang6373 9 років тому +1

    hello, can matlab do infinite loop? like always show receive data from the input , thx

    • @IlyaMikhelson
      @IlyaMikhelson  9 років тому

      +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 9 років тому

      +Ilya Mikhelson Thanks! i'll ask you later if i still don't understand, if you don't mind

  • @hrnoh8005
    @hrnoh8005 8 років тому +1

    I have a question. Why do need there '+5000' next to '1.08*bal'? I think '1.08*bal' contains deposit.

    • @IlyaMikhelson
      @IlyaMikhelson  8 років тому +1

      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 8 років тому +1

      +Ilya Mikhelson Okay, I see. Thanks!

  • @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 років тому +2

      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

  • @benh5351
    @benh5351 8 років тому

    Just curious how to use MATLAB to find the values of two symbols?
    a+c=30 and 2

    • @IlyaMikhelson
      @IlyaMikhelson  8 років тому +1

      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 8 років тому

      It greatly helps, thank you.
      Where do I put the function which a and c are calculated from?

    • @benh5351
      @benh5351 8 років тому

      The function works but give me integers from 10-14 (the interval that is set)

  • @Antonio-ms3gb
    @Antonio-ms3gb 3 роки тому

    What's a(counter) ?

  • @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.

  • @feyisolaadejokun8013
    @feyisolaadejokun8013 9 років тому +1

    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 років тому +3

      +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 років тому +1

      +Ilya Mikhelson Thank you so so much!. You couldn't have explained it better. You are a great tutor.

  • @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 років тому +1

      +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.

  • @aldogama999
    @aldogama999 6 років тому

    best classes

  • @TheCardmaster99
    @TheCardmaster99 9 років тому

    Thanks for the help man, much appreicated :)

  • @joshuarichards6442
    @joshuarichards6442 7 років тому +1

    Thanks, helped a lot

  • @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!

  • @edwardchida2563
    @edwardchida2563 7 років тому +1

    A BIG THANK YOU

    • @IlyaMikhelson
      @IlyaMikhelson  7 років тому +1

      My pleasure!

    • @edwardchida2563
      @edwardchida2563 7 років тому +1

      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

  • @jigarpatel6931
    @jigarpatel6931 7 років тому +1

    awesome!

  • @theflash2873
    @theflash2873 4 роки тому +1

    Thanks!

  • @kunukunu8154
    @kunukunu8154 7 років тому

    excellent

  • @saidteacher3331
    @saidteacher3331 Рік тому

    Cool 😎 thanks

  • @morganfudgsiclemonster9106
    @morganfudgsiclemonster9106 9 років тому

    I love you. Thank you so much.

  • @spartan159
    @spartan159 10 років тому

    thanks for the help

  • @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%!

  • @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!

  • @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 :)

  • @islomkhodjaev9653
    @islomkhodjaev9653 5 років тому

    Can you not chew your gum while recording?

    • @IlyaMikhelson
      @IlyaMikhelson  5 років тому

      I have never chewed gum while recording...

  • @ShaunJW1
    @ShaunJW1 10 років тому +1

    I'm using Octave GNU. My fuck is it a piece of shit....

  • @amrit6755
    @amrit6755 8 років тому

    plz help

  • @au313
    @au313 5 років тому

    wtf is with your = sign?!!

  • @filipivanovic9556
    @filipivanovic9556 3 роки тому

    dado i sako polumenta

  • @ser7ser7i
    @ser7ser7i 6 років тому

    Thank you for the tutorial. Faiz haram.