Computer Applications Lab Computer Applications Lab Lab 1 Lab 1 Introduction to Matlab Introduction to Matlab Chapter 1 Chapter 1 Sections 1,2,3,5 Sections 1,2,3,5 Dr. Iyad Jafar Adapted from the publisher slides
What is Matlab ? What is Matlab ? • A Computer programming language and software environment to manage data interactively • Originally developed in 1970s for applications involving matrices, linear algebra and numerical analysis analysis • Maintained and sold by the MathWorks, Inc. • Functionality: manage variables, import/export data, calculations, generate plots, and more …… • T oolboxes such as image processing, statistics, control, financial analysis, and more … 2
Matlab Desktop Matlab Desktop Menus Toolbar Current Directory Workspace Command History Command Window 3
Matlab as interactive calculator Matlab as interactive calculator >> 8/10 ans= 0.8000 >> 5*ans NOTES ans= • ans is a built-in special variable to store the result of the last computation. Can be reused by result of the last computation. Can be reused by 4 4 typing ans >> r=8/10 • variables defined by the assignment operator ‘=‘ r = are stored in memory and can be used again using 0.8000 their names >> r r = 0.8000 >> s=20*r s = 16 4
Matlab as interactive calculator Matlab as interactive calculator • 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, • 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. 5
Scalar Arithmetic Operations Scalar Arithmetic Operations Symbol Operation Mathematical Matlab Syntax Syntax ^ Exponentiation a b a^b * Multiplication ab a*b / Forward Division a/b a/b \ Backward Division a\b a\b + Addition a+b a+b - Subtraction a-b a-b 6
Order of Precedence of Order of Precedence of Arithmetic Operators Arithmetic Operators Parentheses Note: if Exponentiation Exponentiation precedence is precedence is equal, evaluation is performed from left to right. Multiplication & division Addition and subtraction 7
Examples of Precedence 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= 3 8
Examples of Precedence Examples of Precedence (continued) (continued) >> 3*4^2 + 5 ans= 53 >> (3*4)^2 + 5 ans= 149 149 >> 27^(1/3) + 32^(0.2) ans= 5 >> 27^(1/3) + 32^0.2 ans= 5 >> 27^1/3 + 32^0.2 ans= 11 9
The Assignment Operator ‘=’ 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 3 + 2 = 5 to x. But in algebra this implies that 0 = 2. that 0 = 2. • In algebra we can write x + 2 = 20, but in Matlab we cannot. • In Matlab the left side of the assignment operator ‘=‘ must be a single variable. • The right side must be a computable value. 10
Variables in Matlab Variables in Matlab � No declaration or dimension statements are required to define variables in Matlab. � To define a new variable, simply write the variable name followed by the assignment operator and the value to be stored in the variable. num_students = 25 num_students = 25 � If the variable already exists, Matlab changes its contents and, if necessary, allocates new storage. � To view the value of the variable, type its name on the command prompt and hit Enter. � Variable names consist of a letter, followed by any number of letters, digits, or underscores. 11
Special Variables and Constants Special Variables and Constants Variable Description Temporary variable containing ans the most recent answer i and j The imaginary unit √ − 1 inf Infinity NaN Indicates an undefined numerical result pi The number π 12
Commonly Used Mathematical Commonly Used Mathematical Functions Functions Function Matlab Syntax e x exp(x) √x sqrt(x) ln x log(x) log 10 x log10(x) cos x cos(x) sin x sin x sin(x) sin(x) tan x tan(x) cos − 1 x acos(x) sin − 1 x asin(x) tan − 1 x atan(x) |x| abs(x) NOTES • Trigonometric functions in Matlab use radian measure • cos 2 (x) is written (cos(x))^2 in Matlab 13
Complex Number Operations 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 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 x = 7/(2i) = –3.5i 14
Commands for managing the Commands for managing the work session work session Command Operation clc Clears the command window clear Clears all the variables from the workspace clear v1 v2 Clears variables v1 and v2 only exist(‘var’) exist(‘var’) Check if the variable or function exist Check if the variable or function exist quit Exits Matlab session who List the names of the variables defined in the work space whos List the names of the variables defined in the work space and their details Semicolon (;) Suppresses the display of results in the command window Ellipsis (…) Line continuation 15
Numeric Display Formats Numeric Display Formats Format Operation Example Four decimal digits (the default 13.6745 short format); long long 16 decimal digits; 16 decimal digits; 17.27484029463547 17.27484029463547 Five digits (four decimals) plus short e 6.3792e+03 exponent 16 digits (15 decimals) plus long e 6.379243784781294e–04 exponent; 16
Command Resolving in Matlab Command Resolving in Matlab What happens when you type problem1 in the command prompt ? 1. Matlab first checks to see if problem1 is a variable and if so, displays its value. displays its value. 2. If not, Matlabthen 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 .mand executes problem1if 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. 17
The Help Navigator The Help Navigator � Available through the help menu � Contents ◦ Contents: contents listing tab ◦ Index : a global index tab, ◦ Index : a global index tab, ◦ Search: a search tab having a find function and full text search features ◦ Demos: a bookmarking tab to start built-in demonstrations. 18
Help Functions Help Functions • help funcname : Displays in the Command window a description of the specified function funcname. • lookfor topic : Displays in the Command window a brief description for all functions window a brief description for all functions whose description includes the specified key word topic. • doc funcname : Opens the Help Browser to the reference page for the specified function funcname, providing a description, additional remarks, and examples. 19
Recommend
More recommend