Looping and Conditional-Control Flow : Repetition / Iteration / Looping

In making a more complex program, MATLAB has a syntax for regulate the flow of the program. Program flow controller / control flow consists of 2 types : repetition and conditional.

Control Flow # 1: Repetition / Iteration / Looping

Repetition is the type of controller that allows you to streamline the writing script programs, especially those for programs that require the repeatedly. Repetition is often also referred to as iteration or looping.
Henceforth we only use the term iteration.

Limited iteration (for. .. end)
This iteration syntax is used to perform a repetition of the process has known quantity. For example to calculate the factorial 5, it clearly known number of iterations is 5. The way the writing is as follows:

For variable = start: interval: finish
                commands
end

For more details, here is an example of its use in the program as follows:

1. In the command window, type:
>> Edit

2. Press Enter, then came MATLAB Editor and you type the program under follows:

clear all;
clc;

Disp ('--------------------------');
Disp ( 'My 4th Training Program');
Disp ('--------------------------');

data = input ( 'limit of iterations =');
for n = 1: data
for m = data: -1:1
x (n, m) = n ^ 2 + (5 * m)
end;
end;

3. When finished typing the above programs, you save in a directory 'c: / mytraining', with the name training04.m

4. Ensure your file storage directory is contained in the list MATLAB directory search. Learn directory management here. Then type the file name without the extension training04:
>> training04

5. Press Enter, then the program will run and produce as follows:

--------------------------
My 4th Training Program
--------------------------
iteration limit = 1

x =

6 11 16
9 14 19
14 19 24

6. Done

In the next article we'll  still talk about another form of iteration but it is for unknown quantity.

Input and output dynamic program

If your program before was not interactive, not dynamic, and rigid impressed because every time you want to use the new data you should change its data in the script, you can just make it dynamic.

MATLAB provides the facility to be able to interact directly with the program without having to change the script. To ask for input from the user, MATLAB provides input function. Syntax of writing as follows:

variable = input ( 'string display');

and to display program output to the screen, MATLAB provides the function of Disp. Syntax of writing as follows:

Disp ( 'string is displayed')

Here is an example of writing to receive input syntax is continued by displaying the results of the program to the screen:

1. In the command window, type:

>> Edit

2. Hit enter, then appeared MATLAB Editor and you type in the program follows:

clear all;
CLC;

Disp ('---------------------------');
Disp ( 'My 3th Training Program');
Disp ('---------------------------');

length = input ( 'length of data =');
width = input ( 'width data =');
area = length * width;
Disp ([ 'Area ->', num2str (area)]);

3. When finished typing the above programs, you save it in a directory c: / mytraining, with the name training03.m

4. Ensure your file storage directory is contained in the list MATLAB directory search. You can learn how to do that in directory management article.  Then type the file name without the extension training03:

>> training03

5. Press Enter, then the program will run, in succession enter variable length and width and then hit enter and then hit enter and produce output as follows:

--------------------------
My 3th Training Program
--------------------------
length of data = 6
Data width = 2
Area -> 12
>>

6. Done

* Note:
At the end of the program code is num2str code. Num2str code serves to convert the data type of the original area of numerical type into string type. This is in order to be placed together with another string in the mark these brackets.

General Mathematical Function in MATLAB

Common mathematical functions are often used and provided in the literature MATLAB function is as follows:

Here is an example of using trigonometric functions:

Trigonometry Functions
Description

acos
inverse cosine

acosh
inverse hyperbolic cosine

acot
inverse cotangen

acoth
inverse hyperbolic cotangen

acsc
inverse cosecan

acsch
inverse hyperbolic cosecan

asec

inverse secan

asech
inverse hyperbolic bag

salty
inverse sine

asinh
inverse hyperbolic sine

atan
inverse tangent

atanh
inverse hyperbolic tangent

cos
cosine

cosh
hyperbolic cosine

cot
cotangen

coth
hyperbolic cotangen

csc

cosecan

csch

hyperbolic cosecan

sec
secan

sech
hyperbolic secan

sin
sine

sinh
hyperbolic sine

tan
tangent

tanh
hyperbolic tangent





1. In the command window, type:
>> Edit

2.Press enter, then appeared MATLAB Editor and you type in the following programs :

clear all '
CLC;

x = [0:10:180];% generate the data point

y1 = sin (x * pi/180);% calculation data sine x
y2 = cos (x * pi/180);% calculating the cosine of data x

out = [x 'y1' y2 ']

3. After finish typing or copying  program above, save it in the directory c: / mytraining, gide the name 'training02.m'

4. Ensure your file storage directory is contained in the list MATLAB directory search. You can learn how to do that in directory management article. Then type the name of the file training02 twithout extension:
>> training02

5. Press Enter, then the program will run and produce output in the window of the data x, y1, and y2

Besides MATLAB also provides other mathematical functions such as

Exponential Function 
Description
exp
Exponential
log
natural logarithm
log10
logarithm base10
log2
logarithm base 2
sqrt

root

If see table functions above , you may be confused, how to use that functions? The easiest method to find out how to use the function is as follows:

1. Suppose you want to know how to use MATLAB functions to log10, type the following at the command window:

>> help log10

2.Press enter, then you will get the information you want as follows:

Common log10 (base 10) logarithm.
Log10 (X) is the base 10 logarithm of the elements of X.
Complex results are produced if X is not positive.

See also LOG, log 2, EXP, LOGM.

3. So how to use it is as follows:
>> X = 100;
>> Y = log10 (x)
b =
       2

Complex Number Operation in Matlab programming

Another advantage of the Matlab programming is the ability to process the data complex numbers without requiring special variable declarations. Here is to declare a variable to the complex numbers:

>> a=2+1.5i
a =
   2.0000 + 1.5000i
>> b=3-4j
b =
   3.0000 -4.0000i

In conclusion, there is no difference using the identifier 'i' or 'j' for the number complex. For the purposes of mathematical calculation it does not take special functions, for example, as follows:

>> a + b
ans =
    5.0000 -2.5000i

As for the separation needs of real and imaginary values can easily done, for example by the following examples:

>>a=2+1.5000i
a= 2.000 + 1.5000i
>> real(a)
ans =
      2
>>imag(a)
ans =
     1.5000

Complex form a + bi in the complex integer arithmetic rectangular shape, whereas the polar form of complex numbers is realized with Magnitude and angle. Conversion from rectangular to polar form in Matlab met through the functions abs and angle.

>> a= 2+1.5i
a =
   2.0000 + 1.5000i
>>abs(a)
ans =
    2.5000
>> angle(a)
ans =
    0.6435

MATLAB Mathematic operation

Mathematical operation in matlab programming is very simple, it similar if you use the regular calculator. Here is mathematic table operator that use in Matlab programming.


Addition
+
A + B
Subtraction
-
A - B
Multiplication
*
A * B
Division
/ or \
A / B or A \ B
exponent
^
A ^ B

Knowledge of the matrix is essential in Matlab programming because all patterns of mathematical operations will be restored in the pattern matrix math operations. For example is when we declare variable 'a' and fill it with the value 5 in the following way :
>> a=5
a =
    5

otomatically, matlab will recognize variable 'a' above as a matrix which has 1x1 matric dimension. It can be proof as follow :
>> a(1,1)
ans =
      5

The difference will be more felt when we do an operation that entangle multiplication and division. For example we can use the case of area calculation whith length and width data is provided. As a first case, it is provided length data (5) and width (6). the solution for this case as follow :
>> length = 5;
>> width = 6;
>> area = length*width;
area = 30

whereas  for second case, it is provided four datas in each variable, length (4,5,3,1) and width (5,3,2,2), if we use same method, it will result error message :
>> length = [4 5 3 1]
length =
        4 5 3 1
>> width = [5 3 2 2]
width =
        5 3 2 2
>> area = length*width
??? Error using ==> *
Inner matrix dimensions must agree.

An error message above is shown because it is not meet the matrix multiplication requirement. You should remember that first column of the first matrix should be same with total row of second matrix.

How to Access Variable in MATLAB

How to access variable in Matlab?By default, MATLAB identify variable which you use as a matrix or array. So for variable which contain more than one element, addressing each element variable element in MATLAB following the following notation:
variable(line, column)

For example you can define in command window a matrix which has 3x3 dimension:
>> a = [1 3 5; 5 6 7; 8 2 4]
a =
     1 3 5
     5 6 7
     8 2 4

To access single element of matrix above as follow :
>> a(3,2)
ans =
         2

It means that you are accessing matrix element of 'a' variable in third line and second column.

To access element in the certain line as follow:
>> a(3,:)
ans =
         8 2 4
It means that you are accessing matrix elements of 'a' variable in third line. Sign double dot (:) at the matrix column means all column.

To access element in the certain column as follow:
>> a(:,3)
ans =
         5
         7
         4

It means that you are accessing matrix element of 'a' variable in the third column. Sign double dot (:) in the line means all line

To access some elements in certain line and column as follow:
>> a(1:2,2:3)
ans =
        3 5
        6 7

It means that you are accessing matrix element of 'a' variable in first to second line and in second to third column. Writing (1:2) at the line means first to second line. Writing (2:3) at the column means second to third column.