EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.

Slides:



Advertisements
Similar presentations
Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Advertisements

Welcome to EGR 106 Foundations of Engineering II Course information Today’s specific topics: – Computation and algorithms – M ATLAB Basics Demonstrations.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
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.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Digital Image Processing Lecture3: Introduction to MATLAB.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Part 1 Chapter 2 MATLAB Fundamentals
MATLAB Fundamentals.
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.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
THE MATLAB ENVIRONMENT VARIABLES BASIC COMMANDS HELP HP 100 – MATLAB Wednesday, 8/27/2014
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
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.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
MCE 372 Engineering Analysis MATLAB Review. M ATLAB – What Is It ? Where Is It? Name is from matrix laboratory Powerful tool for – Computation and visualization.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
M ATLAB – What Is It ? Name is from matrix laboratory Powerful tool for – Computation and visualization of engineering and science mathematics – Communication.
Matlab for Engineers Matlab Environment Chapter 2.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Project Teams Project on Diffusion will be a TEAM project. 34 Students in class so 6 teams of 5 and one Team of 4. Determine members of team and a team.
An Introduction to Programming in Matlab Emily Blumenthal
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
M ATLAB – What Is It ? Name is from matrix laboratory Powerful tool for – Computation and visualization of engineering and science mathematics – Communication.
Release Numbers MATLAB is updated regularly
MATLAB DENC 2533 ECADD LAB 9.
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
Using Script Files and Managing Data
Presentation transcript:

EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook , chapter

Review of Last Week Variables: placeholders for numerical data – equal sign is an assignment operator – naming restrictions (not pi, etc. ) – can be complex valued ( x = i ) Basic math on numbers and variables: + – * / ^

Array Concept The fundamental data unit in Matlab – Rectangular collection of data – All variables are considered to be arrays Data values organized into rows and columns

Size or dimension of an array: – number of rows and columns – written as R by C or R x C where R = number of rows C = number of columns e.g. yield is 3 by 4 test is 1 by 5

– Array size (or value if a scalar or small) is shown in the workspace window

Names for special sizes – scalar: 1 x 1 array 4 or [4] – row vector: 1 x C array [ ] is a 1 x 5 row vector – column vector: R x 1 array is a 3 x 1 column vector

– matrix: R x C array with R > 1, C > 1 If R = C  square matrix – each row must have the same number of entries If R = C = 0  null matrix [ ] (a pair of empty brackets)

Array Construction Direct specification: – Name followed by an equal sign ( = ), just like variables – List values within a pair of brackets ( [ ] ) – Enter data one row at a time left to right, top to bottom order space or comma between the values rows separated by semicolons or the enter key

– For example, to get type b = [ 4,5,3,9; 10,4,66,20; 18,-3,2,0 ] or b = [ 4, 5, 3, 9 10, 4, 66, 20 18, -3, 2, 0 ] enter

– Can use simple math operations as well as numerics as the entries: – Note the common format of all entries in the response (exp(1) = e = , log 10 (100) = 2, = ) – M ATLAB scales the exponent to the largest entry !!

– This scaling is sometimes deceptive: Not really zero Really zero

Concatenation – gluing arrays together if a = [ ] b = [ ] – Attaching left to right – use a comma [ a, b ] – Attaching top to bottom – use a semicolon [ a; b ] semicolon comma

– Note that sizes must match for this to work: if a = [ ] then [ a, b ] = ?? [ a; b ] = ?? – Size needs for concatenation: # of rows the same for side by side (comma) # of columns the same for top to bottom (semicolon)

Evenly spaced vectors – the colon operator first : increment : maximum yields a row vector of equally spaced values – examples: 0 : 2 : 10 [ ] 1 : 5 [ ] 7 : -2 : -3 [ ] 1 : 2 : 8 [ ] – default for increment is 1 Note – does not hit 8!! Recall Assignment 1, #10 xv = –3:0.1:3;

Addressing We indicate a particular element within an array by it’s row/column position: – use parentheses after the array name – e.g. yield(2,4)

Used to read a value from an array (right hand side of = )

How about more than one entry? Can specify a rectangular sub-array – again, use parenthesis after the array name – list desired rows, comma, desired columns as separate vectors, typically in brackets – e.g. yield([1 2],[3 4])

Examples sample(3,2) sample (1,2:4) sample (3,[4,3,2,1]) sample ([1,3],[2,4])

Used to read a sub-array ( rhs of =) Note – scalar row choice does not need brackets!

Avoid some common addressing errors:

Rules of the road for arrays: – Symbols to use: brackets to glue elements together to make an array (left to right or top to bottom) comma (or space) and semicolon (or enter) for separating column/row elements parentheses after the array name for addressing – Be careful to match array sizes – Remember – rows first, then columns in addressing

Scripts – Simple Programs So far, commands have been typed in the command window: – Executed by pressing “enter” – Edited using the arrow keys or the history window

Script (m-file) Concept A file containing Matlab commands – Can be re-executed – Is easily changed/modified or ed to someone Commands are executed one by one, sequentially – File is executed by typing its name (without.m) – Results appear in the command window (or use ; ) Can be created using any text editor –.m extension – Listed in Current Directory window

Matlab’s Built-in, Color Editor: – Can create a new file or open an existing M file (icons or click on file name) – Color used to aid in file creation (command types, typos, etc.)

– typical Windows menu – line numbers – “run” button or F5 – debug capability – comment lines – note use of semicolons – note use of colors

How scripts can get data: – From arrays in the current workspace – From arrays defined in the script – Using the “input” command: Numeric: x = input(' how many? ') String: x = input(' name? ', 's')

How scripts can show data: – Command of the array name – Using the display command: Existing array (a single array only – if necessary, use [ ] !!) disp(x) or disp([x y]) Text disp(' The task is done ')

Example : Note that disp shortens the resulting output by dropping the array name and removing blank lines

Other useful script commands: – clc – clears the command window – pause – stops operation and waits for a key press – pause(n) – stops operation and waits for n seconds

Sample Scripts

Audio Arrays (not in the book!) MatLab can interface to microphones and speakers through the computer’s sound card (sampled and digitized) ….e.g. “maple”

– Matlab represents sounds using arrays (actually, as column vectors) – Equipment needed: Microphone Speakers and software: 2 Matlab scripts: init_sound.m sound_in.m Headsets available in ECC Files available on the egr106 website; save them in your “work” directory

– How to use them: Connect headset (is it muted?) Type init_sound at the command prompt (only needed once per session) Type sound_in at the command prompt to record 1 second of sound (waits for your input) generates array named “data” Type sound(data,10000) at the prompt to play array “data” – Can plot “data”, manipulate “data” before replaying, etc.

init_sound – sets up M ATLAB to sound card interface sound_in – prompts you to speak and records one second of input sound(.,.) – a regular MatLab function What are these tools?

Time Sampling – Speech

Whistling a Scale

Normal versus too loud an input