Chp1 MATLAB OverView: Part-1

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

1 A B C
5.1 Rules for Exponents Review of Bases and Exponents Zero Exponents
Variations of the Turing Machine
ALAK ROY. Assistant Professor Dept. of CSE NIT Agartala
AP STUDY SESSION 2.
1
David Burdett May 11, 2004 Package Binding for WS CDL.
Create an Application Title 1Y - Youth Chapter 5.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Add Governors Discretionary (1G) Grants Chapter 6.
CALENDAR.
The 5S numbers game..
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Windows XP Project An Introduction to Microsoft Windows XP and Office 2003.
1.
Break Time Remaining 10:00.
The basics for simulations
Factoring Quadratics — ax² + bx + c Topic
Turing Machines.
PP Test Review Sections 6-1 to 6-6
Chapter 10: Applications of Arrays and the class vector
Digital Lessons on Factoring
MM4A6c: Apply the law of sines and the law of cosines.
Briana B. Morrison Adapted from William Collins
Chapter 15 Complex Numbers
Bellwork Do the following problem on a ½ sheet of paper and turn in.
INTRODUCTION Lesson 1 – Microsoft Word Word Basics
Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1 Section 5.5 Dividing Polynomials Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Chapter 1: Expressions, Equations, & Inequalities
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Before Between After.
Slide R - 1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Prentice Hall Active Learning Lecture Slides For use with Classroom Response.
12 October, 2014 St Joseph's College ADVANCED HIGHER REVISION 1 ADVANCED HIGHER MATHS REVISION AND FORMULAE UNIT 2.
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Types of selection structures
Converting a Fraction to %
Clock will move after 1 minute
Chapter 11 Creating Framed Layouts Principles of Web Design, 4 th Edition.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
1.step PMIT start + initial project data input Concept Concept.
9. Two Functions of Two Random Variables
A Data Warehouse Mining Tool Stephen Turner Chris Frala
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Introduction to MATLAB 7 for Engineers
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
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.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
BASICS OF MATLAB Engr.Mian Shahzad Iqbal Lecturer,Telecom Department University of Engineering & Technology Taxila.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
ECE 1304 Introduction to Electrical and Computer Engineering
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Presentation transcript:

Chp1 MATLAB OverView: Part-1 Engr/Math/Physics 25 Chp1 MATLAB OverView: Part-1 Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

Learning Goals Turn On MATLAB and use as a calculator Create Basic Cartesian Plots Write and Save simple “Script” Program-files Execute Conditional Statements IF, THEN, ELSE, >, <, >=, etc. Execute Loop Statements FOR & WHILE

MATLAB Environment TWO Interaction Modes INTERACTIVE Type in the COMMAND WINDOW Often Called a Command-Window “Session” Interaction is NOT Saved to Disk Commands (NOT results) Stored in “Command History” Buffer Window STORED → Two Types SCRIPT Files FUNCTION Files

MATLAB Command Window Typing Occurs Here

Example Cmd Window Session >> %Use MATLAB As Calculator >> 17*19 ans = 323 >> 77/19 -4.3 -0.2474 >> 64^(1/3) + 32^0.2 6 >> (7+11)*2.5 ans = 45 >> L = 14.4 L = 14.4000 >> W = 13.3 W = 13.3000 >> Area = L*W Area = 191.5200 Time For Live Demo Demo left division shown 7\3 = 3/7

Script/Function File Editor

Script & Function Files (m-files) SCRIPTS and FUNCTIONS in MATLAB are stored in text files that end with the extension “.m” These files are called m-files SCRIPTS (a.k.a. “programs”) Scripts files are useful for automating tasks that may need to be repeated. They have no input/output parameters They can (but probably shouldn’t) share variables with the command workspace

Script & Function Files (m-files) SCRIPTS (cont.) Scripts are sequences of interactive statements stored in a file i.e., They look liked Stored versions of Command Window Sessions FUNCTIONS (a.k.a. “subroutines”) Function m-files are MATLAB subprograms analogous to FORTRAN Subroutines, or C functions They communicate with the command window and other functions via a list of INPUT and OUTPUT PARAMETERS or ARGUMENTS

Script & Function Files (m-files) FUNCTIONS (cont.) Functions COMMUNICATE with the COMMAND WINDOW and other m-files via a list of input and output variables LOCAL variables are variables defined INSIDE the function They only can be used inside the function in which they reside. The number of output parameters used when a function is called must match the number of outputs that the function is expected to return Good Example from ENGR43 = Rectab.m & MagPh.m

Entering Commands & Expressions MATLAB retains your previous keystrokes. Use the up-arrow (↑) key to scroll back through the commands. Press the key (↑) once to see the previous entry, and so on. Use the down-arrow (↓) key to scroll forward. Edit a line using the left (←) & right (→) arrow keys the Backspace key, and the Delete key. Press the Enter key to execute the command

Arithmetic Scalar Operations Symbol Operation MATLAB ^ exponentiation: ab a^b * multiplication: ab a*b / right division: a/b a/b \ left division: b/a a\b + addition: a + b a + b - subtraction: a - b a - b DEMO: 21/7 = 3 = 7\21 LEFT-Division A\b read from Right-to-Left as: “b divided by A”

Math Op Precedence (PEMDAS) Precedence Operation First Parentheses, evaluated starting with the innermost pair. Second Exponentiation, evaluated from left to right. Third Multiplication and Division with EQUAL precedence, evaluated from left to right. Fourth Addition and Subtraction with EQUAL precedence, evaluated from left to right. Demo 19^2/3 is NOT the same as 19^(2/30

Precedence Examples >> 8+3*5 ans = 23 >> 8 + (3*5) >>(8 + 3)*5 55 >> 4^2-12-8/4*2 ans = >> 4^2-12-8/(4*2) 3 4 1

Precedence Examples cont. >> 3*4^2 + 5 ans = 53 >>(3*4)^2 + 5 149 >>27^(1/3) + 32^0.2 ans = 5 >>27^1/3 + 32^0.2 11 48 3 144 9

“=“ → Assignment Operator Typing x = 3 ASSIGNS the value 3 to the variable x. We can then type x = x + 2. This assigns the value 3 + 2 = 5 to x. But in algebra this implies that 0 = 2. In algebra we can write x+2 = 20, but in MATLAB we cannot. In MATLAB the LEFT side of the = operator MUST be a SINGLE variable. The Right side must be a computable value

Work Session Commands Command Description clc Clears the Command window clear Removes all variables from memory clear v1 v2 Removes the variables v1 and v2 from memory exist(‘var’) Determines if a file or variable exists having the name ‘var’ quit Stops MATLAB

Work Session Commands cont.1 Description who Lists the variables currently in memory whos Lists the current variables and sizes, and indicates if they have imaginary parts. : (Colon) Generates an array having regularly spaced elements , (Comma) Separates elements of an array ; (Semicolon) Suppresses screen printing; also denotes a new row in an array … (Ellipsis) Continues a line

whos on First???

Special VARS & const’s Command Description ans Temporary variable containing the most recent answer eps Specifies the accuracy of floating point precision i,j The imaginary unit (-1) Inf Infinity (unbounded magnitude) NaN Indicates an undefined numerical result; a.k.a., Not a Number pi The number pi (3.14159...) DEMO Inf: undef = 37/0 => undef = Inf NaN returns the IEEE arithmetic representation for Not-a-Number (NaN). These result from operations which have undefined numerical results. NaN returns the IEEE arithmetic representation for Not-a-Number (NaN). These result from operations which have undefined numerical results;. e.g., try Q = 0/0

The Complex Plane Im (i or j) Re

Complex-Number Operations The number c1 = 1 – 2i is entered as: c1 = 1­2i or c1 = 1-2j An Asterisk is NOT needed between i or j and a NUMBER, although it is required with a VARIABLE, such as c2 = 5 - i*c1. Be careful. The expressions y = 7/2*i and x = 7/2j give two DIFFERENT results: y = (7/2)i = 3.5i and x = 7/(2j) = –3.5j

Complex Arithmetic >> Im_Pwr = Z1^3.84 Im_Pwr = -1.6858e+004 -2.5886e+004i >> e_to_Z = exp(Z2) e_to_Z = 6.8518e+006 -2.3163e+007i >> ln_Z = log(Z1) ln_Z = 2.6922 + 1.0769i >> Log_Z = log10(Z2) Log_Z = 1.2485 + 0.1242i

Special VARS & const’s Command Description format short Four decimal digits (the default); 13.6745 format long 16 digits; 17.27484029463547 format short e Five digits (four decimals) plus exponent; 6.3792e+03 format long e 16 digits (15 decimals) plus exponent; 6.379243784781294e–04

Discrete Math Funtions Command Description factor(n) Returns a row vector containing the prime factors of n. gcd(m,n) Finds the Greatest Common Divisor/Factor of m & n lcm(m,n) Finds the Least Common Multiple for m & n factorial(n) Returns the factorial of n; i.e., returns n! = 1*2*3…(n-2)*(n-1)*n primes(n) Finds all prime numbers less than n isprime(n) Determines if n is a prime number Demo by: >> isprime(97) ans = 1 * >> isprime(98) ans = 0 >> factor(98) ans = 2 7 7 * >> gcf = gcd(51,119) gcf = 17

Discrete Math Examples factor777 = factor(777) factor777 = 3 7 37 GCF = gcd(1001, 1105) GCF = 13 F7 = factorial(7) F7 = 5040 P93 = primes(93) P93 = Columns 1 through 12 2 3 5 7 11 13 17 19 23 29 31 37 Columns 13 through 24 41 43 47 53 59 61 67 71 73 79 83 89

Arrays An ARRAY is an ORDERED SET of Numbers of with n DIMENSIONS A regular Number (a SCALAR) is an Array of Dimension ZERO a VECTOR is a 1-Dim Array a MATRIX is an ARRAY of Dim  2 with special properties Graphics from SemaTech site http://www.itl.nist.gov/div898/handbook/pmc/section5/pmc53.htm Or http://www.itl.nist.gov/div898/handbook/index.htm

Arrays in MATLAB The numbers 0, 0.1, 0.2, …, 10 can be assigned to the array variable u by typing u = [0:0.1:10] To compute w = 5 sin u for u = 0, 0.1, 0.2, 0.3, 0.4,…, 10, the command session is; >>u = [0:0.1:10]; >>w = 5*sin(u); The single line, w = 5*sin(u), computed the formula, w = 5 sin(u), 101 times.

Array Index >>u(7) ans = 0.6000 >>w(7) 2.8232 Use the LENGTH function to determine how many values are in an array. >>m = length(w) m = 101

Polynomial Roots MATLAB has a Way-Cool Polynomial Root Finder Find the roots of x3 − 7x2 + 40x − 34 = 0 >>a = [1,-7,40,-34]; >>roots(a) ans = 3.0000 + 5.000i 3.0000 - 5.000i 1.0000 The roots are x = 1 and x = 3 ± 5i

5th Order Polynomial Find the roots of the 5th Order function >> roots(r5) ans = 3.0000 + 2.0000i 3.0000 - 2.0000i 1.0000 + 1.0000i 1.0000 - 1.0000i 1.0000 The roots of g(y) y1,2 = 3 ± 2j y3,4 = 1 ± j y5 = 1

Common Math Functions Fcn MATLAB ex exp(x) sin x sin(x) √x sqrt(x) tan x tan(x) ln x log(x) cos-1 x acos(x) log10 x log10(x) sin-1 x asin(x) cos x cos(x) tan-1 x atan(x) Note that MATLAB Trig functions Operate on RADIANS Convert using Ratio: -rads per 180° For trig argument in DEGREEs use MATLAB fcns: cosd, sind, tand

The “d” Trig Comands for Degrees >> T1 = sind(77) T1 = 0.9744 >> T2 = cosd(19) T2 = 0.9455 >> T3 = tand(53) T3 = 1.3270 >> T4 = asind(.497) T4 = 29.8017 >> T5 = acosd(0.629) T5 = 51.0236 >> T6 = atand(1.73) T6 = 59.9706

Printing From Command Window - 1 Text to Print Note: MATLAB “Comments” Start with the “%” Sign

Printing From Command Window - 2 SELECTText to Print

Printing From Command Window - 3 Send to printer from Print Dialog Box Caveat In a COMMAND WINDOW session once you Hit Enter () you can NOT Go back to Edit the Text Can Save your command sequence as an m-file SCRIPT

Alternative Cmd Window Printing Perform MATLAB Operation Select Desired Text COPY text to the Windows Paste Buffer Open Text application MSWord, WordPad, NotePad, etc. PASTE the MATLAB Text Into the Text Processor Print from the Text Processor as Usual

DIARY Function to Record Cmnds Keeping a Session Log → The diary Function The diary function creates a copy of your session in MATLAB on a disk file, including keyboard input and system responses, but excluding graphics. You can view and edit the resulting text file using any text editor, such as the MATLAB Editor. To create a file on your disk called sept23.out that contains all the functions you enter, as well as output from MATLAB, enter diary('sept23.out') To stop recording the session, use diary('off') To view the file, run edit('sept23.out')

Command Execution Hierarchy When you type problem1 MATLAB first checks to see if problem1 is a variable and if so, displays its value. If not, MATLAB then checks to see if problem1 is one of its own commands, and executes it if it is. If not, MATLAB then looks in the current directory for a file named problem1.m and executes problem1 if it finds it. If not, MATLAB then searches the directories in its search path, in order, for problem1.m and then executes it if found.

System, Directory, File Cmnds Command Description addpath dirname Adds the directory dirname to the search path. cd dirname Changes the current directory to dirname dir Lists all files in the current directory dir dirname Lists all the files in the directory dirname path Displays the MATLAB search path pathtool Starts the Set Path tool HINT: Consider putting ALL your m-files in ONE Folder/Directory

Plotting with MATLAB Plot over 573° 10 rads = 573 deg

MATLAB Plotting Commands Description plot(x,y) Generates a plot of the array y versus the array x on rectilinear axes title(’text’) Puts text in a title at the top of the plot xlabel(’text’) Adds a text label to the horizontal axis (the abscissa). ylabel(’text’) Adds a text label to the vertical axis (the ordinate). grid Puts grid lines on the plot gtext(’text’) Enables placement of text with the mouse

DeskTop Recovery to UnScramble the DeskTop

DeskTop “Recovery”

Example Problem 1-21 Time For Live Demo Plot This Function Where T  Temperature (°C) t  time (minutes) For: 1  t  3

Tutorial on HomeWork Construction Next Time A VERY Important Meeting All Done for Today Tutorial on HomeWork Construction Next Time From 2E: P1.30 Tutorial Next Meeting A VERY Important Meeting

Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Appendix Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

Example Demo Session >> %Use MATLAB As Calculator >> 17*19 ans = 323 >> 77/19 -4.3 -0.2474 >> 64^(1/3) + 32^0.2 6 >> (7+11)*2.5 ans = 45 >> L = 14.4 L = 14.4000 >> W = 13.3 W = 13.3000 >> Area = L*W Area = 191.5200

Prob 1-21 Command Script From the Command Window >> T = 6*log(t) - 7*exp(0.2*t); >> plot(t,T), xlabel('time (min)'),ylabel('Temperature (°C)'), title('Problem 1-21'), grid

Prob 1-22 Plot

System, Directory, File Cmnds Command Description pwd Displays the current directory cd dirname Changes the current directory to dirname rmpath dirname Removes the directory dirname from the search path. what Lists the MATLAB-specific files found in the current working directory. Most data files and other non-MATLAB files are not listed. Use dir to get a list of all files what dirname Lists the MATLAB-specific files in directory dirname