intro to matlab (2) - university of waterloojzelek/teaching/syde252/matlab-tutorial2.pdf · intro...

Post on 18-Oct-2020

12 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Intro to Matlab (2)syde252

Monday, 24 September, 12

More on Matlab

Monday, 24 September, 12

Matrix Comparison - watch

Monday, 24 September, 12

Built logic functions for matrices

Monday, 24 September, 12

Control flow - switch & case

Monday, 24 September, 12

Control Loop - For Loop

Monday, 24 September, 12

Short form for loops

Monday, 24 September, 12

While loop - control flow

Monday, 24 September, 12

Break - control flow

Monday, 24 September, 12

MAtrix - array ops

Monday, 24 September, 12

MAtrix ops

Monday, 24 September, 12

Array ops

Monday, 24 September, 12

M-files: scripts & functions

Monday, 24 September, 12

Functions

Monday, 24 September, 12

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

MATLAB Programming Style Guide

Monday, 24 September, 12

Style Guide (2)

Monday, 24 September, 12

style guide (3)

Monday, 24 September, 12

style guide (4)

Monday, 24 September, 12

style guide (5)

Monday, 24 September, 12

style guide (6) - a good example

Monday, 24 September, 12

function of functions - fplot

Monday, 24 September, 12

find zero

Monday, 24 September, 12

find minimum

Monday, 24 September, 12

integration of curve

Monday, 24 September, 12

top related