This document covers various topics related to MATLAB including loops, control flow, plotting, and 3D graphics. It discusses for loops, while loops, and switch statements for control flow. It provides examples of plotting functions like plot, hold, title, ylabel, legend, and properties that can be applied. It also demonstrates how to generate 3D surface and mesh plots using functions like surf, plot3, and mesh along with an example mesh plot of a sinc function.
2. About this Session
• We will coverthe following
topics
•Loops and Execution Control
•MATLAB files: Scripts and Functions
•Program Output and Plotting
3. Loops & control execution
For loop :Repeat statements a specific number of
times.
for i=1:10
<statement 1>;
⋮
<statement n>;
end
4. Loops & control execution
• While Loop (commands below will execute if the
condition is true)
while i<10
<statement 1>;
⋮
<statement n>; i=i+1;
end
5. Switch statement
switch Switch among several cases based on expression.
The general form of the switch statement is:
switch switch_expr
CASE case_expr,
statement, ..., statement
CASE {case_expr1, case_expr2, case_expr3,...}
statement, ..., statement
...
OTHERWISE,
statement, ..., statement
END
6. Practical Task 01
When dhoni hits a ball at
an initial velocity of
20m/s. Find the location
of ball in every 0.1
seconds.
Find the position for first 10 seconds
7. v0=20;
g=9.81;
y=0;
t=0;
time=[];
location=[];
for t = 0:0.1:10
disp(['at time ',num2str(t),'location of the ball at the v
distance',num2str(y)]);
y=(v0*t)-0.5*g*t^2;
time=[t;time];
location=[location;y];
end
plot(time,location);
8. Practical Task 02
When dhoni hits a ball at
an initial velocity of
20m/s. Find the location
of ball in every 0.1
seconds.
Find the position while the position not equal to zero
(upto reaches the ground)
10. Ouputs
disp Display array.
disp(X) displays array X without printing the array name
or
additional description information such as the size and
class name.
In all other ways it's the same as leaving the semicolon
off an
expression except that nothing is shown for empty
arrays.
If X is a string or character array, the text is displayed.
11. num2str Convert numbers to character
representation
T = num2str(X) converts the matrix X into its
character representation T
with about 4 digits and an exponent if
required. This is useful for
labeling plots with the TITLE, XLABEL,
YLABEL, and TEXT commands.
12. Plotting
plot Linear plot.
plot(X,Y) plots vector Y versus vector X. If X
or Y is a matrix, then the vector is plotted
versus the rows or columns of the matrix,
whichever line up. If X is a scalar and Y is a
vector, disconnected line objects are created
and plotted as discrete points vertically at X.
13. Various line types, plot symbols and colors
may be obtained with plot(X,Y,S) where S is
a character string made from one element
from any or all the following 3 columns:
14. b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
15. For example, plot(X,Y,'c+:') plots a cyan dotted
line with a plus at each data point; plot(X,Y,'bd')
plots blue diamond at each data point but does
not draw any line.
plot(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines
the plots defined by the (X,Y,S) triples, where the
X's and Y's are vectors or matrices and the S's
are strings.
16. Task 01
Try creating a plot with sample on the x-axis
and mass1on the y-axis.
17. Task 02
Try plotting mass2 (y-axis) against sample (x-
axis). Use red (r) star (*) markers and no line
in your plot.
plot(x,y,'r--o')
The command above plots a red (r) dashed (--)
line with a circle (o) as a marker. You can learn
more about the symbols available in the
documentation for Line Specification.
18. Task 03
Info: Notice that the first plot you created no
longer exists. To plot one line on top of
another, use the holdon command to hold the
previous plot while you add another line. You
can also use the hold offcommand to return to
the default behavior.
Issue the hold on command.
19. Task 04
Now, plot mass1 (y-axis) against sample (x-
axis) with black (k) square (s) markers and no
line. (Line specification options)
20. Task 05
Try closing all open figure windows by issuing
the closeall command.
21. Task 06
Info: The plot function accepts optional
additional inputs consisting of a property
name and an associated value.
>> plot(y,'LineWidth',5)
The command above plots a heavy line. You
can learn more about available properties in
the documentation for Lineseries Properties.
Now try plotting v1 with a line width of 3.
22. Task 07
Info: The plot function accepts a property
name and property value pair after the plotted
arguments and line specifier.
plot(x,y,'ro-','LineWidth',5)
Try plotting v1 (y-axis) against sample (x-axis)
with red (r) circle (o) markers with a line width
of 4.
23. Task 08
Info: Labels can be added to plots using plot
annotation functions, such as title. The input
to these functions is a string. Strings in
MATLAB are enclosed in single quotes (').
>> title('Plot Title')
Try adding the title 'Sample Densities' to the
existing plot.
24. Task 09
Use the ylabel function to add the
label 'Density (g/cm^3)'.
25. Task 10
Create legend
legend(string1,string2,string3, ...) puts a
legend on the current plot
27. 3D graphics using the
functions surf, plot3 or mesh.
[X,Y] = meshgrid(-10:0.25:10,-10:0.25:10);
f = sinc(sqrt((X/pi).^2+(Y/pi).^2)); mesh(X,Y,f);
axis([-10 10 -10 10 -0.3 1])
xlabel('{bfx}')
ylabel('{bfy}')
zlabel('{bfsinc} ({bfR})')
hidden off