25
Intro to Matlab (2) syde252 Monday, 24 September, 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,

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

integration of curve

Monday, 24 September, 12