Documente Academic
Documente Profesional
Documente Cultură
057
Introduction to programming in MATLAB
Orhan Celiker
IAP 2019
1
Homework 1 Recap
2
Plotting
• Example
»
»
3
What does plot do?
• plot generates dots at each (x,y) pair
and then connects the dots with a line
• To make plot of a function look smoother, evaluate at more points
»
»
• x and y vectors must be same size or else you’ll get an error
»
➢ error!!
4
Exercise: Plotting
5
Outline for Lec 2
(1) Functions
(2) Flow Control
(3) Line Plots
(4) Image/Surface Plots
(5) Efficient Codes
(6) Debugging
6
User-defined Functions
• Functions look exactly like scripts, but for ONE difference
➢ Functions must have a function declaration
Help file
Function declaration
Outputs Inputs
7
User-defined Functions
9
Functions: overloading
11
Outline
(1) Functions
(2) Flow Control
(3) Line Plots
(4) Image/Surface Plots
(5) Efficient Codes
(6) Debugging
12
Relational Operators
IF ELSE ELSEIF
if cond if cond if cond1
commands commands1 commands1
end else elseif cond2
commands2 commands2
end else
Conditional statement:
commands3
evaluates to true or false
end
for n=1:100
commands
end Command block
WHILE
while cond
commands
end
17
Outline
(1) Functions
(2) Flow Control
(3) Line Plots
(4) Image/Surface Plots
(5) Efficient Codes
(6) Debugging
18
Plot Options
• Can change the line color, marker style, and line style by
adding a string argument
»
• Look at help plot for a full list of colors, markers, and line
styles
19
Playing with the Plot
to select lines
and delete or
change to see all plot
properties tools at once
to slide the plot
to zoom in/out around
20
Line and Marker Options
21
Cartesian Plots
• For example:
»
»
22
3D Line Plots
23
3D Line Plots
24
Axis Modes
»
➢ makes the current axis look like a square box
»
➢ fits axes to data
»
➢ makes x and y scales the same
»
➢ puts the origin in the lower left corner (default for plots)
»
➢ puts the origin in the upper left corner (default for
matrices/images)
25
Multiple Plots in one Figure
26
Copy/Paste Figures
• Figures can be pasted into other apps (word, ppt, etc)
• Edit→ copy options→ figure copy template
➢ Change font sizes, line properties; presets for word and ppt
• Edit→ copy figure to copy figure
• Paste into document of interest
27
Saving Figures
• Figures can be saved in many formats. The common ones
are:
.bmp uncompressed
image
.eps high-quality
scaleable format
.pdf compressed
image
28
Advanced Plotting: Exercise
29
Outline
(1) Functions
(2) Flow Control
(3) Line Plots
(4) Image/Surface Plots
(5) Efficient Codes
(6) Debugging
30
Visualizing matrices
31
Colormaps
• You can change the colormap:
»
➢ default map is parula
» parula jet
»
»
32
Surface Plots
• Example:
33
surf
interp flat
35
contour
36
Exercise: 3-D Plots
37
Exercise: 3-D Plots
generates this figure
38
Specialized Plotting Functions
39
Outline
(1) Functions
(2) Flow Control
(3) Line Plots
(4) Image/Surface Plots
(5) Efficient codes
(6) Debugging
40
find
41
Example: Avoiding Loops
• Avoid loops!
• Built-in functions will make it faster to write and execute
42
Vectorization
• Avoid loops
➢ This is referred to as vectorization
• Vectorized code is more efficient for MATLAB
• Use indexing and matrix operations to avoid loops
• For instance, to add every two consecutive terms:
43
Vectorization
• Avoid loops
➢ This is referred to as vectorization
• Vectorized code is more efficient for MATLAB
• Use indexing and matrix operations to avoid loops
• For instance, to add every two consecutive terms:
»
»
»
»
»
»
»
»
»
➢ Slow and complicated 44
Vectorization
• Avoid loops
➢ This is referred to as vectorization
• Vectorized code is more efficient for MATLAB
• Use indexing and matrix operations to avoid loops
• For instance, to add every two consecutive terms:
» »
» »
» ➢ Efficient and clean. Can
» also do this using
»
»
»
»
»
➢ Slow and complicated 45
Preallocation
46
Preallocation
47
Outline
(1) Functions
(2) Flow Control
(3) Line Plots
(4) Image/Surface Plots
(5) Efficient codes
(6) Debugging
48
Display
49
Debugging
• To use the debugger, set breakpoints
➢ Click on – next to line numbers in m-files
➢ Each red dot that appears is a breakpoint
➢ Run the program
➢ The program pauses when it reaches a breakpoint
➢ Use the command window to probe variables
➢ Use the debugging buttons to control debugger
Two breakpoints
51
Performance Measures
52
Performance Measures
53
End of Lecture 2
(1) Functions
(2) Flow Control
(3) Line Plots
(4) Image/Surface Plots
(5) Efficient codes
(6) Debugging
Vectorization makes coding
fun!
54
MIT OpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.