Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to MATLAB 7 for Engineers

Similar presentations


Presentation on theme: "Introduction to MATLAB 7 for Engineers"— Presentation transcript:

1 Introduction to MATLAB 7 for Engineers
بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept of Electrical & Computer Engineering Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers

2 Course Syllabus Instructor: Eng. Ghassan R. Alnwaimi
Instructor: Eng. Ghassan R. Alnwaimi Web Site: Office Room: 317B, Tel Ext Office Hours: Saturday to Wednesday : p.m. Textbook: William J. Palm III, Introduction to MatLab 7 for Engineers, McGraw-Hill International Edition, 2005 King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

3 The Default MATLAB Desktop
Chapter 1 The Default MATLAB Desktop King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

4 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-and right-arrow keys the Back space key, and the Delete key. Press the Enter key to execute the command. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

5 Example: >> 8/10 ans= 0.8000 >> 5*ans 4 >> r=8/10
King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

6 Scalar Arithmetic Operations
Symbol Operation MATLAB form ^ exponentiation: 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 King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

7 Order of Precedence 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. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

8 Examples >> 8 + 3*5 ans= 23 >> 8 + (3*5) >>(8 + 3)*5
55 >>4^ /4*2 >>4^ /(4*2) 3 King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

9 Examples >> 3*4^2 + 5 ans= 53 >>(3*4)^2 + 5 149
>>27^(1/3) + 32^(0.2) 5 >>27^(1/3) + 32^0.2 >>27^1/3 + 32^0.2 11 King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

10 The Assignment Operator =
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. In algebra we can write x + 2 = 20, but in MATLAB 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. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

11 Commands for managing the work session
Command  Description clc Clears the Command window. clear Removes all variables from memory. clear v1 v Removes the variables v1and v2from memory. exist (‘var’) Determines if a file or variable exists having the name ‘var’. Quit Stops MATLAB. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

12 Commands for managing the work session
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. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

13 Special Variables and Constants
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 π. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

14 Complex Number Operations
The number c1= 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. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

15 Numeric Display Formats
Command Description and Example format short Four decimal digits (the default); format long digits; format short e Five digits (four decimals) plus exponent; e+03. format long e digits (15 decimals) plus exponent; e–04. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

16 Arrays The numbers 0, 0.1, 0.2, …, 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 u101 times. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

17 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 King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

18 Polynomial Roots To find the roots of x3–7x2+ 40x–34 = 0, the session is >>a = [1,-7,40,-34]; >>roots(a) ans= i i 1.0000 The roots are x= 1 and x= 3 ± 5i. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

19 Some Commonly Used Mathematical
Function MATLAB syntax1 e exp(x) √x sqrt(x) lnx log(x) log10x log10(x) cosx cos(x) Sin x sin(x) tan x tan(x) cos x acos(x) sin x asin(x) tan x atan(x) x -1 -1 -1 King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

20 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 problem1if 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. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

21 System, Directory, and File Commands
Command Description addpath dirname Adds the directory dirnameto 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. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi

22 System, Directory, and File Commands
Command Description Pwd Displays the current directory. Rmpath dirname Removes the directory dirnamefrom 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. King Abdulaziz University EE Eng. Ghassan R. Alnwaimi


Download ppt "Introduction to MATLAB 7 for Engineers"

Similar presentations


Ads by Google