Matlab Programming for Engineers Introduction to Matlab Basics

Matlab Programming for Engineers Introduction to Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Dr. Bashir NOURI

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Objectives: § Variables and Arrays § Initializing Variables in MATLAB § Multidimensional Arrays § Subarrays § Special Values § Displaying Output Data § Data Files § Scalar and Array Operations § Hierarchy of Operations § Built-in MATLAB Functions § Introduction to Plotting § Debugging MATLAB Programs Mechanical Engineering Department 2

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Variables and Arrays § § § The fundamental unit of data in any Matlab program is the Array Any element of the array can be retrieved by its indexes Arrays could be matrices or vectors (row or column) a(3, 2) Matlab variable is the region of memory containing an array Matlab variable letter + (letter, number, _) Variable type: 1. double precession (real, complex) 2. Character 'How are you? ' Mechanical Engineering Department 3

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Creating and initializing variables in Matlab 1. Initializing variables in assignment statements var = expression; var = 40 i; var 2 = var/5; Mechanical Engineering Department var = [1 2 3 4]; 4

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Creating and initializing variables in Matlab 1. Initializing variables in assignment statements a = [0 1+7]; a = [0 8] b = [ a(2) 7 a]; b = [8 7 0 8] c(2, 3) = 5; c= 0 0 0 5 Mechanical Engineering Department 5

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Creating and initializing variables in Matlab 2. Initializing variables with Shortcut Expression colon operator (: ) first : incr : last Ex: x = 1: 2: 10; x = [1 3 5 7 9] angles = (0. 01: 1)*pi; f = [1: 4]'; if no incr is used default incr=1 (') is the transpose operator. g = [1: 4]; h = [g' g']; Mechanical Engineering Department 6

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Creating and initializing variables in Matlab 3. Initializing variables with Built-in Function Mechanical Engineering Department 7

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Creating and initializing variables in Matlab 4. Initializing variables with Keyboard Input my_val = input('Enter an input value: '); v 1 = input('Enter data: '); v 2 = input('Enter data: ', 's'); Mechanical Engineering Department 8

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Multidimensional Arrays Mechanical Engineering Department 9

CH 2: MATLAB BASICS MATLAB PROGRAMMING FOR ENGINEERS Multidimensional Arrays Array as introduced 2. Accessing this array with one dimension index Mechanical Engineering Department How the array is stored in computer memory 1. Storing Multidimensional Array in Memory 10

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Subarrays Mechanical Engineering Department 11

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Subarrays 1. The end Function (highest value of a given subscript) Mechanical Engineering Department 12

MATLAB PROGRAMMING FOR ENGINEERS Subarrays 3. Assigning a scalar to a subarray Mechanical Engineering Department CH 2: MATLAB BASICS 2. Left-hand side Subarrays assignment statement 13

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Special Values Mechanical Engineering Department 14

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Displaying Output Data Default Matlab Format == Four Digits After the Decimal Point Mechanical Engineering Department 15

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Displaying Output Data (NUMBER FORMAT) Mechanical Engineering Department 16

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Displaying Output Data (disp and fprintf functions) Mechanical Engineering Department 17

CH 2: MATLAB BASICS MATLAB PROGRAMMING FOR ENGINEERS Data Files (save) save filename var 1 var 2 var 3 -ascii Mechanical Engineering Department *. mat text format (ascii) 18

CH 2: MATLAB BASICS MATLAB PROGRAMMING FOR ENGINEERS Data Files (save) save filename var 1 var 2 var 3 -ascii Mechanical Engineering Department *. mat text format (ascii) 19

CH 2: MATLAB BASICS MATLAB PROGRAMMING FOR ENGINEERS Data Files (load) load filename. mat var 1 var 2 load filename. * (*. mat file format, loads specific variables) (any other format, loads all the file data in the file must have array structure) Mechanical Engineering Department 20

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Scalar and Array Operations Mechanical Engineering Department 21

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Scalar and Array Operations Very important Mechanical Engineering Department 22

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Scalar and Array Operations Matrix left division (ab) Mechanical Engineering Department 23

CH 2: MATLAB BASICS MATLAB PROGRAMMING FOR ENGINEERS Hierarchy of Operations Distance = 0. 5 * acceleration* (time^2) Distance = (0. 5 * acceleration* time)^2 Mechanical Engineering Department Not the same! 24

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Class Work Mechanical Engineering Department 25

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Built-in Matlab Functions 1. Optional results Optional 2. Most of functions accept array inputs Mechanical Engineering Department 26

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Built-in Matlab Functions (Common Functions) Mechanical Engineering Department 27

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Built-in Matlab Functions (Common Functions) Mechanical Engineering Department 28

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Plotting in Matlab Mechanical Engineering Department 29

CH 2: MATLAB BASICS MATLAB PROGRAMMING FOR ENGINEERS Printing a Plot / Exporting a Plot as a Graphical Image >> print <options> <filename> If no filename is specified, plot is printed on the printer >> help print (to see print options) Mechanical Engineering Department 30

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Printing a Plot / Exporting a Plot as a Graphical Image Mechanical Engineering Department 31

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Multiple plots Plot the following functions on the same figure f(x) = sin(2 x) and its first derivative over x = 0: pi/100: 2*pi Mechanical Engineering Department 32

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Multiple plots Plot the following functions on the same figure f(x) = sin(2 x) and its first derivative over x = 0: pi/100: 2*pi Mechanical Engineering Department 33

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Line Color, Line Style, Marker Style, and Legends Mechanical Engineering Department 34

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Line Color, Line Style, Marker Style, and Legends Line Color, Line Style, and Marker Style: Mechanical Engineering Department 35

CH 2: MATLAB BASICS MATLAB PROGRAMMING FOR ENGINEERS Line Color, Line Style, Marker Style, and Legends: >> legend off (removes the legend from the plot) >> legend('string 1' , … , 'location', 'value from table') NW NL NC NR NE TW TL TC TR TE MW ML MC MR ME BW BL BC BR BE SW SL SC SR SE Limits of plot axes Be aware of the version of Matlab you are using, see help legend Mechanical Engineering Department 36

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Logarithmic Scale >> plot >> semilogx >> semilogy >> loglog Mechanical Engineering Department 37

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Example Mechanical Engineering Department 38

MATLAB PROGRAMMING FOR ENGINEERS CH 2: MATLAB BASICS Home Work Solve the following problems 2. [9, 10 , 11, 16] Mechanical Engineering Department 39
- Slides: 39