Recap: - Introduction To MATLAB
Recap: - Introduction To MATLAB
• Introduction to MATLAB
Lecture 10 1
Road Map for Today
• The Programming Construct
– Relational and Logical Operators
– for loop
– Conditional statements
– while loops
Lecture 10 2
3.4. MATLAB Scripts: M-files
• Matlab’s Command prompt is where you can enter commands to
be executed immediately…
– Just like a calculator
– You can see what you’ve done but it must be re-entered at the
command prompt to be recalculated
– Only the results (variables) are retained in the Matlab
workspace (diary on will log commands and results to a
diary file; diary off disables this)
>> length=5.5;
>> radius=2.25;
>> volume=pi.*radius^2.*length
volume =
87.4737
Lecture 10 4
MATLAB Scripts (cont…)
• Scripts are simply text files containing Matlab statements
– You can use any text editor but the built-in editor indents and uses
color to highlight the language syntax
– Script files always have the “.m” extension, e.g., m-files
Lecture 10 5
MATLAB Scripts (cont…)
• When a script (m-file) is executed, it is simply read sequentially and
each line is presented to the Matlab command prompt just like it was
typed by hand
– Speed and repeatability are key features
– Matlab control & loop statements (e.g., if, for, while…) can be
executed in this way
Lecture 10 6
Example Script in an m-file
• Use File/New/M-file to start Matlab editor
• Save file with .m extension in directory in Matlab’s path
• Type m-file name at prompt to execute
>> myscript
Enter the length of a cylinder: 8
Now enter the radius: 2.4
Volume of cylinder is: 144.76
>> myscript
Enter the length of a cylinder: 10
Now enter the radius: 1
Volume of cylinder is: 31.416
>>
Lecture 10 7
3.4.2 Script Workspace
• When commands are executed, the results
are left in the Matlab BASE workspace.
– whos will list all the current variables in the
workspace
– Scripts can make use of variables already defined
>> myscript
Enter the length of a cylinder: 10
in
Now enter the the Base
radius: 1
Volume of cylinder is: 31.416
workspace
>> whos
Name Size Bytes Class
Lecture 10 8
5.3 More Scripts…
• Try out commands indivudually…
• Use scripts to collect together commands that can be used to
solve more complicated problems.
• Scripts can call other scripts
– Can “chain” together individual small programs
– Each script can be tested and debugged separately
Lecture 10 9
3.5 The Programming Construct
% • We can use Programming Constructs :
% This program finds the sum of n^2 for
% integers from 1 to 10
– “conditional statements,
% – "loops" and
x = 1 : 10 ; – "program flow control".
y = x.^2;
Result = sum(y)
Sequence Selection Repetition (Loop)
Lecture 10 12
Logical Operator: Examples 3
>> x=[1,2,3,4,5]; >> z>x
>> y=[-2,0,2,4,6]; ans =
>> z=[8,8,8,8,8]; 1 1 1 1 1
>> z>x & z>y
ans = >> z>y
1 1 1 1 1 ans =
>> x>y | x>z 1 1 1 1 1
ans =
1 1 1 0 0 >> x>y
ans =
1 1 1 0 0
>> x>z
ans =
0 0 0 0 0
Lecture 10 13
3.5.2 for loops
• Also called a do loop in other m= 0
languages
• Used when you want the m= m+1
calculations to be performed a
defined number of times (Calculations)
• In this example, the calculations
are performed 10 times No
m = 10?
Yes
Lecture 10 14
for loop
• a for loop begins with the statement indicating how many
times the statements in the loop will be executed
• A counter is defined within this statement
• Examples:
for k = 1:100
(counter = k, the loop will be executed 100 times)
for i = 1:2:7
(counter = i, the counter will be incremented by a value of 2 each time until its
value reaches 7. Therefore, the loop will be executed 4 times (i = 1,3,5, and 7)
Lecture 10 15
for loop (cont…)
• The loop ends with an end statement
• In M-files, the Octave editor will automatically indent text
between the for and end statements:
Lecture 10 18
for loop example 4
• Remember that if you leave off the semi-colon, the results of the
calculations will be written to the screen in every loop:
Lecture 10 19
for loop (example 5)
for j = 1:3
for k = 1:3 Example of Nested Loops to
create a two dimensional array
T(j,k) = j*k;
end
end
Lecture 10 20
More examples
Example 6 Example 7
y = 0; y = 0;
for k = 1:5 for k = 2:2:8
y = y + k; y = y + k;
end end
y y
Lecture 10 21
More examples
Example 8 Example 9
for k = 1:5 for i = 1:21
y(k)=k^2; x(i) = -10 +(i-1);
end y(i) =
y 2^(0.4*x(i)) + 5;
end
Lecture 10 22
3.5.3 Conditional Statements
• Conditional statements let Octave
make decisions
• In the program a condition is
specified:
― If the condition is met, one set of
actions is taken.
― If the condition is not met, a
different set of actions is taken.
Lecture 10 23
The if … end control structure
if false if
condition condition
true true false
if A>10
if A>10
% computations;
% computations; else
end % computations;
end
if A>10
% computations;
elseif A<10
% computations;
else
% computations
end
Lecture 10 26
3.5.4 while loops
• Will do computational loop
initialize k ONLY if while condition is
done
k=0; met
while while k<10
k<10 % computations;
k=k+1;
• Be careful to initialize while
end variable
computations
• Can loop forever if while
change k
variable is not updated
within loop!!!
Lecture 10 27