120 likes | 265 Views
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 .
E N D
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.
Are your fingers tired of typing things again and again? Trying writing… Scripts
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.
Variable Scope in Scripts 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. Circumference.m (script) Command Window d = ‘Diablo’; w = 22; r = 15; Circumference circumf circumf = 94.2478 d d = 2 * r; circumf = pi * d; • 30 • Diablo • Other/None/Error What is printed here?
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 • It tells you that you need to assign the input value to a variable named r before you call the function Circumference. • It tells you that Circumference takes one input, called r. • It tells you that Circumference will output a value in a variable named r. • It tells you that Circumference returns the value of r. • None/more than one/other
Which is NOT a valid way to call the Circumference function from the Command Window? Circumference.m (function) circumf= Circumference(10); Circumference(10); c = Circumference(10); x = 10; c = Circumference(x); None/More than one/Other Command Window function [ circumf ] = Circumference( r ) d = 2 * r; circumf = pi * d; end
Variable Scope in Functions 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. Circumference.m (function) Command Window d = ‘Diablo’; w = 22; r = 15; circumf= Circumference(r) circumf= 94.2478 d function [ circumf ] = Circumference( r ) d = 2 * r; circumf = pi * d; end • 30 • Diablo • Other/None/Error What is printed here?
Variable Scope in Functions 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. Circumference.m (function) Command Window d = ‘Diablo’; w = 22; r = 15; circumf= Circumference(r) circumf= 94.2478 r function [ circumf ] = Circumference( r ) d = 2 * r; r = ‘messing with r’; circumf = pi * d; end • 15 • messing with r • messing with r 30 • Other/None/Error What is printed here?
Scripts vs. Functions Scripts 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 • 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