2.39k likes | 3.75k Views
MATLAB & Its Applications In Engineering. 李清都 副教授 liqd@cqupt.edu.cn. Lect. 1: Introduction. Contents Covered: Why MATLAB? Simple demos. How to learn MATLAB? Objectives: Establish the basic concepts of MATLAB Understand the advantage and disadvantage of MATLAB. Why MATLAB ?.
E N D
MATLAB & Its Applications In Engineering 李清都 副教授 liqd@cqupt.edu.cn
Lect. 1: Introduction Contents Covered: Why MATLAB? Simple demos. How to learn MATLAB? Objectives: Establish the basic concepts of MATLAB Understand the advantage and disadvantage of MATLAB
Why MATLAB? There are totally 87 students I may arrange 10 times of assignments Each time I will randomly look through 20 students’ work Question: What is the probability that you will never be selected? How high is the risk not to do the assignments?
Why MATLAB? 1-[(87-20)/87]10 = ? I have much more important things to do than to calculate this boring figure… Want to know the result ?! • Calculate by hand • use a calculator • use C language
Why MATLAB? Using C language: main() { float m; m = 1-((107-20)/107)^10; printf(“%f”,m); }
Why MATLAB? Using C language: main() { float m; m = 1-((107-20)/107)^10; printf(“%f”,m); } If only there was just line 3…
You need such a powerful tool Powerful compute ability Easy of use MATLAB
MATLAB:the most popular computer program optimized to perform engineering and scientific calculations.
What can MATLAB do? Mathematical computing Visual results System modeling Graphic user interface
The advantages of MATLAB Easy of use Platform independence Predefined functions Device-Independent Plotting Graphic user interface MATLAB compiler
The disadvantages of MATLAB Slower execute speed Matlab is interpreted language other than compiled language All the variables use the highest precision Higher cost
The MATLAB Environment Command window Work Space Command history
How to learn MATLAB? The most important is to program more and practice more The best teacher is Matlab help, not me, nor any person else Think more your idea search in help find a function programming Good programming practice
Assignment: Install Matlab and run the demo Any question or suggestion, please email to: liqd@cqupt.edu.cn with title: matlab - your name - your question
MATLAB & Its Applications In Engineering 李清都 副教授 liqd@cqupt.edu.cn
Lect. 2: Variables & Arrays • Contents Covered: • basic conceptions of MATLAB variables and arrays • initialize/save/load MATLAB variables and arrays • Objectives: • Firmly grasp the naming rules of MATLAB variables. • Fully grasp the initialize and addressing method of a array. • Memorize the basic special variables, such as pi, i, j, ans, NaN. • Grasp how to save and load MATLAB variables
Arrays • Vectors: 1 dimension • Matrix: 2 or more dimensions A scalar is an array with only one row and one column. All data, even scalars, are arrays.
Variables Naming Rule • must begin with a letter, followed by any combinations of letters, numbers, and the underscore character. C can begin with ‘_’ !!! • Only the first 31 characters are significant. A MATLAB variable is a region of memory containing an array, which is known by a user-specified name.
Special Variables i, j, pi, NaN, ans, eps MATLAB has some predefined variables and users should avoid using them create new variables.
Good Programming Practice • Must using meaningful names • Include a data dictionary in the header of the program • Use lowercase letters to name a variable • Use underscore as separator if the name is long
Initialize the Array There are 3 common ways: • Using assignment statement • Read from a file • From keyboard
Initializing Variables in Assignment Statement • Using assignment statement • Var = expression • Initializing with shortcut expression • Var = first : incr : last • Initializing with build-in functions • Var = zeros(?) • Var = ones(?)
Initializing Variables from file • MATLAB formatted file • load xxx.mat • save file_name var1, var2, var3… • ASCII file • load file_name.xxx -ascii
Initializing Variables with Keyboard Input • var = input(‘Enter an input value:’)
Multi-dimensional Arrays • Create a multi-dimensional array e.g. c is a 2x3x2 array • c(:, :, 1) = [1 2 3; 4 5 6]; • c(:, :, 2) = [7 8 9; 0 1 2]; • MATLAB always allocates array elements in column major order. • a(1, 1, 1) -> a(2, 1, 1) -> a(1, 2, 1) -> a(2, 2, 1) -> a(1, 3, 1)… • a(10) = 1
Subarray • Select and use subsets of a array c = [1 2 3; 4 5 6]; a = c(1, [ 2 3]) b = c([1 2], [ 2 3]) • The end functionc(1, 2:end) • Assignment to subarrayc(1, [ 2 3]) = [12 13] • Assigning a scalar to a subarray c(2, [ 2 3]) = 99
Assignment: • Excises 2.1, 2.2, 2.3 2.4
MATLAB & Its Applications In Engineering 李清都 副教授 liqd@cqupt.edu.cn
Lect. 3: Operations • Contents Covered: • basic conceptions of scalar/array/matrix operations • the main differences between array & matrix operation • operation rules • Objectives: • Firmly grasp the differences between array and matrix operation. • Fully grasp the hierarchy & the rules of the operations • Memorize some basic MATLAB functions, such as sqrt, … • Start to write M-file and debug it
MATLAB Operations • Scalar operation • Array operation • Matrix operation the differences between array operation & matrix operation!!!
Scalar operation Examples: • A = 4*2 • A = 2^((8+2)/5) • A = [ 1 2 3 4]; b = A^2;
Array operation Examples: • A = [ 1 2 3 4]; • B = [ 5 6 7 8]; • C = A .* B; Array operations are operations performed between arrays on an element-by-element basis. If as array operation, the result is [ 5 12 21 32]
Matrix operation Examples: • A = [ 1 2 3 4]; • B = [ 5 6 7 8]; • C = A * B; Matrix operations follow the normal rules of linear algebra such as matrix. If as matrix operation, the result is [ 19 22 43 50]
Array & Matrix operators Usually array operators have one extra ‘.’ than matrix operators.
Hierarchy of Operations Rules: • The contents of all parentheses • Exponentials • Multiplication and division • Additions and subtractions Question: What is the execute sequences of the following assignment? a = sin(pi^2/2*3+1) + 4*(2-1)
Build-in MATLAB functions • min, max • sqrt, mod • sin, cos, tan, atan, asin, acos • abs, angle • exp, log • ceil, round, floor, fix • These are very common MATLAB functions which we will use quite frequently.
M-File File new M-file And then write your code Debug… Think… Debug… Think… Debug… Think…
Assignment: • Excises 2.6, 2.7, 2.8, 2.9
Lect. 4: Display & Plotting • Contents Covered: • MATLAB data format • three functions: disp, fprintf, plot • the elements of a MATLAB figure • Objectives: • Understand the concepts of MATLAB data format • Firmly grasp the usage of ‘disp’, ‘fprintf’, ‘plot’ • Strengthen the skill on writing and debugging M-file
Data Formats • format Set display format for output • Syntax 1.format format by itself, changes the output format to the default appropriate for the class of the variable currently being used. For floating-point variables, for example, the default is format short (i.e., 5-digit scaled, fixed-point values).
Data Formats 2.format type format type changes the format to the specified type. The tables shown below list the allowable values for type. 3. format('type') format('type') is the function form of the syntax.
Data Formats Short Long Short e Short g Long e Long g Bank Hex Rat Compact Loose + formatoptions format command only controls the display format. It will not change the precision of the data.
Data Formats • Examples 1.>> format long >> pi ans =3.141592653589793 2. format + >> -2 ans = -
Data Formats Note The format function affects only how numbers are displayed, not how MATLAB computes or saves them.
disp disp( x) disp(X) displays an array, without printing the array name. If X contains a text string, the string is displayed. Another way to display an array on the screen is to type its name, but this prints a leading "X=," which is not always desirable.
disp Note that disp does not display empty arrays. Examples 1.str = [ ‘the value of pi = ‘ ,num2str(pi) ]; disp(str) The result: the value of pi = 3.1416