aseboflower.blogg.se

While loop matlab
While loop matlab






while loop matlab

Matrix assignments follow the form x(i,j) = 4, where “i” and “j” are any two numbers greater than zero. Type your equation inside the second loop, which will be the values given to z. Hitting tab before the second loop can help keep track of which loop is active at which point.

while loop matlab

The first loop should control your x values while the second loop should control your y values.

  • Matrix Method: Set up two nested loops, meaning one loop within the other.
  • Keep in mind, n is the number you will use to determine the number of data points. See the loops section of tips for help with loops. You can also reference parts of the vector that already have a value, like x(3) = x(2) + x(1).

    while loop matlab

    Vector assignments in MATLAB follow the form x(i) = 2, where “i” is any number greater than, but not including, zero. Vector Method: Set up a for loop to place values into a vector.It will look something like "> y = sin(x) " Type your function on the command line in the command window. x = 1:10 will assign the numbers 1,2,3.10 to x). The middle part is optional and will default to intervals of 1 if it is left out (i.e. For example, ">x = 0 : (pi/100) : (2*pi) " will set x to a list of values from 0 to 2*Pi with intervals of Pi/100. Simple Method: Decide on the range of x you want to use for your independent variables and by how much you want it to step each time.If you want to generate a list of points with 3 variables instead of 2, then the matrix method will be most useful.

    while loop matlab

    If you have a set of data that is plotted against an increasing x value, such as (1, y1), (2, y2).(n, yn) but want to use a variable number of points, then use the vector method. If you want to plot a simple function like y = sin(x), use the simple method. This step can be achieved in one of a few ways depending on what type of data you want to graph.

  • For more information on any part of this tutorial, or MATLAB in general, visit the MATLAB Documentation.
  • This would do stuff 5 times for j, then increment i, do stuff 5 times for j, increment i, and so on. This looks something like "for i = 1:5 / for j = 1:5 / do stuff / end / end".
  • Nested Loops: Nested loops are when a loop is inside of another loop.
  • To make a while loop look like the for loop above, you'd type "while i<=n / do stuff / end". Usually the “do stuff” portion has some part that will eventually make the statement false. This loop means “do stuff” while the statement is true.
  • While Loops: While loops in MATLAB take the form of: "while statement is true / do stuff / end".
  • So if this printed “Hello” every time it went through the loop, and n was 5, then it would print “Hello” five times.
  • For Loops: For loops in MATLAB take the form of: "for i = 1:n / do stuff / end" (the forward slash indicates a line break).
  • You can tell if you have made an infinite loop by the fact that your computer will simply sit there, not outputting anything except what is inside the loop. Both can typically be used interchangeably, but it is easier to create an infinite while loop than an infinite for loop. There are two types of loops common in MATLAB, the for loop and the while loop.
  • Loops: Loops are used when you want to perform an action multiple times.
  • So if you wanted to get the value 3 from the z example, you would simply type z. You can then reference the number stored at any position in the vector by typing z, where “i” is the Fifth position in the vector. If you then wanted to add on to z, you could simply state z = 3. To set a variable to a single number, simply type something like z =1. In the case of MATLAB, array size does not need to be specified when the variable is created. This means that variables are stored as lists of numbers, with the most basic list containing only one number.
  • Variable Types: The only type of variable in MATLAB is an array.
  • You can also type “clear” followed by a variable name to clear only the data for that specific variable. Typing “clear” in the command window after the > prompt will clear all current variables, which can help if you're seeing unusual output.
  • Clear Command: There are a few useful command window commands.
  • Also, any time that an output is desired, like a graph, the semicolon should be omitted. This is trivial when the output is a short assignment, like y = 1, but becomes problematic if a large matrix is created.
  • Semi-colon Operator: If a command is followed by a ' ' then the output will not be printed on the screen.







  • While loop matlab