Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

Similar presentations


Presentation on theme: "Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data."— Presentation transcript:

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

2 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 2 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

3 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 3 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)  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?' a(3,2)

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

5 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 5 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 0 0 5

6 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 6 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: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'];

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

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

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

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

11 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 11 Subarrays

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

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

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

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

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

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

18 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 18 Data Files (save) save filename var1 var2 var3 *.mat save filename var1 var2 var3 -ascii text format (ascii)

19 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 19 Data Files (save) save filename var1 var2 var3 *.mat save filename var1 var2 var3 -ascii text format (ascii)

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

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

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

23 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 23 Scalar and Array Operations Matrix left division (a\b)

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

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

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

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

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

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

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

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

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

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

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

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

36 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 36 Line Color, Line Style, Marker Style, and Legends Legends: >> legend off (removes the legend from the plot) >> legend('string1', 'string1', …,'location', 'value from table') Be aware of the version of Matlab you are using, see help legend NWNLNCNRNE TWTLTCTRTE MWMLMCMRME BWBLBCBRBE SWSLSCSRSE Limits of plot axes

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

38 CH2: MATLAB BASICS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 38 Example

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


Download ppt "Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data."

Similar presentations


Ads by Google