CS100A Lecture 21 Previous Lecture This Lecture

Slides:



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

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,
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.
MATLAB - Basics Centro de Informática Universidade Federal de Pernambuco Aprendizagem de Máquina – IN1102 Arley Ristar –
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
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.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
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.
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
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.
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
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.
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.
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.
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.
CS 100Lecture 231 CS100J Lecture 23 n Previous Lecture –MatLab and its implementation, continued. n This Lecture –MatLab demonstration.
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,
Manipulating MATLAB Matrices Chapter 4
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,
Lecture: MATLAB Chapter 1 Introduction
Seminar 1 for DCSP Using Matlab.
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)
Array Creation ENGR 1181 MATLAB 02.
Introduction to Matlab
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
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
Matlab Basics Tutorial
Matlab Basics.
Math review - scalars, vectors, and matrices
EECS Introduction to Computing for the Physical Sciences
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Computer Simulation Lab
Presentation transcript:

CS100A Lecture 21 Previous Lecture This Lecture MatLab and its implementation, continued. This Lecture MatLab demonstration CS 100 Lecture 21

MatLab The notation j:k gives a row matrix consisting of the integers from j through k. >> 1:8 ans = 1 2 3 4 5 6 7 8 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 3 5 7 9 To get a vector of n linearly spaced points between lo and hi, use linspace(lo,hi,n) >> linspace(1,3,5) 1.0 1.5 2.0 2.5 3.0 To transpose a matrix m, write m’ >> (1:3)’ 1 2 3 CS 100 Lecture 21

MatLab Variables MatLab has variables and assignment: >> evens = 2.*(1:8) evens = 2 4 6 8 10 12 14 16 To suppress output, terminate line with a semicolon (not to be confused with vertical concatenation operator) >> odds = evens - 1; >> To see the value of variable v, just enter expression v: >> odds odds = 1 3 5 7 9 11 13 15 CS 100 Lecture 21

MatLab Idiom 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. Essentially every operation and built in function takes a matrix as an argument. CS 100 Lecture 21

sum, prod, cumsum, cumprod Function sum adds row elements, and the function prod multiplies row elements: >> sum(1:1000) ans = 500500 >> prod(1:5) 120 Function cumsum computes a row of the partial sums and cumprod computes a row of the partial products: >> cumprod(1:7) 1 2 6 24 120 720 5040 >> cumsum(odds) 1 4 9 16 25 36 49 64 CS 100 Lecture 21

Compute Pi by Euler Formula Leonard Euler (1707-1783) 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) To define a function, select New/m-file and type definition: function e = euler(n) % Return a vector of approximations to pi. e = sqrt( 6 .* cumsum(1 ./ (1:n) .^ 2)); Select SaveAs and save to a file with the same name as the function. To invoke: >> pi = euler(100); CS 100 Lecture 21

Help System Use on-line help system >> help euler >> help function ... description of how to define functions ... >> help euler Return a vector of approximations to pi. CS 100 Lecture 21

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

Wallis Function 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 >> plot(1:100, euler(100), 1:100, wallis(100)) CS 100 Lecture 21

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

Darts The expression rand(1,n) computes a row of n 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. >> 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. >> in = d2 <= 1; Let hits be the cumulative sum of darts in circle >> hits = cumsum(in); Let f be cumulative fraction of hits >> f = hits ./ (1:n); Let pi be successive approximations to pi >> pi = 4 .* f; CS 100 Lecture 21

Compute Pi by Throwing Needles 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 rules by parallel lines distance 1 apart. The probability that a needles intersects one of the ruled lines is 2/. Thus, as N approaches infinity, the fraction of needles intersection a ruled line approaches 2/. CS 100 Lecture 21

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

Control Structures: Conditionals if expression list-of-statements end else elseif expression . . . CS 100 Lecture 21

Control Structures: Loops while expression list-of-statements end for variable = lo:hi for variable = lo:by:hi CS 100 Lecture 21