Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS112 Scientific Computation Department of Computer Science Wellesley College Building your own Functions.

Similar presentations


Presentation on theme: "CS112 Scientific Computation Department of Computer Science Wellesley College Building your own Functions."— Presentation transcript:

1 CS112 Scientific Computation Department of Computer Science Wellesley College Building your own Functions

2 8-2 Three big ideas Programmers use three important concepts to help manage the complexity of their projects: Abstraction Modularity Divide, Conquer and Glue ENIAC

3 Functions8-3 Big idea number 1: Abstraction Function Black Box Sohie You Implementer / Designer User / Client Contract

4 Functions8-4 Big idea number 2: Modularity Large systems are built from components called modules Modules may capture parts of the task that could be re-used in other contexts The interfaces between modules are designed so they can be put together in a mix-and-match way

5 Functions8-5 Big idea number 3: Divide, conquer & glue P P1P1 P2P2 P3P3 P4P4 S4S4 S3S3 S2S2 S1S1 S Divide problem P into subproblems Conquer each of the subproblems Glue combine the solutions to the subproblems into a solution S for P

6 Functions8-6 Functions help us to do all three Functions encapsulate code for performing common tasks Functions help us to hide programming details and modularize our programs Functions help us to manage the divide-conquer-and-glue strategy

7 Functions8-7 Built-in MATLAB functions math:sum, prod, mean, std, abs, sqrt, sin, cos, abs, exp, min, max logical:any, all, and, or, not creation:linspace, colon, ones, zeros dimensions: size, length graph/display: plot, figure, subplot, xlabel, ylabel, title, axis, legend, imshow, imtool input/output: input, disp

8 Functions8-8 The other side of the contract We’ve been using functions built by others - let’s try writing a few of our own Implement a myMean function that returns a single value representing the average value of a vector or matrix

9 Functions8-9 Rules of the road function avg = myMean (data) % avg = myMean(data) % returns the average of all of the values % in data, which may be a vector or matrix dims = size(data); if (min(dims) == 1) avg = sum(data)/length(data); else avg = sum(sum(data))/prod(dims); end output parameter function name (stored in file myMean.m) input parameter help comments contract function body assign output local variable* * Local variables only exist during the execution of the function keyword

10 Functions8-10 Calling the new myMean function >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) meanVal = 5.6000 >> nums = [3 4 7; 2 8 6] nums = 3 4 7 2 8 6 >> meanVal = myMean(nums) meanVal = 5.0000

11 Functions8-11 Executing a function MATLAB workspace Execution land >>

12 Functions8-12 Create nums vector MATLAB workspace Execution land >> nums = [3 9 6 2 8]; nums 39628

13 Functions8-13 Assignment statement MATLAB workspace Execution land >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) nums 39628 meanVal

14 Functions8-14 Invoke myMean function MATLAB workspace Execution land >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) nums 39628 meanVal myMean local workspace function avg = myMean(data) dims = size(data); if (min(dims) == 1) avg = sum(data)/length(data); else avg = sum(sum(data))/prod(dims); end

15 Functions8-15 Create variables for input parameters MATLAB workspace Execution land >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) nums 39628 meanVal myMean local workspace data function avg = myMean(data) dims = size(data); if (min(dims) == 1) avg = sum(data)/length(data); else avg = sum(sum(data))/prod(dims); end

16 Functions8-16 Assign input parameters to call values MATLAB workspace Execution land >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) nums 39628 meanVal myMean local workspace data 39628 function avg = myMean(data) dims = size(data); if (min(dims) == 1) avg = sum(data)/length(data); else avg = sum(sum(data))/prod(dims); end

17 Functions8-17 Execute function body MATLAB workspace nums 39628 meanVal myMean local workspace data 39628 Execution land >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) function avg = myMean(data) dims = size(data); if (min(dims) == 1) avg = sum(data)/length(data); else avg = sum(sum(data))/prod(dims); end dims 15

18 Functions8-18 Is min(dims) == 1? MATLAB workspace nums 39628 meanVal myMean local workspace data 39628 Execution land >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) function avg = myMean(data) dims = size(data); if (min(dims) == 1) avg = sum(data)/length(data); else avg = sum(sum(data))/prod(dims); end dims 15

19 Functions8-19 Yes, so we do 'then' clause MATLAB workspace nums 39628 meanVal myMean local workspace data 39628 Execution land >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) function avg = myMean(data) dims = size(data); if (min(dims) == 1) avg = sum(data)/length(data); else avg = sum(sum(data))/prod(dims); end dims 15 avg 5.60

20 Functions8-20 Return value stored in output variable MATLAB workspace nums 39628 meanVal myMean local workspace data 39628 Execution land >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) function avg = myMean(data) dims = size(data); if (min(dims) == 1) avg = sum(data)/length(data); else avg = sum(sum(data))/prod(dims); end dims 15 avg 5.60

21 Functions8-21 And the local workspace goes away MATLAB workspace nums 39628 meanVal Execution land >> nums = [3 9 6 2 8]; >> meanVal = myMean(nums) meanVal = 5.6000 5.60

22 Functions8-22 Try another one? Write a function to draw a circle Think first about the contract: use inputs to control appearance: radius, location, color, markers, line style, line width Call the new function drawCircle and store it in an M-File named drawCircle.m

23 Functions8-23 Getting started function drawCircle (radius, xcenter, ycenter, properties, width) % drawCircle(radius, xcenter, ycenter, properties, width) % draws circle with the specified radius, centered on location % (xcenter, ycenter) with the specified properties and width don’t forget contract comments!

24 Functions8-24 Filling in the function body function drawCircle (radius, xcenter, ycenter, properties, width) % drawCircle(radius, xcenter, ycenter, properties, width) % draws circle with the specified radius, centered on location % (xcenter, ycenter) with the specified properties and width angles = linspace(0, 2*pi, 50); xcoords = xcenter + radius * cos(angles); ycoords = ycenter + radius * sin(angles); plot(xcoords, ycoords, properties, 'LineWidth', width);

25 Functions8-25 Executing drawCircle function Execution land >> drawCircle(40, 50, 50, 'g-*', 1); MATLAB workspace

26 Functions8-26 MATLAB workspace Execution land >> drawCircle(40, 50, 50, 'g-*', 1); function drawCircle (radius, xcenter, ycenter, … properties, width) angles = linspace(0, 2*pi, 50); xcoords = xcenter + radius * cos(angles); ycoords = ycenter + radius * sin(angles); plot(xcoords, ycoords, properties,... 'LineWidth', width); Create input parameter variables... drawCircle local workspace radius xcenter ycenter properties width

27 Functions8-27 MATLAB workspace Execution land >> drawCircle(40, 50, 50, 'g-*', 1); function drawCircle (radius, xcenter, ycenter,... properties, width) angles = linspace(0, 2*pi, 50); xcoords = xcenter + radius * cos(angles); ycoords = ycenter + radius * sin(angles); plot(xcoords, ycoords, properties,... 'LineWidth', width);... and fill them in from call statement drawCircle local workspace radius 40 xcenter 50 ycenter 50 properties g-* width 1

28 Functions8-28 MATLAB workspace Execution land >> drawCircle(40, 50, 50, 'g-*', 1); function drawCircle (radius, xcenter, ycenter,... properties, width) angles = linspace(0, 2*pi, 50); xcoords = xcenter + radius * cos(angles); ycoords = ycenter + radius * sin(angles); plot(xcoords, ycoords, properties,... 'LineWidth', width); Execute body of function drawCircle local workspace radius 40 xcenter 50 ycenter 50 properties g-* width 1 angles …

29 Functions8-29 MATLAB workspace Execution land >> drawCircle(40, 50, 50, 'g-*', 1); function drawCircle (radius, xcenter, ycenter,... properties, width) angles = linspace(0, 2*pi, 50); xcoords = xcenter + radius * cos(angles); ycoords = ycenter + radius * sin(angles); plot(xcoords, ycoords, properties,... 'LineWidth', width); Next statement drawCircle local workspace radius 40 xcenter 50 ycenter 5 properties g-* width 1 angles xcoords … …

30 Functions8-30 MATLAB workspace Execution land >> drawCircle(40, 50, 50, 'g-*', 1); function drawCircle (radius, xcenter, ycenter,... properties, width) angles = linspace(0, 2*pi, 50); xcoords = xcenter + radius * cos(angles); ycoords = ycenter + radius * sin(angles); plot(xcoords, ycoords, properties,... 'LineWidth', width); Next statement drawCircle local workspace radius 40 xcenter 50 ycenter 5 properties g-* width 1 angles xcoords … … ycoords …

31 Functions8-31 MATLAB workspace Execution land >> drawCircle(40, 50, 50, 'g-*', 1); function drawCircle (radius, xcenter, ycenter,... properties, width) angles = linspace(0, 2*pi, 50); xcoords = xcenter + radius * cos(angles); ycoords = ycenter + radius * sin(angles); plot(xcoords, ycoords, properties,... 'LineWidth', width); And we draw the circle drawCircle local workspace radius 40 xcenter 50 ycenter 5 properties g-* width 1 angles xcoords … … ycoords …

32 Functions8-32 MATLAB workspace Execution land >> drawCircle(40, 50, 50, 'g-*', 1); Where’d everybody go?

33 Functions8-33 A thorough test % testCircle.m % tests the drawCircle function hold on drawCircle(40, 50, 50, 'g-*', 1); drawCircle(20, 40, 60, 'b:^', 2); drawCircle(25, 30, 40, 'r-.s', 2); legend('one', 'two', 'three'); drawCircle(10, 20, 20, 'm--o', 1); axis equal axis([0 100 0 100]) hold off

34 Functions8-34 Functions with multiple outputs Suppose we’d like to write a variation of myMean that returns both the arithmetic mean and geometric mean Given n numbers: a 1, a 2 … a n arithmetic mean: (a 1 + a 2 + … + a n )/n geometric mean: √ a 1 * a 2 * … * a n n

35 Functions8-35 Consider first... Some built-in functions can return one or more values, depending on how the function is called For example, consider the min function: >> nums = [3 9 6 2 8]; >> minVal = min(nums) minVal = 2 >> [minVal minIndex] = min(nums) minVal = 2 minIndex = 4

36 Functions8-36 New lean mean machine function [arith geom] = myMean2(data) % [arith geom] = myMean2(data) % returns both the arithmetic and geometric mean of % the values in data, which may be a vector or matrix dims = size(data); if (min(dims) == 1) arith = sum(data)/length(data); geom = nthroot(prod(data), length(data)); else arith = sum(sum(data))/prod(dims); geom = nthroot(prod(prod(data)), prod(dims)); end

37 Functions8-37 Credit Eniac: http://en.wikipedia.org/wiki/ENIAC US Army photo from the archives of the ARL Technical Library


Download ppt "CS112 Scientific Computation Department of Computer Science Wellesley College Building your own Functions."

Similar presentations


Ads by Google