Chapter Objectives: Dr. Bambang Heru Iswanto, M.Si
Chapter Objectives: Dr. Bambang Heru Iswanto, M.Si
Chapter Objectives
PROGRAM PASCASARJANA
UNIVERSITAS NEGERI JAKARTA
Objectives (cont)
Learning how to write clear and well-documented Mfiles by employing structured programming constructs to
implement logic and repetition.
Recognizing the difference between if...elseif and
switch constructs.
Recognizing the difference between for...end and
while structures.
Understanding what is meant by vectorization and why it
is beneficial.
Knowing how to animate MATLAB plots.
Understanding how anonymous functions can be
employed to pass function functions to function M-files.
M-files
While commands can be entered directly to the
command window, MATLAB also allows you to put
commands in text files called M-files. M-files are
so named because the files are stored with a .m
extension.
There are two main kinds of M-file
Script files
Function files
24/02/2014
Script Files
Function Files
Subfunctions
24/02/2014
Input
Output
n = input('promptstring')
MATLAB will display the characters in
promptstring, and whatever value is typed is
stored in n. For example, if you type pi, n will store
3.1416
n = input('promptstring', 's')
MATLAB will display the characters in
promptstring, and whatever characters are typed
will be stored as a string in n. For example, if you type
pi, n will store the letters p and i in a 2x1 char array.
Formatted Output
For formatted output, or for output generated by
combining variable values with literal text, use the
fprintf command:
fprintf('format', x, y,...)
where format is a string specifying how you want
the value of the variables x, y, and more to be
displayed - including literal text to be printed along
with the values.
The values in the variables are formatted based on
format codes.
Example:
>> fprintf('Bilangan pi = %.2f a = %.1f b = %.4f \n', pi, a, b);
disp(value)
will show the value on the screen.
If value is a string, enclose it in single quotes.
24/02/2014
ASCII Files
Structured Programming
Relational Operators
Operator
==
Relationship
Equal
unit ~= m
~=
Not equal
a < 0
<
Less than
s > t
>
Greater than
<=
r >= 0
>=
24/02/2014
Logical Operators
Order of Operations
Decisions
Selections
24/02/2014
Loops
for Loops
while Loops
Vectorization
Sometimes, it is more efficient to have MATLAB
perform calculations on an entire array rather than
processing an array element by element. This can
be done through vectorization.
for loop
i = 0;
for t = 0:0.02:50
i = i + 1;
y(i) = cos(t);
end
Vectorization
t = 0:0.02:50;
y = cos(t);
24/02/2014
Early Termination
Sometimes it will be useful to break out of a for or
while loop early - this can be done using a break
statement, generally in conjunction with an if
structure.
Example:
x = 24
while (1)
x = x - 5
if x < 0, break, end
end
will produce x values of 24, 19, 14, 9, 4, and -1,
then stop.
Example
Animation
Two ways to animate plots in MATLAB:
Using looping with simple plotting functions
This approach merely replots the graph over and over again.
Important to use the axis command so that the plots scales
are fixed.
Using special function: getframe and movie
This allows you to capture a sequence of plots (getframe)
and then play them back (movie).
Script
The following code illustrates both approaches:
x = v0 cos(0 t)
y = v0 sin(0 t) - 0.5 gt2
where
clc,clf,clear
g=9.81; theta0=45*pi/180; v0=5;
t(1)=0;x=0;y=0;
plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 3 0 0.8])
M(1)=getframe;
dt=1/128;
for j = 2:1000
t(j)=t(j-1)+dt;
x=v0*cos(theta0)*t(j);
y=v0*sin(theta0)*t(j)-0.5*g*t(j)^2;
plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 3 0 0.8])
M(j)=getframe;
if y<=0, break, end
end
pause
movie(M,1)
24/02/2014
Result
Function Functions