I am going to show you the different types of programming structures that are found in
matlab and go through a couple of examples in this screencast. First of all I wanted
to introduce the operators and precedence before we get into programming. We have the
order of operations. We start with the caret which represents exponentiation. Then we have
the negation or the unary symbol. Next we have multiplication and division and finally
plus and minus. Everything is read left to right. If you want to over ride something
similar to other programming languages you use a parenthesis. Relational operators if
you want to see if something is equal too something else you actually use two equal signs. Not
equal to is a tilde equal. Then you have less than, greater than, less than or equal too
and greater than and equal too. Logical operators are the ampersand sign is for "and", we have
a vertical bar (which is above the backslash on most keyboards) that is "or" and then we
have the tilde which represents "not". The most basic selection structure is the "one-way
IF". We have a flow chart where we come into some logical condition. If it is true we do
something otherwise if it is false we do not do anything and just move on. In matlab this
looks like just an "if" with logical statements and then "end". A simple example of this would
be this function I have set up here which is "sgnsqr". First we calculate "y" to be
equal to "x" squared. If "x" is less than 0 then we set "y" equal to negative "y". We
carry over the sign of the original "x" into the square of that value. The "sgnsqr" of
-3 would be -9. That is where the "if" comes in. The second type of conditional is the
"If-Then-Else". This is different from the one-way because you do something if it is
false. If the logical condition is true then you do something. If it is false then you
do something else. This looks like the "if" some logical expression "else" statements
"end". An example of this is the friction factor depending upon the Reynold's number.
I have a function called "frictionfactor" which is a function of "r". If "r" is less
than 2000 then the friction factor is 16/r otherwise the friction factor is a different
equation. When we type these in for the two different ranges for the Reynold's number
we see that we get different answers depending upon the route that we take. Next we have
the "Multi-alternative If" which is "If-Then-ElseIf". We can have one condition that is true we
do something. If that original condition is false then we have another conditional. If
that is true we do something otherwise we go down skimming off the false answers here.
It looks like "If" condition 1 then you do code block 1 "elseif" condition 2 code block
2 and so on. The end here you have just an else. If nothing else is satisfied then you
do something and that is optional. A good example of the multi-alternative "if-then-else"
is thermocouples. For example we might have 3 different correlations depending upon the
voltage from a thermocouple. The top one here if we have a low voltage, next if we have
a different range for the voltage and then we have another correlation for a higher voltage
value. If it less than -8 we display and error and if it is greater than 96.55 display another
error. This looks like the following. We have the function "thermocouple" as a function
of voltage. I have if voltage is less than -8 we say "out of range, voltage too low".
Then we have "elseif" voltage is less than 0 we have the first correlation, less then
42 we have the second correlation, less than 96.55 we have another correlation and then
we have an "else" which means that if none of those are true we display :out of range,
voltage too high". Two examples here are I have an in range voltage here of 5.2 and it
displays a temperature of around 95 degrees or if it is out of range it display the correct
message. Next we have the select case. We come into some sort of decision and it is
not really where we are looking at ranges. We are looking at discrete cases so we either
have a menu for example of "a", "b" , "c" or integers 1, 2 ,3. This looks like "switch"
and then you have an expression case and then at the end if you do not have anything else
going on you just write otherwise. I want to show you an example of this. I have "pressure
= input('Enter pressure in psi: '). The user is going to input a pressure and then it asks
if you would like to convert to kPa, bar or atm. You also notice importantly I have an
"s" here at the end of my input. You have to do that if it is going to be a string.
Depending upon what they enter as their units that they want to convert to we are going
to have a different equation for the output. We have something at the end which just sets
"x" equal to the output units. Then I display "fprintf" converted. I have two place holders
here. One so that "x" is actually a vector of our output and the units. I have 6 digits
here with 2 to the right of the decimal and then I have my string here which is the percent
"s" is going to display the string in terms of units. When I run menu it is going to ask
me for a pressure in psi. I will do kPa. I press "A" and then it says that the converted
pressure is 101 kPa. Next we have the "Do-while" or the "pre-test loop". You just have "while"
condition loop code and then "end". An example of the "Do-while" is shown here where I set
"x" equal to 20 and "k" equal to 0. I say that while "x" is greater than 1.1 I am going
to take the square root of it and then take this counter so I am going to iterate through
to tell me how many iterations this goes through. When I run "do-while" and we see that is comes
out at about 1.0981 and it has gone through 5 iterations of that loop. We have the "Do-until"
or the "post-test loop" where we are doing the test after the loop code. There is no
real way to do this in matlab but you can fool matlab into doing this by just doing
a "while". One and zero are logical constants for true and false in matlab. While(1) is
always true. This loop is going to keep going unless you break out of the loop. An example
of this is shown here. We have come into this loop with "x" equal to 5, "n" equal to 0 and
"fx" equal to 0 following by while(1) If "fx" is less than 2 we are going to break and end.
I am going to show you how we can debug this and watch what is going on. Over here in the
workspace I am going to un-dock by clicking on this arrow. I am then going to take the
editor and pull it over so that I can simultaneously see the editor and the workspace. Next I am
going to click over here on the left and put a break point and click play. As I press "F10"
or step here we can look over here in the window and see that is going on. We see that
"x" keeps going down and our function value keeps going down because "x" is going down.
At some point our function becomes less than 2. Now it is negative 3 and then we break
out of the loop and we will display "x". Here we display "x" as negative 2. Lastly we have
the "count controlled iteration" or the "For Loop". We start with some sort of index. When
the index exceeds an end value then we exit. It actually exits this "for" loop with an
index value of 1 greater than the end but then matlab actually resets and moves down
to index equal to end. When the index is not greater than the "end" then we have a loop
code. We increment the index and then we keep going. In matlabe the syntax looks like 4
index equals start ":" increment which is 1 if it is left out. Colon "end:. Then we
have our loop code and keep going. An example of the "For loop". I have set up a vector
here with "x", "n" is the length of "x" and then I am declaring a temporary value here.
"Temp" is going to be the first component of our "x" vector. I am going to do this "For-Loop"
and I am iterating from "k" equals 1 to "n" minus 1. I am saying the vector component
of "x" is going to be the next component of "x". It is actually going to reassign "x".
Then at the end we are saying the last one is going to be "temp" which we stored as the
first one. All this is doing is iterating through every item of that vector and moving
it up one spot in our vector. We can have a more complicated structure involving two
"For-Loops". Here we have nested "For-Loops" Basically summing this erray we start with
the size of our array. We set the number of rows equal to the size of 1 because the size
will give us a vector of rows and columns. We declare "sum" is equal to 0. You have to
do this to somewhat "dim" variables. Then we are going to go through for "i" equals
1 to "nrows" and for "j" equals 1 to"ncols". We are taking the sum is going to be the old
sum plus the current array value "A(i,j)". Then at the end we display our sum. I have
an "A" array here and I do "sumarray" it is going to go through those and spit out the
sum of our array.