Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Basic Elements of Fortran Programming.
Advertisements

Welcome to EGR 106 Foundations of Engineering II Course information Today’s specific topics: – Computation and algorithms – M ATLAB Basics Demonstrations.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Introduction to MATLAB
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Introduction to MATLAB 7
Introduction to MATLAB 7 for Engineers
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
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 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.
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.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Matlab Review Exam 2 – Oct 28, Exam Reminders Open book. Only Matlab book, no notes or loose papers in book. Open book. Only Matlab book, no notes.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
1 Chapter 1 MATLAB Primer This introductory chapter is relatively short and has as its main objective the introduction of MATLAB ® to the reader. This.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MATLAB for Engineers Chapter 1 An Overview Of MATLAB.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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 – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Chapter 1 An Overview Of MATLAB. The default MATLAB Desktop. Figure 1.1–1 1-2 More? See pages 6-7.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
A Brief introduction to MatLAB ($50 - $99 for students)
Chapter 1 An Overview Of MATLAB. The default MATLAB Desktop. Figure 1.1–1 1-2 More? See pages 6-7.
26 - 3/20/2000AME 150L1 M ATLAB ® /20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Introduction to Programming with RAPTOR
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ENGR-25_MATLAB_OverView-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Matlab. Textbooks Required:, by Palm (3 rd edition) Required: Introduction to MATLAB for Engineers, by Palm (3 rd edition)
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Matlab for Engineers Matlab Environment Chapter 2.
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.
Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Copyright © The McGraw-Hill Companies, Inc. This work is only for non-profit use by instructors in courses for which this textbook has been adopted.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 MATLAB 입문 An Overview of MATLAB. An Overview of MATLAB 2 Starting Matlab The default MATLAB Desktop.
מטלב וניתוח נתונים ממערכות עצביות היכרות עם MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to MATLAB for Engineers, Third Edition
Basic operations in Matlab
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Introduction to MATLAB 7
Experiment No. (1) - an introduction to MATLAB
MatLab Program Used to Calculate Interactive
Presentation transcript:

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm III Chapter 1 An Overview Of MATLAB PowerPoint to accompany

What you will learn Identify the built in function, syntax and notation in MATLAB Construct script files for execution of mathematical calculations Produce a program by using loops and conditional statements.

Assessment Test (s)20 Assignment(s) /Quiz (s)30 Final Exam50

Reference 1. Palm, W.J., Introduction to Matlab 7 for Engineers, McGraw-Hill, New York. 2. Hanselman et al., Mastering MATLAB 7, Pearson Education Inc, New Jersey. 3. Amos, G., MATLAB: An Introduction with Application, 2nd Ed, John Wiley Inc, USA. 4. Moore, H., MATLAB for Engineers, 2nd Ed., Prentice Hall, New Jersey.

The default MATLAB Desktop. Figure 1.1–1 1-2 More? See pages 6-7.

Entering Commands and Expressions MATLAB retains your previous keystrokes. Use the up-arrow key to scroll back 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- and right-arrow keys the Backspace key, and the Delete key. Press the Enter key to execute the command. 1-3

Scalar Arithmetic Operations Table 1.1–1 1-5 Symbol Operation MATLAB form ^ exponentiation: a b 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

An Example Session >> 8/10 ans = >> 5*ans ans = 4 >> r=8/10 r = >> r r = >> s=20*r s = More? See pages 8-9.

Order of Precedence Table 1.1–2 1-6 Precedence Operation FirstParentheses, evaluated starting with the innermost pair. SecondExponentiation, evaluated from left to right. ThirdMultiplication and division with equal precedence, evaluated from left to right. FourthAddition and subtraction with equal precedence, evaluated from left to right.

Examples of Precedence >> 8 + 3*5 ans = 23 >> 8 + (3*5) ans = 23 >>(8 + 3)*5 ans = 55 >>4^2­12­ 8/4*2 ans = 0 >>4^2­12­ 8/(4*2) ans = (continued …)

Examples of Precedence (continued) >> 3*4^2 + 5 ans = 53 >>(3*4)^2 + 5 ans = 149 >>27^(1/3) + 32^(0.2) ans = 5 >>27^(1/3) + 32^0.2 ans = 5 >>27^1/3 + 32^0.2 ans =

The Assignment Operator = Typing x = 3 assigns the value 3 to the variable x. Typing x = 3 assigns the value 3 to the variable x. We can then type x = x + 2. This assigns the value = 5 to x. But in algebra this implies that 0 = 2. We can then type x = x + 2. This assigns the value = 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 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. In MATLAB the left side of the = operator must be a single variable. The right side must be a computable value. The right side must be a computable value. 1-9 More? See pages

Commands for managing the work session Table 1.1– CommandDescription 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. (continued …)

Commands for managing the work session Table 1.1–3 (continued) 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. 1-11More? See pages

Special Variables and Constants Table 1.1– 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. NaN Indicates an undefined numerical result. pi The number .

Special variable and constant Inf= infinity Inf= infinity Example: try typing 5/0=? In Mathlab Example: try typing 5/0=? In Mathlab NaN= undefined numerical result NaN= undefined numerical result Example: try typing 0/0=? Example: try typing 0/0=? Eps= smallest number which when add to 1 will create number greater than 1. Use eps for indicator of accuracy. Eps= smallest number which when add to 1 will create number greater than 1. Use eps for indicator of accuracy. Example: try x=1+eps, y=1, prove by (x>y) and (x y) and (x<y)

Complex Number Operations The number c 1 = 1 – 2i is entered as follows: c1 = 1-­2i. 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/2i give two different results: y = (7/2)i = 3.5i and x = 7/(2i) = –3.5i. 1-13

Test your understanding Given x=-5+9i and y=6-2i. Use mathlab to show that x+y=1+7i, xy=-12+64i and Given x=-5+9i and y=6-2i. Use mathlab to show that x+y=1+7i, xy=-12+64i andx/y= i

Numeric Display Formats Table 1.1– Command Description and Example format short Four decimal digits (the default); format long 16 digits; format short e Five digits (four decimals) plus exponent; e+03. format long e 16 digits (15 decimals) plus exponent; e–04.

Arrays The numbers 0, 0.1, 0.2, until 10 can be assigned to the variable u by typing u = [0:0.1:10]. To compute w = 5 sin u for u = 0, 0.1, 0.2, …, 10, the 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. 1-15

Array Index To find specific value in array >>u(7) ans = >>w(7) ans = Use the length function to determine how many values are in an array. >>m = length(w) m = More? See pages

exercise

Array exercise Use mathlab to determine how many elements are in the array start with cos(0), increment 0.02 until log10(100). Then, use mathlab to determine the 25 th element. Use mathlab to determine how many elements are in the array start with cos(0), increment 0.02 until log10(100). Then, use mathlab to determine the 25 th element. Answer: 51 element and 1.48 Answer: 51 element and 1.48

Polynomial Roots To find the roots of x 3 – 7x x – 34 = 0, the session is >>a = [1,-7,40,-34]; >>roots(a) ans = i i The roots are x = 1 and x = 3 ± 5i. 1-17

Polynomial exercise Use mathlab to find the roots of the polynomial x+6x 2 +x 3. Use mathlab to find the roots of the polynomial x+6x 2 +x 3. Answer: x=-10 and 2 ± 5i Answer: x=-10 and 2 ± 5i

Some Commonly Used Mathematical Functions Table 1.3– Function MATLAB syntax 1 e x exp(x) √x sqrt(x) ln x log(x) log 10 x log10(x) cos x cos(x) sin x sin(x) tan x tan(x) cos  1 x acos(x) sin  1 x asin(x) tan  1 x atan(x) 1 The MATLAB trigonometric functions use radian measure.

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

System, Directory, and File Commands Table 1.3– 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. (continued …)

System, Directory, and File Commands Table 1.3–2 (continued) CommandDescription pwd Displays the current directory. rmpath dirname Removes the directory dirname from the search path. what Lists the MATLAB-specificfiles 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. 1-22

MATLAB plotting commands Table 1.3–3 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). 1-25More? See pages

MATLAB plotting commands Table 1.3–3 (continued) 1-24 CommandDescription [x,y] = ginput(n) Enables the mouse to get n points from a plot, and returns the x and y coordinates in the vectors x and y, which have a length n. grid Puts grid lines on the plot. gtext(’text’) Enables placement of text with the mouse. (continued …)

A graphics window showing a plot. Figure 1.3– X=[0: 0.02: 8] Y=5*sin(x) Plot (x,y) and label x and y- axis

Test you understanding by

Answer: Answer: >> t=[0:5]; >> t=[0:5]; >> s=2*sin(3*t+2)+sqrt(5*t+1); >> s=2*sin(3*t+2)+sqrt(5*t+1); >> plot (t,s),xlabel('x (s)'), ylabel('y(ft/s)'),title('speed vs time') >> plot (t,s),xlabel('x (s)'), ylabel('y(ft/s)'),title('speed vs time')

Solution of Linear Algebraic Equations 6x + 12y + 4z = 70 7x – 2y + 3z = 5 2x + 8y – 9z = 64 >>A = [6,12,4;7,-2,3;2,8,-9]; >>B = [70;5;64]; >>Solution = A\B Solution = The solution is x = 3, y = 5, and z = – More? See page 28.

Try this Solve the following set of equations Solve the following set of equations 6x-4y+8z=112 6x-4y+8z=112 -5x-3y+7z=75 -5x-3y+7z=75 14x+9y-5z=-67 14x+9y-5z=-67 Answer: x=2, y=-5, z=10 Answer: x=2, y=-5, z=10

exercise 3x+y+z+w=24 3x+y+z+w=24 x-3y+7z+w=12 x-3y+7z+w=12 2x+2y-3z+4w=17 2x+2y-3z+4w=17 x+y+z+w=0 x+y+z+w=0

You can perform operations in MATLAB in two ways: 1. In the interactive mode, in which all commands are entered directly in the Command window, or 2.By running a MATLAB program stored in script file. This type of file contains MATLAB commands, so running it is equivalent to typing all the commands—one at a time— at the Command window prompt. You can run the file by typing its name at the Command window prompt More? See pages

COMMENTS The comment symbol may be put anywhere in the line. MATLAB ignores everything to the right of the % symbol. For example, >>% This is a comment. >>x = 2+3 % So is this. x = 5 Note that the portion of the line before the % sign is executed to compute x. 1-28

How to create M-file Select NEW from the FILE menu. Then select BLANK M-file Select NEW from the FILE menu. Then select BLANK M-file In the new M-file, save the M-file to example1. In the new M-file, save the M-file to example1. In the example1, please write down the following: In the example1, please write down the following:

The MATLAB Command window with the Editor/Debugger open. Figure 1.4–1 1-29

Keep in mind when using script files: 1. The name of a script file must begin with a letter, and may include digits and the underscore character, up to 31 characters. 2. Do not give a script file the same name as a variable. 3. Do not give a script file the same name as a MATLAB command or function. You can check to see if a command, function or file name already exists by using the exist command. 1-30

Debugging Script Files Program errors usually fall into one of the following categories. 1. Syntax errors such as omitting a parenthesis or comma, or spelling a command name incorrectly. MATLAB usually detects the more obvious errors and displays a message describing the error and its location. 2. Errors due to an incorrect mathematical procedure, called runtime errors. Their occurrence often depends on the particular input data. A common example is division by zero. 1-31

To locate program errors, try the following: 1. Test your program with a simple version of the problem which can be checked by hand. 2. Display any intermediate calculations by removing semicolons at the end of statements. 3. Use the debugging features of the Editor/Debugger More? See pages

Programming Style 1. Comments section a. The name of the program and any key words in the first line. b. The date created, and the creators' names in the second line. c. The definitions of the variable names for every input and output variable. Include definitions of variables used in the calculations and units of measurement for all input and all output variables! d. The name of every user-defined function called by the program (continued …)

2.Input section Include input data and/or the input functions and comments for documentation. 3.Calculation section 4. Output section This section might contain functions for displaying the output on the screen. Programming Style (continued) 1-34 More? See pages

Input/output commands Table 1.4– CommandDescription disp(A)Displays the contents, but not the name, of the array A. disp(’text’)Displays the text string enclosed within quotes. x = input(’text’)Displays the text in quotes, waits for user input from the keyboard, and stores the value in x. x = input(’text’,’s’)Displays the text in quotes, waits for user input from the keyboard, and stores the input as a string in x.

Example of a Script File Problem: The speed v of a falling object dropped with no initial velocity is given as a function of time t by v = gt. Plot v as a function of t for 0 ≤ t ≤ t f, where t f is the final time entered by the user (continued …)

Example of a Script File (continued) % Program falling_speed.m: % Plots speed of a falling object. % Created on March 1, 2004 by W. Palm % % Input Variable: % tf = final time (in seconds) % % Output Variables: % t = array of times at which speed is % computed (in seconds) % v = array of speeds (meters/second) % (continued …) 1-37

Example of a Script File (continued) % Parameter Value: g = 9.81; % Acceleration in SI units % % Input section: tf = input(’Enter final time in seconds:’); % (continued …) 1-38

Example of a Script File (continued) % Calculation section: dt = tf/500; % Create an array of 501 time values. t = [0:dt:tf]; % Compute speed values. v = g*t; % % Output section: Plot(t,v),xlabel(’t (s)’),ylabel(’v m/s)’) 1-39 More? See pages