390 likes | 426 Views
Matlab Programming for Engineers. 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). Dr. Bashir NOURI. Objectives:.
E N D
Matlab Programming for Engineers Introduction to MatlabMatlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Dr. Bashir NOURI
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
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: • double precession (real, complex) • Character 'How are you?' a(3,2)
Creating and initializing variables in Matlab • Initializing variables in assignment statements var = expression; var = 40i; var2 = var/5; var = [1 2 3 4];
Creating and initializing variables in Matlab • 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
Creating and initializing variables in Matlab • 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'];
Creating and initializing variables in Matlab • Initializing variables with Built-in Function
Creating and initializing variables in Matlab • Initializing variables with Keyboard Input my_val = input('Enter an input value: '); v1 = input('Enter data: '); v2 = input('Enter data: ', 's');
Multidimensional Arrays • Storing Multidimensional Array in Memory • Accessing this array with one dimension index Array as introduced How the array is stored in computer memory
Subarrays • The end Function (highest value of a given subscript)
Subarrays Left-hand side Subarrays assignment statement • Assigning a scalar to a subarray
Displaying Output Data Default Matlab Format == Four Digits After the Decimal Point
Data Files (save) save filename var1 var2 var3 *.mat save filename var1 var2 var3 -ascii text format (ascii)
Data Files (save) save filename var1 var2 var3 *.mat save filename var1 var2 var3 -ascii text format (ascii)
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)
Scalar and Array Operations Very important
Scalar and Array Operations Matrix left division (a\b)
Hierarchy of Operations Not the same! Distance = 0.5 * acceleration* (time^2) Distance = (0.5 * acceleration* time)^2
Built-in Matlab Functions 1 . Optional results 2. Most of functions accept array inputs Optional
Printing a Plot / Exporting a Plot as a Graphical Image >> print <options> <filename> >> help print (to see print options) If no filename is specified, plot is printed on the printer
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
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
Line Color, Line Style, Marker Style, and Legends Line Color, Line Style, and Marker Style:
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 Limits of plot axes
Logarithmic Scale >> plot >> semilogx >> semilogy >> loglog
Home Work Solve the following problems 2. [9, 10 , 11, 16]