CS 100Lecture 231 CS100J Lecture 23 n Previous Lecture –MatLab and its implementation, continued. n This Lecture –MatLab demonstration.

Slides:



Advertisements
Similar presentations
StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Advertisements

COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
1 Eng. Mohamed El-Taher Eng. Ahmed Ibrahim. 2 1.FUNCTION SUMMARY polyfun  Polynomial functions are located in the MATLAB polyfun directory. For a complete.
Lecture 4.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Systems of Equations and Inequalities
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
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.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
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.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
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.
IC3003 B ASIC S CIENTIFIC C OMPUTING Lecture 1 Monday 08:30-11:30 U204a.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
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.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Monte Carlo Methods Versatile methods for analyzing the behavior of some activity, plan or process that involves uncertainty.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
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.
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”
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.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
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.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
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,
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
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,
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
Communication and Coding Theory Lab(CS491)
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Chapter 7: Matrices and Systems of Equations and Inequalities
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Programming Languages
Announcements P3 due today
-seminar 1 for digital signal processing
EECS Introduction to Computing for the Physical Sciences
CS100A Lecture 21 Previous Lecture This Lecture
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Computer Simulation Lab
Presentation transcript:

CS 100Lecture 231 CS100J Lecture 23 n Previous Lecture –MatLab and its implementation, continued. n This Lecture –MatLab demonstration

CS 100Lecture 232 MatLab 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

CS 100Lecture 233 MatLab Variables n MatLab has variables and assignment: >> evens = 2.*(1:8) evens = n To suppress output, terminate line with a semicolon (not to be confused with the vertical concatenation operator) >> odds = evens - 1; >> n To see the value of variable v, just enter expression v: >> odds odds =

CS 100Lecture 234 MatLab Idiom n MatLab has for-loops, conditional statements, subscripting, etc., like Java. But the preferred programming idiom uses uniform operations on matrices and avoids explicit loops, if possible. n Essentially every operation and built in function takes a matrix as an argument.

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

CS 100Lecture 236 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 : function e = euler(n) function e = euler(n) % Return a vector of approximations to pi. % Return a vector of approximations to pi. 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)

CS 100Lecture 237 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 Return a vector of approximations to pi. Return a vector of approximations to pi.

CS 100Lecture 238 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)) )

CS 100Lecture 239 Wallis Function Function Definition Function Definition function w = wallis(n) % compute successive approximations 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))

CS 100Lecture 2310 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

CS 100Lecture 2311 Darts The expression rand(h,w) computes an h-by-w matrix of random numbers between 0 and 1. The expression rand(h,w) computes 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); Let 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. Let 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; Let hits(i) be the number of darts in circle in i tries Let hits(i) be the number of darts in circle in i tries >> hits = cumsum(in); Let f(i) be franction of darts in circle in i tries Let f(i) be franction of darts in circle in i tries >> f = hits./ (1:10); Let pi be successive approximations to pi Let pi be successive approximations to pi >> pi = 4.* f;

CS 100Lecture 2312 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/ .

CS 100Lecture 2313 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 =

CS 100Lecture 2314 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

CS 100Lecture 2315 Control Structures: Loops while expression list-of-statementsend for variable = lo:hi list-of-statementsend for variable = lo:by:hi list-of-statementsend