0% found this document useful (0 votes)
9 views27 pages

Recap: - Introduction To MATLAB

Uploaded by

Arifah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views27 pages

Recap: - Introduction To MATLAB

Uploaded by

Arifah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Recap

• 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

– What if you want to enter different


Lecture 10 values for radius or length? 3
3.4.1 MATLAB Scripts
• Matlab scripts are the solution to this problem!
– You can create a “script” that can be repeatedly executed
– This is the basic Matlab “program”

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

length 1x1 8 double array


>> density=100;
radius 1x1 8 double array
>> myscript
volume 1x1 8 double array
Enter the length of a cylinder: 10
Now enter the radius: 1
Grand total is 3 elements using 24 bytes
Weight of cylinder is: 3141.6
>>
>>

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)

• Commands are executed in the order


they are typed, one at a time, from
first to last.

• Many situations in which programs


require much more complex logic.
Lecture 10 10
3.5.1 Relational and Logical Operators
Relational Operators make those Logical Operators all use to combine the
comparisons comparisons
< Less than & and
<= Less than or equal to ~ not
> Greater than | or
>= Greater than or equal to xor exclusive or
== Equal to
~= Not equal to
When relational and logical operators are present:
 All arithmetic operations are performed first (in their particular order)
 The relational and logical operators are evaluated after.
Lecture 10 11
Relational Operator: Examples 2
Most computer programs use the number 1 for true and 0 for false
>> x=5; >> x=1:5 >> x=1:5
>> y=1 x= x=
y= 1 1 2 3 4 5 1 2 3 4 5
>> x<y >> y=x-4 >> y=[4,8,10,7,9]
y= y=
ans = 0
-3 -2 -1 0 1 4 8 10 7 9
>> x>y
>> x<y >> x<y
ans = 1 ans = ans =
0 0 0 0 0 1 1 1 1 1

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:

• Can you determine what the variable x will be after running


this M-file?
Lecture 10 16
for loop example 4

• The first time through the loop, j = 1


• Because of the single value in parentheses, x will be a one-
dimensional array
• x(1) will be set equal to 5*1 = 5
• The second time through the loop, j = 2
• x(2) will be set equal to 5*2 = 10
• This will be repeated until j = 10 and x(10) = 50
Lecture 10 17
for loop example 4

• x will be a one-dimensional array (a row matrix) with 10


elements:

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

statements statements (1) statements (2)

if A>10
if A>10
% computations;
% computations; else
end % computations;
end

• Can include multiple statements


• Statements can also include other if statements
(can nest if statements inside if statements)
• Be careful not to overlap (crossover) if statements!
Lecture 10 24
if-elseif statement
if false elseif false elseif false
condition condition condition else

true true

statements (1) statements (2) statements (n) statements (n+1)

if A>10
% computations;
elseif A<10
% computations;
else
% computations
end

• Can have several elseif conditions…


• else is optional and executes if all other tests fail
Lecture 10 25
Rules about if–end Structures
• Every if command must have an end.
• A program can have many if … end statements following each other.
• A program can perform the same task using different combinations of if-end, if-else-
end, and if-elseif-else-end statements.
• Multiple elseif conditions are allowed within if-elseif-else-end statements.
• The else condition is not required.
• When else is used, a conditional statement is NOT added.

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

You might also like