Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro...
Transcript of Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro...
![Page 1: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/1.jpg)
Intro to Matlab (2)syde252
Monday, 24 September, 12
![Page 2: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/2.jpg)
More on Matlab
Monday, 24 September, 12
![Page 3: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/3.jpg)
Matrix Comparison - watch
Monday, 24 September, 12
![Page 4: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/4.jpg)
Built logic functions for matrices
Monday, 24 September, 12
![Page 5: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/5.jpg)
Control flow - switch & case
Monday, 24 September, 12
![Page 6: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/6.jpg)
Control Loop - For Loop
Monday, 24 September, 12
![Page 7: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/7.jpg)
Short form for loops
Monday, 24 September, 12
![Page 8: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/8.jpg)
While loop - control flow
Monday, 24 September, 12
![Page 9: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/9.jpg)
Break - control flow
Monday, 24 September, 12
![Page 10: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/10.jpg)
MAtrix - array ops
Monday, 24 September, 12
![Page 11: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/11.jpg)
MAtrix ops
Monday, 24 September, 12
![Page 12: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/12.jpg)
Array ops
Monday, 24 September, 12
![Page 13: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/13.jpg)
M-files: scripts & functions
Monday, 24 September, 12
![Page 14: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/14.jpg)
Functions
Monday, 24 September, 12
![Page 15: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/15.jpg)
Scopes of variables
Matlab 2 - 13 pykc - Jan-8-10 E2.5 Signals & Systems – Matlab Tutorial 2
Functions
function r = myfunct (x) % Calculate the function: % r = x^3 - 2*x - 5 % x can be a vector
r = x.^3 - x.*2 -5;
Define function name and arguments Return variable
% on column 1 is a comment
» X = 0:0.05:3; » y = myfunct (x); » plot(x,y)
function myfunct.m
This is how plot on p.2-27 was obtained
Matlab 2 - 14 pykc - Jan-8-10 E2.5 Signals & Systems – Matlab Tutorial 2
Scopes of variables
! All variables used inside a function are local to that function ! Parameters are passed in and out of the function explicitly as
defined by the first line of the function ! You can use the keyword global to make a variable visible
everywhere ! As a good programming practice, only use global variables
when it is absolutely required
Matlab 2 - 15 pykc - Jan-8-10 E2.5 Signals & Systems – Matlab Tutorial 2
MATLAB Programming Style Guide (1)
! This Style Guideline is originally prepared by Mike Cook
" The first line of code in script m-files should be indicate the name of the file.
" The first line of function m-files has a mandatory structure. The first line of a function is a declaration line. It has the word function in it to identifies the file as a function, rather than a generic m-file. For example, for a function named abs_error.m, the the first line would be: function [X,Y] = abs_error(A,B)
" A block of comments should be placed at the top of the regular m
-files, and just after the function definition in function m-files. This is
the header comment block. The formats are different for m-files and functions.
Matlab 2 - 16 pykc - Jan-8-10 E2.5 Signals & Systems – Matlab Tutorial 2
Style Guide (2)
! Variables should have meaningful names. This will make your code easier to read, and will reduce the number of comments you will need. However here are some pitfalls about choosing variable names:
• Meaningful variable names are good, but when the variable name gets to 15 characters or more, it tends to obscure rather than improve code.
• The maximum length of a variable name is 19 characters and all variables must start with a character (not number).
• Be careful of naming a variable that will conflict with matlab's built-in functions, or reserved names: if, while, end, pi, sin, cos, etc.
• Avoid names that differ only in case, look similar, or differ only slightly from each other.
! Make good use of white space, both horizontally and vertically, it will improve the readability of your program greatly.
Monday, 24 September, 12
![Page 16: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/16.jpg)
MATLAB Programming Style Guide
Monday, 24 September, 12
![Page 17: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/17.jpg)
Style Guide (2)
Monday, 24 September, 12
![Page 18: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/18.jpg)
style guide (3)
Monday, 24 September, 12
![Page 19: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/19.jpg)
style guide (4)
Monday, 24 September, 12
![Page 20: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/20.jpg)
style guide (5)
Monday, 24 September, 12
![Page 21: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/21.jpg)
style guide (6) - a good example
Monday, 24 September, 12
![Page 22: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/22.jpg)
function of functions - fplot
Monday, 24 September, 12
![Page 23: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/23.jpg)
find zero
Monday, 24 September, 12
![Page 24: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/24.jpg)
find minimum
Monday, 24 September, 12
![Page 25: Intro to Matlab (2) - University of Waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · Intro to Matlab (2) syde252 Monday, 24 September, 12. More on Matlab Monday, 24 September,](https://reader035.fdocuments.in/reader035/viewer/2022062415/605b6575d10ba60724325a92/html5/thumbnails/25.jpg)
integration of curve
Monday, 24 September, 12