Program notes - Programming Concepts 1 Introduction...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming Concepts 1 Introduction Computer programs use “variables” to hold information. Variables can be of several different types: integers, character strings, real numbers, etc., to name a few. We will mostly deal with scalars, vectors and matrices of integer and real types. It is important to remember that vectors are a special case of matrices. 2 Vectors Are Matrices Too Suppose we type the following at the MATLAB prompt: > a=[10 20 30 40 50]; The meaning of the above command is obvious: a is a vector with five entries, i.e., its size is five. Upon entering the command > size(a) we find the output is ans = 1 5 This tells us that MATLAB stores the vector as a matrix of size 1 × 5. Addressing individual elements We can refer to the second element of a as > a(1,2) or > a(2) Question: When can we use just one index (instead of two) to refer to elements of a matrix? Of course, an attempt to address a non-existent element leads to an error. For instance, > a(10)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Index exceeds matrix dimensions. By the way, here’s an easier way to generate the vector a : > a = 10:10:50 This command tells MATLAB to set the first element to 10, and then keep adding elements to the vector in increments of 10, until it hits 50. Question: Can you guess the result of the command b=1:2:10 3 Looping Mechanisms There are two very important looping mechanisms in MATLAB: the for loop and the while loop. Looping mechanisms are used to perform repetitive steps. 3.1 The for loop The for loop is typically used when the number of repetitions is known. For instance, when adding two vectors of size five each, we need to repeat the addition operation five times. Here the number of repetitive steps is known. In this case, we prefer to use the for loop . The form of the for loop is for k=begval:incr:endval <repetitive step(s) here> end Here, k is the counter , begval is the beginning value for k , incr is the increment, and endval is the upper limit on k . This loop tells MATLAB to initialize the counter k to begval , and to execute the steps inside the loop as long as k is less than or equal to endval . The counter k is automatically incremented each step by the amount incr . Going back to the vector addition example, we would write MATLAB C/C++ for k=1:1:5 c(k) = a(k) + b(k) end for ( k=0; k<5; k++ ) { c[k] = a[k] + b[k]; } For the sake of comparison, the C/C++ version is shown on the right. The operation c(k)=a(k)+b(k) is performed for k=1, 2, 3, 4, 5 . 2
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/13/2010 for the course MECH 342 taught by Professor Vemaganti during the Winter '05 term at University of Cincinnati.

Page1 / 9

Program notes - Programming Concepts 1 Introduction...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online