



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of matlab as a programming language, focusing on its strengths and weaknesses compared to other languages like c/c++/fortran. It also covers the use of scripts and functions, techniques for writing functions, and guidelines for writing effective functions. Examples and references for further learning.
Typology: Study notes
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















Matt Wyant University of Washington
Fast to write -no type declarations needed - Memory allocation/deallocation handledautomatically - Functions and scripts can be accessedautomatically from path - Vector/Matrix operations in many dimensionsbuilt in - Polymorphic with respect to matrix dimensions
Scripts vs. Functions
Techniques for writing functions - Structures - Cell-Arrays - Examples
plotsquared.m: x = [-5:5]; y = x .^2; plot(x,y) Run with ‘plotsquared’
Suppose y is a 20x20 array. Then the following script is run: x = [-5:5]; y(1:11) = x .^2; plot(x,y)
Example (complex_phase_and_magnitude.m) - Arguments are ‘passed by value’. - All variables are internal (unless you useGLOBAL variables), so your workspace is notaltered. - Can be called with fewer than specified inputarguments. - Can be called with fewer than specified outputarguments. - Functions can have indefinite arguments at theend of the argument list
Reuseable – develop your own libraries, or getfunctions from others - Robust – rules all in one place (debug sharedmethods once) - Sharable - Self-documenting - Can be used easily customize built-in matlabfunctions - Provide variable scope safety - Can be called inside of scripts - ‘one rule, one place’
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
If you can, don’t assume the size of input matrices - Write functions to apply a process to whole matrices, notjust scalars (avoid looping over matrices). - If you see repeated similar procedures, consider loopingover 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)