1 CS100J 02 May Matlab and PI and other things The notation j:k gives a row matrix consisting of the integers from j through k. The notation j:k gives.

Slides:



Advertisements
Similar presentations
259 Lecture 17 Working with Data in MATLAB. Overview  In this lecture, we’ll look at some commands that are useful for working with data!  fzero  sum,
Advertisements

Chapter Matrices Matrix Arithmetic
Introduction to Matlab
Lecture 4.
BUFFON’S NEEDLE PROBLEM Abby Yinger Probability theory and stochastic processes for additional applications Geometric probability and stochastic geometry.
Sequences, Induction and Probability
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Class 25: Question 1 Which of the following vectors is orthogonal to the row space of A?
Systems of Equations and Inequalities
Solving Equations. Is a statement that two algebraic expressions are equal. EXAMPLES 3x – 5 = 7, x 2 – x – 6 = 0, and 4x = 4 To solve a equation in x.
Section 1.1 Numbers and Their Properties.
The Complex Numbers The ratio of the length of a diagonal of a square to the length of a side cannot be represented as the quotient of two integers.
Buffon’s Needle Todd Savage. Buffon's needle problem asks to find the probability that a needle of length ‘l’ will land on a line, given a floor with.
1 © 2010 Pearson Education, Inc. All rights reserved 10.1 DEFINITION OF A SEQUENCE An infinite sequence is a function whose domain is the set of positive.
Chapter Sequences and Series.
1 Preliminaries Precalculus Review I Precalculus Review II
Copyright © 2014, 2010 Pearson Education, Inc. Chapter 9 Further Topics in Algebra Copyright © 2014, 2010 Pearson Education, Inc.
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
Matlab tutorial course Lesson 2: Arrays and data types
Mathematics for Computer Graphics (Appendix A) Won-Ki Jeong.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
TechConnect Concrete Math.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Buffon’s Needle Problem Grant Weller Math 402. Georges-Louis Leclerc, Comte de Buffon French naturalist, mathematician, biologist, cosmologist, and author.
Foundations Basics: Notation, and other things Algebraic manipulations Indices, Logs, Roots and Surds Binomial expansion Trigonometric functions Trigonometric.
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
Slide 1-1 Copyright © 2006 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Math 002 College Algebra Final Exam Review.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
1 © 2010 Pearson Education, Inc. All rights reserved © 2010 Pearson Education, Inc. All rights reserved Chapter 11 Further Topics in Algebra.
1 © 2010 Pearson Education, Inc. All rights reserved © 2010 Pearson Education, Inc. All rights reserved Chapter 10 Further Topics in Algebra.
Slide 7- 1 Copyright © 2006 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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.
Monte Carlo Methods Versatile methods for analyzing the behavior of some activity, plan or process that involves uncertainty.
Circumference and Diameter 1/31/2006. Circumference A circle is a shape with all points the same distance from the center. It is named by the center.
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 11 Pairs and Vector of Random Variables Last Time Pairs of R.Vs. Marginal PMF (Cont.) Joint PDF Marginal PDF Functions of Two R.Vs Expected Values.
College Algebra Sixth Edition James Stewart Lothar Redlin Saleem Watson.
Lec-10 Manipulations of Matlab Matrices. Built-in functions The chapter will be covered quickly There are quite a few built-in functions in MATLAB – If.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Infinite Series Objective: We will try to find the sum of a series with infinitely many terms.
Slide Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley A set of equations is called a system of equations. The solution.
Hosted by Mr. Bagunolo Function Area Formula Circle, Rectangle, Square What if?
Changing Bases. Base 10: example number ³ 10² 10¹ 10 ⁰ ₁₀ 10³∙2 + 10²∙1 + 10¹∙ ⁰ ∙0 = 2120 ₁₀ Implied base 10 Base 8: 4110 ₈ 8³ 8².
Graphs of Equations Objective: To use many methods to sketch the graphs of equations.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Copyright © 2007 Pearson Education, Inc. Slide 7-1.
Section 1Chapter 1. 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. Objectives Basic Concepts Write sets using set notation. Use number.
Calculus I Ms. Plata Fortunately, several rules have been developed for finding derivatives without having to use the definition directly. Why?
Approximating Derivatives Using Taylor Series and Vandermonde Matrices.
CS 100Lecture 231 CS100J Lecture 23 n Previous Lecture –MatLab and its implementation, continued. n This Lecture –MatLab demonstration.
Circles: Arcs, Angles, and Chords. Define the following terms Chord Circle Circumference Circumference Formula Central Angle Diameter Inscribed Angle.
CS100A, Fall 1998, Lecture 191 CS100A, Fall 1998 Lecture 19, Thursday Nov 05 Matlab Concepts: Matlab arrays Matlab subscripting Matlab plotting.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Math Vocabulary Practice MCA prep. Denominator the part of a fraction that is below the line and that functions as the divisor of the numerator.
Circumference and Area of Circles Section 8.7. Goal Find the circumference and area of circles.
Buffon's needle problem
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
Real Numbers and Algebraic Expressions
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Vectors and Matrices I.
Sequences and Series.
Simultaneous Equations
Sequences Overview.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Chapter 7: Matrices and Systems of Equations and Inequalities
Programming Languages
Announcements P3 due today
Math review - scalars, vectors, and matrices
CS100A Lecture 21 Previous Lecture This Lecture
Presentation transcript:

1 CS100J 02 May Matlab and PI and other things The notation j:k gives a row matrix consisting of the integers from j through k. The notation j:k gives a row matrix consisting of the integers from j through k. >> 1:8 ans = The notation j:b:k gives a row matrix consisting of the integers from j through k in steps of b. The notation j:b:k gives a row matrix consisting of the integers from j through k in steps of b. >> 1:2:10 >> 1:2:10 ans = To get a vector of n linearly spaced points between lo and hi, use linspace(lo,hi,n) To get a vector of n linearly spaced points between lo and hi, use linspace(lo,hi,n) >> linspace(1,3,5) ans = n To transpose a matrix m, write m’ >> (1:3)’ ans = 1 2 3

2 sum, prod, cumsum, cumprod Function sum adds row elements, and function prod multiplies row elements: Function sum adds row elements, and function prod multiplies row elements: >> sum(1:1000) ans = >> prod(1:5) ans = Function cumsum computes a row of partial sums and cumprod computes a row of partial products: Function cumsum computes a row of partial sums and cumprod computes a row of partial products: >> cumprod(1:7) ans = >> cumsum(odds) ans =

3 Compute Pi by Euler Formula n Leonard Euler ( ) derived the following infinite sum expansion:  2 / 6 =  1/j 2 (for j from 1 to  ) >> pi = sqrt( 6.* cumsum(1./ (1:10).^ 2)); >> plot(pi) n To define a function, select New/m-file and type definition: % = a vector of approximations to pi. function e = euler(n) e= sqrt( 6.* cumsum(1./ (1:n).^ 2)); e= sqrt( 6.* cumsum(1./ (1:n).^ 2)); n Select SaveAs and save to a file with the same name as the function. n To invoke: >> pi= euler(100); >> plot(pi)

4 Help System n Use on-line help system >> help function >> help function... description of how to define functions description of how to define functions... >> help euler >> help euler = a vector of approximations to pi, using Euler’s appoximation = a vector of approximations to pi, using Euler’s appoximation

5 Compute Pi by Wallis Formula n John Wallis ( ) derived the following infinite sum expansion: (2*2) * (4*4) * (6*6) *... (2*2) * (4*4) * (6*6) *...  / 2 = (1*3) * (3*5) * (5*7) *... (1*3) * (3*5) * (5*7) *... Terms in Numerator Terms in Numerator evens.^ 2 evens.^ 2 n Terms in Denominator odds odds odds odds *3 3*5 5*7 7*9 9* *3 3*5 5*7 7*9 9*11... i.e. odds.* (odds + 2) i.e. odds.* (odds + 2) Quotient Quotient prod( (evens.^ 2)./ (odds.* (odds+2)) prod( (evens.^ 2)./ (odds.* (odds+2)) Successive approximations to Pi Successive approximations to Pi pi = 2.* cumprod( (evens.^2)./ (odds.* (odds+2)) ) pi = 2.* cumprod( (evens.^2)./ (odds.* (odds+2)) )

6 Wallis Function Function Definition Function Definition function w = wallis(n) % compute successive approx’s to pi. evens = 2.* (1:n); odds = evens - 1; odds2 = odds.* (odds + 2); w = 2.* cumprod( (evens.^ 2)./ odds2 ); Contrasting Wallis and Euler approximations Contrasting Wallis and Euler approximations >> plot(1:100, euler(100), 1:100, wallis(100))

7 Compute Pi by Throwing Darts n Throw random darts at a circle of radius 1 inscribed in a 2-by-2 square. n The fraction hitting the circle should be the ratio of the area of the circle to the area of the square: f =  / 4 f =  / 4 n This is called a Monte Carlo method x y 1 1

8 Darts (h,w) yields an h-by-w matrix of random numbers between 0 and 1. (h,w) yields an h-by-w matrix of random numbers between 0 and 1. >> x = rand(1,10); >> y = rand(1,10); Let d2 be the distance squared from the center of the circle. Let d2 be the distance squared from the center of the circle. >> d2 = (x.^ 2) + (y.^ 2); in be a row of 0’s and 1’s signifying whether the dart is in (1) or not in (0) the circle. Note 1 is used for true and 0 for false. in be a row of 0’s and 1’s signifying whether the dart is in (1) or not in (0) the circle. Note 1 is used for true and 0 for false. >> in = d2 > in = d2 <= 1; hits(i) be the number of darts in circle in i tries hits(i) be the number of darts in circle in i tries >> hits = cumsum(in); f(i) be franction of darts in circle in i tries f(i) be franction of darts in circle in i tries >> f = hits./ (1:10); pi be successive approximations to pi pi be successive approximations to pi >> pi = 4.* f;

9 Compute Pi by Throwing Needles In 1777, Comte de Buffon published this method for computing  : In 1777, Comte de Buffon published this method for computing  : N needles of length 1 are thrown at random positions and random angles on a plate ruled by parallel lines distance 1 apart. The probability that a needle intersects one of the ruled lines is 2/ . Thus, as N approaches infinity, the fraction of needles intersecting a ruled line approaches 2/ .

10 Subscripting n Subscripts start at 1, not zero. >> a = [1 2 3; 4 5 6; 7 8 9] ans = >> a(2,2) ans = 5 n A range of indices can be specified: >> a(1:2, 2:3) ans = n A colon indicates all subscripts in range: >> a(:, 2:3) ans =

11 Control Structures: Conditionals if expression list-of-statements list-of-statementsend if expression list-of-statements list-of-statementselse end if expression list-of-statements list-of-statements elseif expression list-of-statements list-of-statements elseif expression list-of-statements list-of-statementselse end

12 Control Structures: Loops while expression list-of-statementsend for variable = lo:hi list-of-statementsend for variable = lo:by:hi list-of-statementsend