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.
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
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
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.
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.
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
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
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.
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.?
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.
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!
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!
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?
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!
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??
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!
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
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!
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.)
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.
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!
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.
+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!
+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!
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
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.
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 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.
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.
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
+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.
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.