Download - Introduction to Programming in MATLAB
1
Introduction to Programming in
MATLAB
Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Based on a work at www.peerinstruction4cs.org.
2
SCRIPTSAre your fingers tired of typing things again and again? Trying writing…
3
Using Scripts
• I entered some important data about my dog Diablo into MATLAB, including: – Her name (‘Diablo’) – Her weight (22 kg) – The radius of her torso (15 cm)
• I now want to calculate the circumference of her circular-shaped torso, using a script.
4
Variable Scope in Scripts
Circumference.m (script)d = 2 * r;circumf = pi * d;
Command Window» d = ‘Diablo’; » w = 22;» r = 15; » Circumference» circumfcircumf =
94.2478» d
I entered some data about my dog Diablo into MATLAB, including her name (Diablo), weight (22kg) and the radius of her torso (15cm). I now want to calculate the circumference of her
circular-shaped torso, using a script.
What is printed here?
a) 30b) Diabloc) Other/None/Error
5
FUNCTIONSAnother way to save yourself some typing:
6
What is the role of the pink part of the function definition line?
Circumference.m (function)function [ circumf ] = Circumference( r )d = 2 * r;circumf = pi * d;end
a) It tells you that you need to assign the input value to a variable named r before you call the function Circumference.
b) It tells you that Circumference takes one input, called r.c) It tells you that Circumference will output a value in a variable
named r.d) It tells you that Circumference returns the value of r.e) None/more than one/other
Which is NOT a valid way to call the Circumference function from the
Command Window?Circumference.m (function)function [ circumf ] = Circumference( r )d = 2 * r;circumf = pi * d;end
» circumf = Circumference(10);» Circumference(10);» c = Circumference(10);» x = 10; » c = Circumference(x);None/More than one/Other
7
a) b) c) d)
e)
Comm
and Window
8
Variable Scope in Functions
Circumference.m (function)function [ circumf ] = Circumference( r )d = 2 * r;circumf = pi * d;end
Command Window» d = ‘Diablo’; » w = 22;» r = 15; » circumf = Circumference(r)circumf =
94.2478» d
I entered some data about my dog Diablo into MATLAB, including his name (Diablo), weight (22kg) and the radius of his torso (15cm). I now want to calculate the circumference of his
circular-shaped torso, using a function.
What is printed here?
a) 30b) Diablo
c) Other/None/Error
9
(a) Functions work this way, OR (b) Scripts work this way
10
(a) Functions work this way, OR (b) Scripts work this way
11
Variable Scope in Functions
Circumference.m (function)function [ circumf ] = Circumference( r )d = 2 * r;r = ‘messing with r’;circumf = pi * d;end
Command Window» d = ‘Diablo’; » w = 22;» r = 15; » circumf = Circumference(r)circumf =
94.2478» r
I entered some data about my dog Diablo into MATLAB, including his name (Diablo), weight (22kg) and the radius of his torso (15cm). I now want to calculate the circumference of his
circular-shaped torso, using a function.
What is printed here?
a) 15b) messing with rc) messing with r 30
d) Other/None/Error
12
Scripts vs. FunctionsScripts• Location: in a file (file’s name will
be how you call the script)• Use: type script name in the
Command Window• Input/Output: must be careful to
have variable(s) with certain names set to input values before calling, then look at variable(s) with certain names to see what happened/output
• Variable scope: script has full access to all variables as if its code had been just typed in the Command Window
Functions• Location: in a file with same name as
the function• Use: type function name in Command
Window, include assignment of output and/or include input arguments
• Input/Output: input(s) are provided as specified arguments, output(s) are provided as specified return values
• Variable scope: function has no access to read any variables from the Command Window, except as they are explicitly provided in the input arguments. Function has no access to change/write to any variables to the Command Window, except as explicitly given in the outputs