Programming with MATLAB
Sean de Wolski
Application Engineer
© 2013 The MathWorks, Inc.1
Today’s Objectives
Introduce you to programming with MATLAB
– The MATLAB language
– Development tools
Demonstrate the range of
programming styles supported
– Interactive command line,
scripts and functions,
object-oriented programming
Show you how to program
effectively in MATLAB
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
𝑊 𝑣 =0
Region II: 𝑣in< 𝑣 < 𝑣rated
𝑣 2 −𝑣𝑖𝑛 2
𝑊 𝑣 = 𝑝𝑟𝑎𝑡𝑒𝑑 𝑣 2 2
𝑟𝑎𝑡𝑒𝑑 −𝑣𝑖𝑛
Region III: 𝑣rated< 𝑣 < 𝑣out
𝑊 𝑣 = 𝑝𝑟𝑎𝑡𝑒𝑑
Region IV: 𝑣 > 𝑣out
𝑊 𝑣 =0
5
Demo Summary Products Used
MATLAB
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 and functions
(Editor, command history, comments)
6
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 Products Used
MATLAB
Curve Fitting Toolbox
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
if (x == 23)
x = 12 disp('Hello')
while (x < 100) end
x = x+1 end
if (x == 23)
disp('Hello')
end Assignment
end Looping Test
Increment
Test to Act
Code Take Action
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, states)
– Methods (algorithms, behavior)
Object
– Specific example of a class
Element of the set – object
– Instance Example: Triangle
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
Deploying Applications with MATLAB
Toolboxes
1 MATLAB End-User
Desktop Machine
MATLAB Compiler
3
.exe
20
Deploying Applications with MATLAB
Give MATLAB code
to other users
– MATLAB apps
– MATLAB files
MATLAB Compiler
Share applications
with end users who MATLAB MATLAB MATLAB
Builder EX Builder JA Builder NE
do not need MATLAB
– Stand-alone
executables
– Shared libraries
.dll
– Software components .exe .lib
Excel Java Web .NET
Royalty-free distribution
21
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
Multiple Ways to Get Help
doc
help <name>
Function Browser,
function hints,
tab completion
29
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
30
MATLAB Central
File Exchange
– Download free files
– Over 19000K available – including functions,
apps, examples, and models
MATLAB Answers
– Ask programming questions
– Search 70000K+ existing answers
Cody
– Challenge and expand your
knowledge of MATLAB
Blogs
– Read commentary from MathWorks engineers who
design, build, and support MATLAB and Simulink
31
© 2013 The MathWorks, Inc.
35