Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS112 Scientific Computation Department of Computer Science Wellesley College Hodgepodge “Clumsy mixture of ingredients…”

Similar presentations


Presentation on theme: "CS112 Scientific Computation Department of Computer Science Wellesley College Hodgepodge “Clumsy mixture of ingredients…”"— Presentation transcript:

1 CS112 Scientific Computation Department of Computer Science Wellesley College Hodgepodge “Clumsy mixture of ingredients…”

2 Hodgepodge10-2 The return of Peter Piper function peterPiper (numReps) % peterPiper % repeats a tongue twister “numReps” times for count = 1:numReps disp('Peter Piper picked a peck of pickled peppers'); end Can we make the numReps input optional?

3 Hodgepodge10-3 Optional input arguments Inside a user-defined function, nargin returns the number of inputs entered when the function was called function peterPiper (numReps) % repeats a tongue twister multiple times % numReps input is optional if (nargin == 0) numReps = 5; end for count = 1:numReps disp('Peter Piper picked a peck of pickled peppers'); end

4 Hodgepodge10-4 Two forms of the if statement if (condition) … else … end if (condition) … end condition truefalse............ condition true false......

5 Hodgepodge10-5 A third form truefalse cond1 truefalse cond2 cond3 false.................. true...... if (cond1) … elseif (cond2) … elseif (cond3) … else … end

6 Hodgepodge10-6 The elseif clause in action function drawCircle (radius, xcenter, ycenter, properties, width) % drawCircle(radius, xcenter, ycenter, properties, width) % draws a circle with specified radius, centered on (xcenter, ycenter) % with the (optional) properties and width angles = linspace(0, 2*pi, 50); xcoords = xcenter + radius * cos(angles); ycoords = ycenter + radius * sin(angles); if (nargin == 3) plot(xcoords, ycoords, 'b', 'LineWidth', 1); elseif (nargin == 4) plot(xcoords, ycoords, properties, 'LineWidth', 1); else plot(xcoords, ycoords, properties, 'LineWidth', width); end axis equal

7 Hodgepodge10-7 Looping through a 2-D matrix count = 0; for row = 1:5 for col = 1:5 if (nums(row,col) ~= 0) count = count + 1; end 37006 20540 60219 03107 69052 nums But why bother with nested loops here?!? count = sum(sum(nums ~= 0))

8 Hodgepodge10-8 Counting peaks 14527 912735 02686 435102 18462 A peak is a value that is larger than its 4 neighbors* two peaks * Don’t bother checking locations around the border of the matrix

9 Hodgepodge10-9 How many peaks? function numPeaks = countPeaks (matrix) % counts the number of peaks in a matrix of numbers, where % a peak is a value that is larger than its 4 neighbors [rows cols] = size(matrix); numPeaks = 0; for row = 2:rows-1 for col = 2:cols-1 val = matrix(row, col); if (val > matrix(row-1, col)) &... (val > matrix(row+1, col)) &... (val > matrix(row, col+1)) &... (val > matrix(row, col-1)) numPeaks = numPeaks + 1; end

10 Hodgepodge10-10 Bighorn National Forest Needs You Nested loops for also useful for performing a computation for every combination of two or more values For example, we can create a distance table for the National Park Service

11 Hodgepodge10-11 Suppose… We are given the (x, y) coordinates of each site in a table named coords 232208 97 17399362 90 55 35277 coords 1 2 3 4 5 1 2 x coordinates y coordinates Locations of national parks

12 Hodgepodge10-12 Creating a distance table function distTable = makeDistanceTable (coords) % constructs and returns table with the distances between pairs % of cities whose coordinates are stored in the 2 x n input matrix numCities = size(coords, 2); distTable = zeros(numCities, numCities); % step through each possible pair of cities for cityA = 1:numCities for cityB = 1:numCities distance = sqrt((coords(1,cityA)- coords(1,cityB))^2 +... (coords(2,cityA)-coords(2,cityB))^2); distTable(cityA, cityB) = distance; end

13 Hodgepodge10-13 Creating simple animations A movie is a sequence of picture frames displayed in rapid succession In MATLAB, anything that can be displayed in a figure window can be made into a movie frame with the getframe function A sequence of frames can be stored in a vector and displayed with the movie function

14 Hodgepodge10-14 Quiet on the set function myMovie = makeCircleMovie (minRadius, maxRadius, nFrames) % creates frames of an expanding circle whose radius changes % from minRadius to maxRadius over numFrames frames angles = linspace(0, 2*pi, 50); frame = 1; for radius = linspace(minRadius, maxRadius, nFrames) plot(radius*cos(angles), radius*sin(angles)); axis equal axis([-maxRadius maxRadius -maxRadius maxRadius]); axis off myMovie(frame) = getframe; frame = frame + 1; end Return vector of movie frames Get snapshot of figure window and store it in current frame

15 Hodgepodge10-15 Action! >> circleMovie = makeCircleMovie(2, 10, 5); circleMovie >> movie(circleMovie, 10, 12); Name of vector storing movie frames Number of frames per second (default 12) Number of times to display movie (default 1) 12345

16 Hodgepodge10-16 Exercise – a random walk in the park Create a movie of a random walk that starts in the center of a 50x50 grid and progresses in unit steps chosen randomly from left, right, up and down


Download ppt "CS112 Scientific Computation Department of Computer Science Wellesley College Hodgepodge “Clumsy mixture of ingredients…”"

Similar presentations


Ads by Google