Programming With MATLAB - Michigan State University

Transcription

Programming with MATLABSean de WolskiApplication Engineer 2013 The MathWorks, Inc.1

Today’s Objectives Introduce you to programming with MATLAB– The MATLAB language– Development tools Demonstrate the range ofprogramming styles supported– Interactive command line,scripts and functions,object-oriented programming Show you how to programeffectively in MATLAB2

Demo: Assessment of Wind Turbine Locations Goal: Create analysis to choose best windturbine location by estimating powergeneration at multiple locations Approach:– Interactively explore data and developanalysis approach Load and preprocess datafrom observation towersFit wind speed probability distributionEstimate average power– Automate to run on datafrom other locations3

Calculating Average Turbine Power𝐴𝑣𝑒𝑟𝑎𝑔𝑒 𝑃𝑜𝑤𝑒𝑟 𝑣𝑚𝑎𝑥𝑓 𝑣 𝑊 𝑣 𝑑𝑣𝑣𝑚𝑖𝑛𝑓 𝑣 𝑖𝑠 𝑡ℎ𝑒 𝑤𝑖𝑛𝑑 𝑠𝑝𝑒𝑒𝑑 ��𝑡𝑟𝑖𝑏𝑢𝑡𝑖𝑜𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛𝑊 𝑣 𝑖𝑠 𝑡ℎ𝑒 𝑡𝑢𝑟𝑏𝑖𝑛𝑒 𝑝𝑜𝑤𝑒𝑟 𝑐𝑢𝑟𝑣𝑒(𝑝𝑜𝑤𝑒𝑟 𝑎𝑠 𝑎 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑜𝑓 𝑤𝑖𝑛𝑑 𝑠𝑝𝑒𝑒𝑑)4

Wind Turbine Power CurveRegion I: 𝑣 𝑣in𝑊 𝑣 0Region II: 𝑣in 𝑣 𝑣rated𝑊 𝑣 𝑣 2 𝑣𝑖𝑛 2𝑝𝑟𝑎𝑡𝑒𝑑 𝑣22𝑟𝑎𝑡𝑒𝑑 𝑣𝑖𝑛Region III: 𝑣rated 𝑣 𝑣out𝑊 𝑣 𝑝𝑟𝑎𝑡𝑒𝑑Region IV: 𝑣 𝑣out𝑊 𝑣 05

Demo Summary Products Used MATLABMATLAB language– High-level– Matrix-based– No need for low-leveladministrative tasks Math and graphics functionsfor 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 ofvirus dynamics to make it more maintainable,reusable, and robust Approach:– Organize code by using differentfunction types– Add error checking to validate inputs– Allow different calling syntaxes tosupport different use cases8

Mathematical Model of Virus ��𝑡 𝛽 𝑇 𝑉 𝛽𝑇𝑉 δ𝐼 𝑝𝐼 c𝑉𝑇 – target (uninfected cells)I – infected cellsV – free virions (virus particles)Model parameters:𝛽 - infection rate of uninfected cellsδ - death rate for infected cells𝑝 - production rate of virus particles𝑐 - clearance rate of virus particles𝑝 𝛽 𝑇𝑜𝑅 𝑐𝛿If R 1,infection canbe established.9

Demo Summary Started with working code Refined and improved codeProducts Used MATLAB Curve Fitting Toolbox– Maintainable Subfunctions, nested functions– Reusable / more general Function with flexible input arguments– Robust Error checking and validating inputs Profiler to assess performanceUsed development tools– Code Analyzer, Debugger10

Range of Programming Techniquesvaluevariablestructure Manage larger,complex applications Organize dataand algorithmsfunctionscriptcommand line11

What is a program?Datax 12while (x 100)x x 1if (x 23)disp('Hello')endendCodex 12while (x 100)x x 1if (x 23)disp('Hello')endendAssignmentLooping TestIncrementTest to ActTake ActionEndEndAlgorithm12

Range of Programming ass(methods)functionscriptcommand lineAlgorithm13

Object-Oriented Terminology Class– Outline of an idea– Properties (data, states)– Methods (algorithms, behavior) Object– Specific example of a class– InstanceElement of the set – objectExample: TriangleDefined set – classExample: Polygons14

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 andmethods with attributes– Monitor object property changes andactions with events and listeners Use matrix-based aspects ofMATLAB with objects Packages – define scope (namespace)of functions and classes15

Packaging and Sharing MATLAB Apps Create single file for distributionand installation into gallery Packaging tool:– Automatically includes all necessary files– Documents required products16

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 app17

Deploying to Other Environments Share individual algorithms orcomplete applications Create stand-alone applicationsand software components Generate portable C code fordesktop or embedded applications18

Deploying Applications with MATLABToolboxesMATLABDesktop1End-UserMachineMATLAB Compiler23.exe20

Deploying Applications with MATLAB Give MATLAB codeto other users– MATLAB apps– MATLAB filesMATLAB Compiler Share applicationswith end users whodo not need MATLABMATLABBuilder EXMATLABBuilder JAMATLABBuilder NE– Stand-aloneexecutables– Shared libraries– Software components .exe.dll.libExcelJavaWeb.NETRoyalty-free distribution21

Summary – MATLAB for Programming High-level language– Matrix-based– Math and graphics functions– Traditional programminglanguage features Interactive development environment– Tools, visualizations, and help Supports a range of programming styles– Interactive command line, scripts and functions,object-oriented programming26

SummaryMultiple Ways to Get Help doc help name Function Browser,function hints,tab completion29

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 MATLABand Simulink user community30

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 yourknowledge of MATLAB Blogs– Read commentary from MathWorks engineers whodesign, build, and support MATLAB and Simulink31

2013 The MathWorks, Inc.35

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