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

Slides:



Advertisements
Similar presentations
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Advertisements

Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
RAPTOR Syntax and Semantics By Lt Col Schorsch
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Introduction to Computing Science and Programming I
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Insight Through Computing 8. More Practice with Iteration and Conditionals Through Graphics For-Loop Problems Introduce While-Loops.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
More Looping Structures
CS112 Scientific Computation Department of Computer Science Wellesley College Cold Coffee Vectors and plotting.
Chapter 2 - Algorithms and Design
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Designing a Web Page with Tables. A text table: contains only text, evenly spaced on the Web page in rows and columns uses only standard word processing.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
Game Maker Terminology
CS112 Scientific Computation Department of Computer Science Wellesley College Building your own Functions.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lesson Two: Everything You Need to Know
Lec 16 Chapter 10 Nov 2, 11 Chapter 10, user interface design examples.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
CS112 Scientific Computation Department of Computer Science Wellesley College Loops Iteration with for loops.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Section 1.1 Introduction to Graphing Copyright ©2013, 2009, 2006, 2001 Pearson Education, Inc.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
CS112 Scientific Computation Department of Computer Science Wellesley College Tables More matrix fun.
CS112 Scientific Computation Department of Computer Science Wellesley College Blind to change More on GUIs.
CS112 Scientific Computation Department of Computer Science Wellesley College Decisions, Decisions… Conditional statements and expressions.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
TONGUE TWISTERS. Unique New York Woodchuck How much wood could a woodchuck chuck if a woodchuck could chuck wood?
CS112 Scientific Computation Department of Computer Science Wellesley College Building your own Functions.
L8. Iteration and Graphics Introduce Matlab Graphics More practice with iteration and boolean-type thinking Warm-up for functions and arrays.
CS 100Lecture 231 CS100J Lecture 23 n Previous Lecture –MatLab and its implementation, continued. n This Lecture –MatLab demonstration.
CS112 Scientific Computation Department of Computer Science Wellesley College Vectors and indexing Tools of the trade.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
1 Structured Programming EEN170 Programming in MATLAB.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
EEE 242 Computer Tools for Electrical Engineering
Control Structures Hara URL:
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
CS112 Scientific Computation Department of Computer Science Wellesley College Kodak moments 3-D Visualization and Color.
CS112 Scientific Computation Department of Computer Science Wellesley College Kodak moments 3-D Visualization and Color.
Chapter 3 Selection Statements
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
Python: Control Structures
Chapter 4 MATLAB Programming
Two-Dimensional Plots
More on Graphical User Interfaces
“Clumsy mixture of ingredients…”
Cold Coffee Vectors and plotting
Introduction to Graphing
Chapter 1 Graphs, Functions, and Models.
Topic 1: Problem Solving
Introduction to Graphing
CS100A Lecture 21 Previous Lecture This Lecture
Lecture 9: Implementing Complex Logic
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

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

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?

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

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

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

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

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 nums But why bother with nested loops here?!? count = sum(sum(nums ~= 0))

Hodgepodge10-8 Counting peaks 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

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

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

Hodgepodge10-11 Suppose… We are given the (x, y) coordinates of each site in a table named coords coords x coordinates y coordinates Locations of national parks

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))^ (coords(2,cityA)-coords(2,cityB))^2); distTable(cityA, cityB) = distance; end

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

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

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

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