Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Slides:



Advertisements
Similar presentations
1 Chapter 13 Curve Fitting and Correlation This chapter will be concerned primarily with two separate but closely interrelated processes: (1) the fitting.
Advertisements

Insight Through Computing 10. Plotting Continuous Functions Linspace Array Operations.
Introduction to Matlab
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Insight Through Computing 13. More on Arrays Square Bracket Notation Subscripts Plotting and color Built-In Functions: ginput, fill, sum, axis.
1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial Plotting 11.
Insight Through Computing 26. Divide and Conquer Algorithms Binary Search Merge Sort Mesh Generation Recursion.
Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.
Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Yuval Hart, Weizmann 2010 © 1 Introduction to Matlab & Data Analysis Tutorial 2 and 3: Variables and Matrix Manipulation.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
Chapter 8 Loops while loop syntax while ;... end;.
Eilon Sharon, Weizmann 2008 © 1 Introduction to Matlab & Data Analysis Tutorial 2: Variables and Matrix Manipulation.
Insight Through Computing 18. Two-Dimensional Arrays Set-Up Rows and Columns Subscripting Operations Examples.
Week 3 Last week Vectors Array Addressing Mathematical Operations Array Multiplication and Division Identity Matrix Inverse of a Matrix Element by Element.
To install the TDA package on a PC: install.packages("TDA") To install the TDA package on a Mac: install.packages("TDA", type = "source") XX = circleUnif(30)
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Chapter 8 Loops while loop syntax while ;... end;.
CHAPTER 4 Coordinate Geometry and Traverse Surveying
CSE554SimplificationSlide 1 CSE 554 Lecture 7: Simplification Fall 2014.
HRSB, 2008 Algebra Tiles 1 x -x x2x2 -x 2. HRSB, 2008 Algebra Tiles y-y xy -y 2 y2y2 -xy.
CS 450: Computer Graphics REVIEW: OVERVIEW OF POLYGONS
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
CSE554AlignmentSlide 1 CSE 554 Lecture 5: Alignment Fall 2011.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Graphics Pipeline Rasterization CMSC 435/634. Drawing Terms Primitive – Basic shape, drawn directly – Compare to building from simpler shapes Rasterization.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
1 Chapter 3 Arrays (2) 1. Array Referencing 2. Common Operations 1. Slicing 2. Diminution 3. Augmentation 3. List of Commonly Used Built-in Functions 1.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
Introduction to Scientific Computing - - A Matrix Vector Approach Using Matlab Written by Charles F.Van Loan 陈 文 斌 // ORG: 复旦大学 (Revised.
Chapter 9 Transformations.
CSE554SimplificationSlide 1 CSE 554 Lecture 7: Simplification Fall 2013.
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.
PATTERN RECOGNITION LAB 3 TA : Nouf Al-Harbi::
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Anne Calder & Alan Cordero. Goals Create 1000 random polynomials up to degree 100 Calculate the number of real zeros on average for each degree Check.
CS418 Computer Graphics John C. Hart
9-4 Polygons in the Coordinate Plane Learn to draw polygons in the coordinate plane and find the lengths of their sides.
10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.
1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial Plotting 11.
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Honors Geometry.  We learned how to set up a polygon / vertex matrix  We learned how to add matrices  We learned how to multiply matrices.
Intro to Matlab Rogelio Long September 3, How to access MyDesktop Log in with your utep id and password.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Department of Computer Science Western Michigan University
CSE 554 Lecture 8: Alignment
MID-POINT CIRCLE ALGORITHM

Math Fundamentals Maths revisit.
1.6 Basic Constructions SOL: G4 Objectives: The Student Will …
Chapter 3 Arrays and Vectors
(c) 2002 University of Wisconsin, CS559
Lecture 05: Mid-point Ellipse algorithm Dr. Manal Helal – Fall 2014
StatLab Matlab Workshop
L14. Arrays and Functions Functions with array parameters.
CS 175 Project in AI Discussion -- matlab
L13. More on Arrays Square Bracket Notation Subscripts
Nonparametric density estimation and classification
Empirical Distributions
Presentation transcript:

Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand, randn, max

Insight Through Computing Row and Column Vectors >> v = [1 2 3] v = >> v = [1 ; 2 ; 3] v = Observe semicolons

Insight Through Computing zeros(, ) >> x = zeros(3,1) x = 0 >> x = zeros(1,3) x = 0 0 0

Insight Through Computing rand(, ) >> x = rand(3,1) x = >> x = rand(1,3) x =

Insight Through Computing randn(, ) >> x = randn(1,3) x = >> x = randn(3,1) x =

Insight Through Computing Normal Distribution with Zero Mean and Unit STD

Insight Through Computing Affirmations >> n = ; >> x = randn(n,1); >> ave = sum(x)/n ave = >> standDev = std(x) standDev =

Insight Through Computing length >> v = randn(1,5); >> n = length(v) n = 5 >> u = rand(5,1); >> n = length(u) n = 5 The length function doesn’t care about row or column orientation.

Insight Through Computing Augmenting Row Vectors >> x = [10 20] x = >> x = [x 30] x = >>

Insight Through Computing Augmenting Column Vectors >> x = [10;20] x = >> x = [x ; 30] x = Observe semicolons!

Insight Through Computing “Concatenating” Row Vectors >> x = [10 20] x = >> y = [ ] y = >> z = [x y] z =

Insight Through Computing “Concatenating” Column Vectors >> x = [10 ; 20]; >> y = [30 ; 40 ; 50]; >> z = [ x ; y ] z = Observe semicolons!

Insight Through Computing Application x = linspace(0,2*pi,100); y = sin(x); x = [x x+2*pi]; y = [y y]; plot(x,y) Plot sine across [0,4*pi] and use the fact that it has period 2pi.

Insight Through Computing x = linspace(0,2*pi,100); x = [ x x+2*pi ]; linspace(2*pi,4*pi,100)]

Insight Through Computing The Empty Vector x = [ ]; for k=1:50 if floor(sqrt(k))==sqrt(k) x = [x; k]; end x = x x =

Insight Through Computing Array Hints & Errors

Insight Through Computing Dimension Mismatch >> x = [1;2] x = 1 2 >> y = [3 4] y = 3 4 >> z = x+y ??? Error using ==> plus Matrix dimensions must agree. Can’t add a row vector to a column vector

Insight Through Computing Not a Syntax Error >> x = rand(3) x = You probably meant to say x = rand(1,3) or x = rand(3,1).

Insight Through Computing A Style Hint Assume n is initialized. a = zeros(1,n) for k=1:n a(k) = sqrt(k); end a = [ ]; for k=1:n a = [a sqrt(k)]; end Better because it reminds you of the size and shape of the array you set up.

Insight Through Computing Error: Out-ofRange Subscript >> x = [ ] x = >> c = x(4) ??? Index exceeds matrix dimensions.

Insight Through Computing This is OK… >> x = [ ] x = >> x(4) = 100 x =

Insight Through Computing Forgot the Semicolon? >> x = randn( ,1)

Insight Through Computing Forgot the Semicolon? >> x = randn( ,1) Remember: ctrl-C

Insight Through Computing Question Time A = [ 1 ]; while(length(A) < 5) A = [length(A)+1 ; A]; end A = A Is this the same as A = linspace(1,5,5) ? A. Yes B. No

Insight Through Computing No! Linspace: Fragment:

Insight Through Computing Question Time x = zeros(1,1); for k=1:3 x = [x x]; end y = x(7) Will this cause a subscript out of bounds error? A. Yes B. No

Insight Through Computing No! How x changes: After 1st pass: [0 0] After 2 nd pass: [ ] After 3 rd pass: [ ] So y = x(7) makes sense.

Insight Through Computing Polygon Transformations Functions & arrays

Insight Through Computing A Polygon (x 4,y 4 ) (x 1,y 1 ) (x 3,y 3 ) (x 2,y 2 ) (x 5,y 5 ) Store xy- coordinates in vectors x and y.

Insight Through Computing Operation 1: Centralize Move a polygon so that the centroid of its vertices is at the origin.

Insight Through Computing Before After Centralize

Insight Through Computing Centralize function [xNew,yNew] = Centralize(x,y) n = length(x); xBar = sum(x)/n; yBar = sum(y)/n; xNew = x-xBar; yNew = y-yBar; Computes the vertices of the new polygon Notice how length is used to figure out the size of the incoming vectors.

Insight Through Computing Operation 2: Normalize Shrink (enlarge) the polygon so that the vertex furthest from the (0,0) is on the unit circle.

Insight Through Computing Before After Normalize

Insight Through Computing function [xNew,yNew] = Normalize(x,y) d = max(sqrt(x.^2 + y.^2)); xNew = x/d; yNew = y/d; Normalize Applied to a vector, max returns the largest value in the vector.

Insight Through Computing Operation 3: Smooth Obtain a new polygon by connecting the midpoints of the edges

Insight Through Computing Smooth

Insight Through Computing Midpoints (a,b) ( (a+c)/2, (b+d)/2 ) (c,d)

Insight Through Computing function [xNew,yNew] = Smooth(x,y) n = length(x); xNew = zeros(n,1); yNew = zeros(n,1); for i=1:n Compute the mdpt of ith edge. Store in xNew(i) and yNew(i) end Smooth

Insight Through Computing (x 4,y 4 ) (x 1,y 1 ) (x 3,y 3 ) (x 2,y 2 ) (x 5,y 5 ) xNew(1) = (x(1)+x(2))/2 yNew(1) = (y(1)+y(2))/2

Insight Through Computing (x 4,y 4 ) (x 1,y 1 ) (x 3,y 3 ) (x 2,y 2 ) (x 5,y 5 ) xNew(2) = (x(2)+x(3))/2 yNew(2) = (y(2)+y(3))/2

Insight Through Computing (x 4,y 4 ) (x 1,y 1 ) (x 3,y 3 ) (x 2,y 2 ) (x 5,y 5 ) xNew(3) = (x(3)+x(4))/2 yNew(3) = (y(3)+y(4))/2

Insight Through Computing (x 4,y 4 ) (x 1,y 1 ) (x 3,y 3 ) (x 2,y 2 ) (x 5,y 5 ) xNew(4) = (x(4)+x(5))/2 yNew(4) = (y(4)+y(5))/2

Insight Through Computing (x 4,y 4 ) (x 1,y 1 ) (x 3,y 3 ) (x 2,y 2 ) (x 5,y 5 ) xNew(5) = (x(5)+x(1))/2 yNew(5) = (y(5)+y(1))/2

Insight Through Computing (x 4,y 4 ) (x 1,y 1 ) (x 3,y 3 ) (x 2,y 2 ) (x 5,y 5 ) xNew(5) = (x(5)+x(1))/2 yNew(5) = (y(5)+y(1))/2

Insight Through Computing for i=1:n xNew(i) = (x(i) + x(i+1))/2; yNew(i) = (y(i) + y(i+1))/2; end Will result in a subscript out of bounds error when i is n. Smooth

Insight Through Computing for i=1:n if i<n xNew(i) = (x(i) + x(i+1))/2; yNew(i) = (y(i) + y(i+1))/2; else xNew(n) = (x(n) + x(1))/2; yNew(n) = (y(n) + y(1))/2; end Smooth

Insight Through Computing for i=1:n-1 xNew(i) = (x(i) + x(i+1))/2; yNew(i) = (y(i) + y(i+1))/2; end xNew(n) = (x(n) + x(1))/2; yNew(n) = (y(n) + y(1))/2; Smooth

Insight Through Computing Proposed Simulation Create a polygon with randomly located vertices. Repeat: Centralize Normalize Smooth

Insight Through Computing

2D Random Walk

Insight Through Computing Start in middle tile. Repeat until boundary reached: Pick a compass heading* at random. Move one tile in that direction. * North, East, South, West 1-by-1 tiles

Insight Through Computing function [x y] = RandomWalk2D(N) k = 0; xc = 0; yc = 0; while abs(xc)<N && abs(yc)< N Take another hop. Update location (xc,yc). k = k + 1; x(k) = xc; y(k) = yc; end Function that Returns the Path

Insight Through Computing k x(k) y(k) : : :

Insight Through Computing if rand <.5 xc = xc + 1; % East else xc = xc - 1; % West end else if rand <.5 yc = yc + 1; % North else yc = yc - 1; % Sounth end

Insight Through Computing How Many Returns to (0,0)? % x and y are n-vectors. % How many times do we have % (x(k),y(k)) = (0,0)? m = 0; for k=1:length(x) if x(k)==0 && y(k)==0 m = m + 1; end