1 / 167

MATLAB & Its Applications In Engineering

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 ?.

jaegar
Télécharger la présentation

MATLAB & Its Applications In Engineering

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB & Its Applications In Engineering 李清都 副教授 liqd@cqupt.edu.cn

  2. 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

  3. 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?

  4. 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

  5. Why MATLAB? Using C language: main() { float m; m = 1-((107-20)/107)^10; printf(“%f”,m); }

  6. Why MATLAB? Using C language: main() { float m; m = 1-((107-20)/107)^10; printf(“%f”,m); } If only there was just line 3…

  7. You need such a powerful tool Powerful compute ability Easy of use MATLAB

  8. MATLAB:the most popular computer program optimized to perform engineering and scientific calculations.

  9. What can MATLAB do? Mathematical computing Visual results System modeling Graphic user interface

  10. The advantages of MATLAB Easy of use Platform independence Predefined functions Device-Independent Plotting Graphic user interface MATLAB compiler

  11. The disadvantages of MATLAB Slower execute speed Matlab is interpreted language other than compiled language All the variables use the highest precision Higher cost

  12. The MATLAB Environment Command window Work Space Command history

  13. 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

  14. 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

  15. MATLAB & Its Applications In Engineering 李清都 副教授 liqd@cqupt.edu.cn

  16. 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

  17. 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.

  18. 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.

  19. Special Variables i, j, pi, NaN, ans, eps MATLAB has some predefined variables and users should avoid using them create new variables.

  20. 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

  21. Initialize the Array There are 3 common ways: • Using assignment statement • Read from a file • From keyboard

  22. 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(?)

  23. Initializing Variables from file • MATLAB formatted file • load xxx.mat • save file_name var1, var2, var3… • ASCII file • load file_name.xxx -ascii

  24. Initializing Variables with Keyboard Input • var = input(‘Enter an input value:’)

  25. 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

  26. 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

  27. Assignment: • Excises 2.1, 2.2, 2.3 2.4

  28. MATLAB & Its Applications In Engineering 李清都 副教授 liqd@cqupt.edu.cn

  29. 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

  30. MATLAB Operations • Scalar operation • Array operation • Matrix operation the differences between array operation & matrix operation!!!

  31. Scalar operation Examples: • A = 4*2 • A = 2^((8+2)/5) • A = [ 1 2 3 4]; b = A^2;

  32. 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]

  33. 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]

  34. Array & Matrix operators Usually array operators have one extra ‘.’ than matrix operators.

  35. 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)

  36. 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.

  37. M-File File  new  M-file And then write your code Debug… Think… Debug… Think… Debug… Think…

  38. Assignment: • Excises 2.6, 2.7, 2.8, 2.9

  39. MATLAB & Its Applications In Engineering

  40. 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

  41. 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).

  42. 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.

  43. 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.

  44. Data Formats

  45. Data Formats

  46. Data Formats

  47. Data Formats • Examples 1.>> format long >> pi ans =3.141592653589793 2. format + >> -2 ans = -

  48. Data Formats Note   The format function affects only how numbers are displayed, not how MATLAB computes or saves them.

  49. 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.

  50. 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

More Related