Programming with MATLAB
Loren Shure
© 2016 The MathWorks, Inc.
1
Today’s Objectives
Introduce you to programming with MATLAB
– The MATLAB language
– Development tools
Show you how to program effectively in MATLAB
Demonstrate the range of programming styles supported
– Interactive command line
– Scripts and functions
– Object-oriented programming
2
Demo: Assessment of Wind Turbine Locations
Goal: Create analysis to choose best wind turbine
location by estimating power generation at
multiple locations
Approach:
– Interactively explore data and develop analysis approach
Load and preprocess data from observation towers
Fit wind speed probability distribution
Estimate average power
– Automate to run on data from other locations
3
Calculating Average Turbine Power
𝑣𝑚𝑎𝑥
𝐴𝑣𝑒𝑟𝑎𝑔𝑒 𝑃𝑜𝑤𝑒𝑟 = 𝑓 𝑣 ∗ 𝑊 𝑣 𝑑𝑣
𝑣𝑚𝑖𝑛
𝑓 𝑣 𝑖𝑠 𝑡ℎ𝑒 𝑤𝑖𝑛𝑑 𝑠𝑝𝑒𝑒𝑑 𝑝𝑟𝑜𝑏𝑎𝑏𝑙𝑖𝑡𝑦 𝑊 𝑣 𝑖𝑠 𝑡ℎ𝑒 𝑡𝑢𝑟𝑏𝑖𝑛𝑒 𝑝𝑜𝑤𝑒𝑟 𝑐𝑢𝑟𝑣𝑒
𝑑𝑖𝑠𝑡𝑟𝑖𝑏𝑢𝑡𝑖𝑜𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 (𝑝𝑜𝑤𝑒𝑟 𝑎𝑠 𝑎 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑜𝑓 𝑤𝑖𝑛𝑑 𝑠𝑝𝑒𝑒𝑑)
4
Wind Turbine Power Curve
Region I: 𝑣 < 𝑣in Region II: 𝑣in< 𝑣 < 𝑣rated
𝑊 𝑣 =0 𝑣 2 −𝑣𝑖𝑛
2
𝑊 𝑣 = 𝑝𝑟𝑎𝑡𝑒𝑑 2
𝑣𝑟𝑎𝑡𝑒𝑑 2 −𝑣𝑖𝑛
Region III: 𝑣rated< 𝑣 < 𝑣out Region IV: 𝑣 > 𝑣
out
𝑊 𝑣 = 𝑝𝑟𝑎𝑡𝑒𝑑 𝑊 𝑣 =0
5
Demo Summary
MATLAB language
– High-level
– Matrix-based
– No need for low-level administrative tasks
Math and graphics functions for engineering and science
Supports a range of programming styles
– Started working interactively
Command line, plotting
– Automated with a script
Editor, command history, comments
6
Range of Programming Techniques
value
variable
structure
Refine and improve code
Maintainable
Reusable/ more general
Robust
function
script
command line
7
Demo: Virus Dynamics Modeling
Goal: Modify the code for a working model of virus dynamics to make
it more maintainable, reusable, and robust
Approach:
– Organize code by using different function types
– Add error checking to validate inputs
– Allow different calling syntaxes to support
different use cases
8
Mathematical Model of Virus Dynamics
𝑑𝑇
𝑑𝑡 = −𝛽 𝑇 𝑉
𝑑𝐼
𝑑𝑡 =𝛽𝑇𝑉 − δ𝐼
𝑑𝑉
𝑑𝑡 =𝑝𝐼 −c𝑉
𝑇 – target (uninfected cells)
I – infected cells
V – free virions (virus particles)
Model parameters:
𝛽 - infection rate of uninfected cells
δ - death rate for infected cells
𝑝 𝛽 𝑇𝑜 If R>1,
𝑅= infection can
𝑝 - production rate of virus particles 𝑐𝛿 be established.
𝑐 - clearance rate of virus particles
9
Demo Summary
Started with working code
Refined and improved code
– Maintainable
Subfunctions, nested functions
– Reusable / more general
Function with flexible input arguments
– Robust
Error checking and validating inputs
Profiler to assess performance
Used Development tools
Code Analyzer, Debugger
10
Range of Programming Techniques
value
variable
structure
Manage larger,
complex applications
Organize data and
algorithms
function
script
command line
11
What is a program?
Data
x = 12
while (x < 100)
x = x+1
x = 12
if (x == 23)
while (x < 100)
x = x+1 disp('Hello')
end
if (x == 23)
end
disp('Hello')
end
end
Assignment
Looping Test
Increment
Test to Act
Take Action
Code End
End
Algorithm 12
Range of Programming Techniques
value Data
variable
structure
(properties)
class
(methods)
function
script
command line
Algorithm
13
Object-Oriented Terminology
Class
– Outline of an idea
– Properties (data)
– Methods (algorithms)
Object Element of the set – object
– Specific example of a class Example: Triangle
– Instance
Defined set – class
Example: Polygons
14
Object-Oriented Programming with MATLAB
Combines related data and algorithms
Class definition files – describe object behavior
– Build on existing classes with inheritance
– Control access to properties and methods with attributes
– Monitor object property changes and actions with events
and listeners
Use matrix-based aspects of MATLAB with objects
Packages – define scope (namespace) of functions
and classes
15
Packaging and Sharing MATLAB Apps
Create single file for distribution and installation into gallery
Packaging Tool:
– Automatically includes all necessary files
– Documents required products
16
Sharing Results from MATLAB
Automatically generate reports
– Publish MATLAB files
Create graphical user interfaces
– Programmatically
– GUIDE: GUI Design Environment
(includes a layout editor)
Package as an app
17
Deploying to Other Environments
Share individual algorithms or complete
applications
Create stand-alone applications and
software components
Generate portable C code for desktop or
embedded applications
18
Demo: Lane Marking System
Goal: Share lane marking video
processing system designed
in MATLAB
Approach:
– Deploy full application as a stand-alone
executable
– Generate stand-alone C code for underlying video
processing algorithm
19
Sharing Standalone Applications
Application Author End User
MATLAB + Toolboxes
MATLAB
Runtime
MATLAB Compiler
20
Deploying Applications with MATLAB
MATLAB
MATLAB MATLAB
Compiler Compiler SDK
MATLAB
Standalone Excel Production
Application Add-in Hadoop C/C++ Java .NET Python
Server
MATLAB Compiler for sharing MATLAB programs without integration
programming
MATLAB Compiler SDK provides implementation and platform flexibility for
software developers
Royalty-free distribution
21
Demo: Lane Marking System
Goal: Share lane marking video
processing system designed
in MATLAB
Approach:
– Deploy full application as a stand-alone
executable
– Generate stand-alone C code for underlying video
processing algorithm
22
Why translate MATLAB to C?
Deploy to desktops as source code or libraries
– Integration into existing C applications
– Standalone executables for prototyping
Deploy to embedded environments
Accelerate user-written MATLAB algorithms (via MEX)
23
MATLAB Compiler and MATLAB Coder
MATLAB Compiler MATLAB Coder
.exe Excel .c
MATLAB
Runtime .exe .lib .dll
24
MATLAB Compiler and MATLAB Coder
MATLAB
Runtime .c
MATLAB Compiler MATLAB Coder
Executable or software Portable and readable
Output component/library C source code
MATLAB language support Full Subset
Additional libraries MATLAB Runtime None
Supported toolboxes Most toolboxes Some toolboxes
25
Summary – MATLAB for Programming
High-level language
– Matrix-based
– Math and graphics functions
– Traditional programming language features
Interactive development environment
– Tools, visualizations, and help
Supports a range of programming styles
– Interactive command line, scripts and functions, object-oriented programming
26
Summary
MATLAB Development Environment
Command Window
– Provides immediate results by interactively executing commands
Data visualization
MATLAB Editor
– Debugger
– Code Analyzer
MATLAB Profiler
Additional tools for managing code and data
(e.g., shortcuts, code comparison, variable editor)
27
Summary
Multiple Ways to Get Help
doc
help <name>
Function Browser, function hints, tab completion
28
Resources
Webinars
– Object-Oriented Programming in MATLAB
– MATLAB for C/C++ Programmers
– MATLAB to C Made Easy
Videos and code examples
– MATLAB product page
– Documentation
MATLAB Central
– Open exchange for the MATLAB
and Simulink user community
29
© 2016 The MathWorks, Inc.
30