Welcome to
Getting Started
with MATLAB
Programming
DAY #04
WORKSHOP RULES!!
All Participants must:
▸ Have a version of MATLAB installed on their PCs.
▸ Practice all tasks alongside – try to keep up the
pace!
▸ Clear any doubts of previous session before the
next one. Note down the important commands!
▸ Complete Daily Assignments and submit them
together at the end 5 days.
▸ Obtain atleast 70% grade to be eligible for
60
Certificate.
Day#4 Overview
1. Switch Conditional Statement
2. Loops
4
3. FOR Loop
4. WHILE Loop
5. Plotting in MATLAB
6. More Plotting – 2D
7. More Plotting – 3D
8. More Plotting
61
Switch Conditional Statement
▸ A switch block conditionally executes one set of
statements from several choices. Each choice is
covered by a case statement.
4.1 ▸ The case expression is a scalar, a string or a cell
array of scalars or strings.
▸ The switch block tests each case until one of the
cases is true. When a case is true, MATLAB
executes the corresponding statements and
then exits the switch block.
▸ We also specify an otherwise block that
62 executes when no case is true
Switch Conditional Statement
▸ General Syntax:
4.1
63
Practice – 1
▸ You are required to monitor the voltage of a
particular power plant and ensure that it is
operating within safe limits defined as:
100 < 𝑉 < 125
▸ Write a function that uses the switch statement
to inform the user if current voltage levels are
within safe limits or not.
64
Loops
▸ MATLAB allows us to use 2 different types of
loop functions:
▹ For Loop – counter-controlled
4.2 ▹ While Loop – condition-controlled
▸ Loops can also be nested in one another.
65
FOR Loops
▸ For Loop syntax:
4.3
▸ Values can be defined using following ways:
▹ Initial_value : final_value (eg: 1:10)
▹ Initial : step : final (eg: 1: 2: 10)
▹ Vector arrays (eg: [2 3 5 7])
66
While Loops
▸ While Loop syntax:
4.4
67
Practice – 2
▸ Create a vector of n elements. Calculate the
statistical mean and standard deviation using
both For loop and while loop.
▸ Important formulas:
68
Plotting in MATLAB
▸ Basics of Plotting in MATLAB:
▹ Plotting a dataset
4.5 ▹ Adding axis labels
▹ Adding titles
▹ Axis Limits
▹ Plotting multiple functions in same figure
▹ Line Styles, colours and markers
▹ Adding Legends
69
▹ Displaying grid
Plotting in MATLAB
4.5
70
Plotting in MATLAB
▸ We can also plot using logarithmic axes using
following functions:
▹ Semilogx
4.5 ▹ Semilogy
▹ Loglog
71
Practice – 3
▸ Plot sin and cos functions and try to replicate
the following design features:
72
More Plotting – 2D
▸ Some other forms of Plotting Features availabe
in MATLAB:
▹ Sub Plots
4.6 ▹ Histogram
▹ 2D Scatter Plot
▹ Polar Plots
▹ Semilogx, Semilogy
▹ Loglog
73
More Plotting – 3D
▸ Some other forms of Plotting Features available
in MATLAB:
▹ Contour Plots
4.7 ▹ Mesh
▹ Surface
74
More Plotting
▸ You can explore more kinds of plotting and
visualization through the following link:
▸ https://www.mathworks.com/help/matlab/2-and-3d-
4.8 plots.html?s_tid=CRUX_lftnav
▸ You can also try the help function. Try:
▹ help graph2d
▹ help graph3d
75
Practice – 4
▸ We need to create the following subplots in a
single figure:
▸ Plot of y = x2
▹ Plot of semilog(x)
▹ Plot of semilog(y)
▹ Plot of log-log
76
Practice – 5
▸ Create a 3D Plot for:
▸ With x and y on horizontal plane and t on vertical
plane.
77
Practice – 6
▸ Create a 3D Mesh, Surface and Contour Plot for:
▸ Over the interval:
78
Any
Questions?
“The moment you stop
asking questions is the
moment you stop
learning.”
Remember to complete
79 your Assignment!