Matlab Advanced Programming. Matt Wyant University of Washington

Similar documents
Numerical Methods in Scientific Computation

Introduction to Scientific Computing with Matlab

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

LEARNING TO PROGRAM WITH MATLAB. Building GUI Tools. Wiley. University of Notre Dame. Craig S. Lent Department of Electrical Engineering

Getting Started with MATLAB

Matlab Introduction. Scalar Variables and Arithmetic Operators

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

개발과정에서의 MATLAB 과 C 의연동 ( 영상처리분야 )

Introduction to MATLAB

Computational Methods of Scientific Programming

An Introduction to Matlab5

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

MATLAB 1. Jeff Freymueller September 24, 2009

Chapter 4: Programming with MATLAB

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

Stokes Modelling Workshop

Introduction to Scientific Computing with Matlab

Introduction to MATLAB

MatLab Just a beginning

Advanced Programming Techniques in MATLAB

Session 3 Introduction to SIMULINK

MATLAB. Miran H. S. Mohammed. Lecture 1

Introduction to MATLAB

COP 1170 Introduction to Computer Programming using Visual Basic

User Defined Functions

MATLAB. Devon Cormack and James Staley

Dynamics and Vibrations Mupad tutorial

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

C/C++ Programming for Engineers: Matlab Branches and Loops

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Introduction to MATLAB

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

Hands-On Perl Scripting and CGI Programming

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 Introduction to MATLAB

MATLAB is a multi-paradigm numerical computing environment fourth-generation programming language. A proprietary programming language developed by

System Design S.CS301

MATLAB TUTORIAL WORKSHEET

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

MATLAB Part 1 Oct

CSE 341: Programming Languages

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Pace University. Fundamental Concepts of CS121 1

Desktop Command window

SIMPLE INPUT and OUTPUT:

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

Python Scripting for Computational Science

Introduction to Python Part 2

An Introduction to MATLAB

HERIOT-WATT UNIVERSITY DEPARTMENT OF COMPUTING AND ELECTRICAL ENGINEERING. B35SD2 Matlab tutorial 1 MATLAB BASICS

2015 The MathWorks, Inc. 1

MATLAB to iphone Made Easy

Important From Last Time

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Introduction to MATLAB

MATLAB GUIDE UMD PHYS401 SPRING 2012

Chapter 1 Introduction to MATLAB

function [s p] = sumprod (f, g)

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Python Scripting for Computational Science

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Fundamental Programming Principles: Variables and Func6ons

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

MATLAB Project: Getting Started with MATLAB

Matlab Tutorial, CDS

Introduction to MATLAB

Experiment 6 SIMULINK

Working with JavaScript

MS6021 Scientific Computing. MatLab and Python for Mathematical Modelling. Aimed at the absolute beginner.

Image Processing Matlab tutorial 2 MATLAB PROGRAMMING

Introduction to MATLAB. Recommended bibliography. Need more help? Session 1. Getting started.

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

Introduction to Matlab for Econ 511b

Standards/ Strands Essential Questions Skills Assessment. What are the strengths and weaknesses of Processing as a programming language?

COMS W4115. Programming Languages and Translators. ASML: White Paper

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

CSE/Math 485 Matlab Tutorial and Demo

STEPHEN WOLFRAM MATHEMATICADO. Fourth Edition WOLFRAM MEDIA CAMBRIDGE UNIVERSITY PRESS

Top-down design. Topic 9. Functions II User-defined Functions. Component Testing. Flow of Top-Down Design

Chapter 6 User-Defined Functions. dr.dcd.h CS 101 /SJC 5th Edition 1

Programming in MATLAB

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Summer 2009 REU: Introduction to Matlab

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

Introduction to Scientific Computing Tool Scilab

MATLAB GUIDE UMD PHYS401 SPRING 2011

Statistics for Biologists: Practicals

ECE Lesson Plan - Class 1 Fall, 2001

COP 1220 Introduction to Programming in C++ Course Justification

Page 1 of 7 E7 Spring 2009 Midterm I SID: UNIVERSITY OF CALIFORNIA, BERKELEY Department of Civil and Environmental Engineering. Practice Midterm 01

How to learn MATLAB? Some predefined variables

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Nuts & Bolts guide to MATLAB

Programming Exercise 3: Multi-class Classification and Neural Networks

Introduction to MatLab. Introduction to MatLab K. Craig 1

Transcription:

Matlab Advanced Programming Matt Wyant University of Washington

Matlab as a programming Language Strengths (as compared to C/C++/Fortran) Fast to write -no type declarations needed Memory allocation/deallocation handled automatically Functions and scripts can be accessed automatically from path Vector/Matrix operations in many dimensions built in Polymorphic with respect to matrix dimensions

Matlab as a programming Language Strengths Fairly easy to handle strings Easy input/output Easy to build a function library Mathematical functions built in Plotting and Visualization built in Additional toolboxes

Matlab as a programming Expensive! Language - Weaknesses Debugging complex code can be tricky Not object-oriented Behavior of built in functions must be learned. No type declarations needed (example) Namespace collisions (example)

Outline Scripts vs. Functions Techniques for writing functions Structures Cell-Arrays Examples

Not included Matlab graphics Object-oriented programming Interfaces to other languages Not much on handling matrices

Matlab Scripts plotsquared.m: x = [-5:5]; y = x.^2; plot(x,y) Run with plotsquared

Advantages of Scripts Easy to write Easy to patch together and nest Can be cut and paste directly into Matlab command line Can use any variables already in the workspace

Disadvantages of Scripts Scripts have uncertain variable scope. Scripts are hard to modularize. Modifications can be cumbersome. Script inputs and outputs are not explicit. Scripts are hard to loop over safely. Scripts can be cumbersome to test.

Ways to protect against scope clear all problems in scripts include at top of script or type it sometimes not desirable Initialize all variables you use Beware assignment to parts of an uninitialized array

Suppose y is a 20x20 array. Then the following script is run: x = [-5:5]; y(1:11) = x.^2; plot(x,y)

Fixed version: y = zeros(1:11) x = [-5:5]; y(1:11) = x.^2; plot(x,y)

Matlab Functions Example (complex_phase_and_magnitude.m) Arguments are passed by value. All variables are internal (unless you use GLOBAL variables), so your workspace is not altered. Can be called with fewer than specified input arguments. Can be called with fewer than specified output arguments. Functions can have indefinite arguments at the end of the argument list

Advantages of Functions Reuseable develop your own libraries, or get functions from others Robust rules all in one place (debug shared methods once) Sharable Self-documenting Can be used easily customize built-in matlab functions Provide variable scope safety Can be called inside of scripts one rule, one place

Disadvantages of Functions Designing them takes time and practice. Debugging can be more involved than for scripts.

Guidelines for writing functions Write at least minimal documentation required inputs optional inputs outputs units Use a descriptive function name Use descriptive variable names Possibly check the input arguments size number of dimensions type

Guidelines for writing functions If you can, don t assume the size of input matrices Write functions to apply a process to whole matrices, not just scalars (avoid looping over matrices). If you see repeated similar procedures, consider looping over them or making them a function. If you can, don t assume the size of input matrices Avoid GLOBAL variables. Make them arguments instead. If you need persistent variables use persistent. If you need lots of physical constants pass them in as a structure) Small simple functions can be very useful (examples)

Evolve a script into a function Choose a script or piece of a script that has a narrowly defined purpose. Choose a script or piece of a script that you are likely going to want to use multiple times. Identify the parameters. Gather all parameters at the top of the script. Decide which parameters are going to be changed often. These will be the input arguments. Identify the useful outputs of the script. These will be the output arguments. Choose a good name!

Matlab Structures Group variables Group data of different sizes/types (metadata with variables) Sort by name Nest

Cell-arrays Containers for multiple Matlab objects of different types and sizes Useful for grouping character strings Can be used as comma-separated lists ()used to get subset of cell array {}used to get contents of cells

Examples Exploiting Matlab matrix handling Passing back complex output using structures Extending existing Matlab functions using varargin Looping over variables with cell-arrays

Exploiting Matlab matrix handling Many built-in and user made functions can handle arbitrarily sized matrices. Matrices typically must all be the same size. The functions must use element-by-element math methods on these matrices: x.^ y x.* y x./ y Example: (qsat.m), (sat_vapor_pressure.m)

Passing back complex output using structures Example: retrieve_sounding.m Saves all useful information about a procedure or computation. Adding new info won t break existing functions or scripts. Results can be passed to other functions or grouped in arrays.

Extending existing Matlab functions varargin allows you to create functions with an unspecified number of arguments. Use varargin as last argument in your function. In body of the function, the cell array called varargin contains a list of the extra arguments. Example: fitscatterplot.m, fitscatterplot2.m

Looping over variables with cellarrays Cell-arrays provide a convenient way to loop through variable data (noloop.m, loop.m).

References Matlab Help help help [topic] help [function name] doc [function name] www.mathworks.com Graphics and GUI s with MATLAB, Patrick Marchand and O. Thomas Holland This talk will be placed (soon) at www.atmos.washington.edu/~mwyant

Additional Topics Matrix manipulations and higher dimensional matrices Handle Graphics Techniques Matlab Graphics for Publication and Presentation Performance Tips NetCDF Interface Handling Missing Data Interface with C/C++ or Fortran