matlab programming lecture 1
play

MATLAB Programming (Lecture 1) Dr. SUN Bing School of EIE Beihang - PowerPoint PPT Presentation

MATLAB Programming (Lecture 1) Dr. SUN Bing School of EIE Beihang University www.buaa.edu.cn Contents 2 Contents 3 1.1 What s Matlab? MATLAB is short for MATrix LABoratory. MATLAB is both a powerful computational environment


  1. MATLAB Programming (Lecture 1) Dr. SUN Bing School of EIE Beihang University www.buaa.edu.cn

  2. Contents 2

  3. Contents 3

  4. 1.1 What ’ s Matlab? • MATLAB is short for MATrix LABoratory. • MATLAB is both a powerful computational environment and a programming language for scientific and engineering computations and graphics. • MATLAB has three parts: • Computational environment and a programming language • Data visualization • Symbolic operations • Typical uses include: • Math and numeric computation • Algorithm development • Scientific and engineering graphics • Modelling, simulation, and prototyping 4

  5. 1.1 What is MATLAB? Developed by The MathWorks Latest release R2016b / 2016-9-15 OS Cross-platform Type Technical computing License Proprietary Website www.mathworks.com Paradigm imperative Appeared in late 1970s Designed by Cleve Moler Developer The MathWorks OS Cross-platform 5

  6. 1.1 What is MATLAB? MATLAB Allows: • Easy matrix manipulation • Plotting of functions and data • Implementation of algorithms • Creation of user interfaces • Interfacing with programs in other languages • Access to computer algebra capabilities (Maple engine) 6

  7. 1.2 Why do we learn Matlab?  It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation.  Now more than 3200 Universities around the world use the MathWorks ’ s products for teaching and research in a broad range of technique.  Now, MATLAB has become a standard tool for many working in science or engineering fields. It ’ s very useful for us to learn Matlab Programming. 7

  8. 1.3 How to learn Matlab? Class study • Lectures • Examples Exercises • Homework • Demo • help Projects • Solve your problems • Applications 8

  9. 1.4 Course arrangement  1.4.1 Course Contents  1.4.2 Teaching material  1.4.3 Teacher information  1.4.4 Assessment  1.4.5 Homework  1.4.6 Objective 9

  10. 1.4.1 Course Contents  The MATLAB user interface  Working with MATLAB variables  Plotting and data visualization  Matrix and Array Operations  M-Files (script and function)  Data types and Data input and output  Programming Techniques  Building graphical user interfaces ( GUI )  Matlab application 10

  11. 1.4.2 Teaching material MATLAB Programming for Engineers (Second Edition) by Stephen J. Chapman ( 英文影印版 ) 科学出版社 Science Press www.sciencep.com 11

  12. 1.4.3 Teacher information Teacher: Dr. SUN Bing ( 孙兵 ) • E-mail : bingsun@buaa.edu.cn • Office room : New Main Building F-617 • Office Tel. : 82338670 Assistant: Nouman Ahmed ( 诺曼 ) • E-mail : fnouman90@yahoo.com • Office room : New Main Building D-537 Assistant: Zuo Zhixiong ( 左志雄 ) • E-mail : zuozhixiong@buaa.edu.cn • Office room : New Main Building F-601 • Office Tel. : 82338154 12

  13. 1.4.4 Assessment Your grade will be computed based upon your final Project, your home works problem set grades, and your attendance record in class. Course Hours: 32 Credits: 2 The weighting on these factors are as follows: • Attendance records in class 10% (at least 7 times) • 6 Home works 60% • Design and implement a GUI project 30% If you sign 6 or less, you will get zero. 13

  14. 1.4.5 Homework There are 6 homework. The requirements are: 1. Copying is not allowed and will be punished. 2. In your homework M-file the first two comment lines should be your full name and your student ID. For example, % Student name: xxxxxx xxxxx % Student ID : LSxxxxxx 3. Submit your M-files to the following website. 14

  15. How to submit homework 1.Open the website. http://course.buaa.edu.cn/opencourse/course/ detail/5476 15

  16. 2.Login in 16

  17. 3.Choose-matlab program-assignments 17

  18. 4.Select attachment and submit 18

  19. Attentions  Maybe you can’t login the above website out of school, login vpn.buaa.edu.cn first.  If still not, please send your homework via the Email: buaamatlab@yahoo.com 19

  20. Contents 20

  21. 2.1 The History of MATLAB  In the mid-1970s, Cleve Moler and several colleagues developed the FORTRAN subroutine libraries called LINPACK and EISPACK under a grant from the National Science Foundation.  LINPACK is a collection of FORTRAN subroutines for solving linear equations, while EISPACK contains subroutines for solving eigenvalue problems. 21

  22. 2.1 The History of MATLAB  In the late 1970s, Cleve, who was then chairman of the computer science department at the University of New Mexico, wanted to be able to teach students in his linear algebra courses using the LINPACK and EISPACK software. However, he didn't want them to have to program in FORTRAN, because this wasn't the purpose of the course.  So, as a "hobby" on his own time, he started to write a program that would provide simple interactive access to LINPACK and EISPACK. He named his program MATLAB, for MATrix LABoratory. 22

  23. 2.1 The History of MATLAB  Over the next several years, when Cleve would visit another university to give a talk, or as a visiting professor, he would end up by leaving a copy of his MATLAB on the university machines.  In early 1983, John Little was exposed to MATLAB because of a visit Cleve made to Stanford University. Little, an engineer, recognized the potential application of MATLAB to engineering applications.  In 1983, Little teamed up with Moler and Steve Bangert to develop a second generation, professional version of MATLAB written in C and integrated with graphics. 23

  24. 2.1 The History of MATLAB  The MathWorks, Inc. was founded in 1984 to market and continue development of MATLAB.  Now thousands of Universities around the world use the MathWorks ’ s products for teaching and research in a broad range of technique.  The latest version is MATLAB R2016b. 24

  25. Fortran and Scientific Computing Engineering and scientific applications involve a lot of • "number crunching". For many years, the main language for this was • FORTRAN -- first "high level" programming language, and especially designed for numerical computing. Here's a Fortran code to solve a x 2 + b x + c = 0: • C Solve a quadratic equation (this is a comment). DESC = B*B - 4*A*C IF ( DESC .LT. 0.0 ) GOTO 10 DESC = SQRT(DESC) X1 = (-B + DESC)/(2.0*A) X2 = (-B - DESC)/(2.0*A) WRITE(6,*) "SOLUTIONS ARE ",X1," AND ", X2 RETURN 10 WRITE(6,*) "EQUATION HAS COMPLEX ROOTS" RETURN 25

  26. Problems using FORTRAN " Number crunching " on a computer can be tricky. Problems that occur are:  loss of precision and inaccurate results: X = 0.1 Y = 1.0 - 10*X Y "should" equal 0, but probably does not!  underflow and overflow: X = 1.0E20, X*X  too big!  efficient coding of algorithms not always obvious DO 10 N=1,100000  inefficient! 10 Y(N) = SQRT(2.0)*X(N)  programming errors! 26

  27. Solving a Linear System in Fortran Here's a Fortran code to solve a linear system b = A*x, solve for x. It doesn't check for degeneracy or zeros. C Solve B = A*X for X. C... Backwards substitution C N is dimension of vectors and matrix X(N) = X(N)/A(N,N) C Does not use row interchange, scaling. DO 21 I=N-1,1,-1 SUBROUTINE LINSYS(N, A, X, B, TMP) TMP = X(I) INTEGER N DO 20 J=I+1,N DOUBLE PRECISION A(N,N), X(N), B(N) 20 TMP = TMP - A(I,J)*X(J) DOUBLE PRECISION TMP(N), RATIO X(I) = TMP/A(I,I) C... Forward elimination 21 CONTINUE DO 13 J=1,N-1 RETURN DO 12 I=J+1,N END RATIO = -A(I,J)/A(J,J) A(I,*) = A(I,*) +RATIO*ROW(J,*) This is just a small example. DO 11 K=J+1,N 11 A(I,K) = A(I,K) + RATIO*A(J,K) A full program may be 1000's of A(I,J) = 0.0 X(I) = X(I) + RATIO*X(J) lines long. 12 CONTINUE 11 CONTINUE Continued... 27

  28. Need for Numerical Libraries  The U.S. government recognized these problems, and the inefficiency of many engineers all writing the same algorithms... again and again.  So, they commissioned numerical analysts to write good quality algorithms for common tasks.  Make the results freely available as "libraries" of subroutines than anyone can use in their programs.  Libraries are available at: www.netlib.org 28

  29. Examples of Numerical Libraries  BLAS (Basic Linear Algebra Subroutines): operations on vectors, like adding to vectors, dot product, norm.  LINPACK: linear algebra subroutines for vector-matrix operations, solving linear systems, factoring a matrix, inverting a matrix. Later replaced by LAPACK.  EISPACK: compute eigenvalues and eigenvectors of matrices.  Example: solve A*x = b using LINPACK C.... factor the A matrix CALL SGEFA(A, N, N, IPVT, INFO) C.... copy B vector into X vector CALL SCOPY(N, B, 1, X, 1) C.... solve the system of equations CALL SGESL(A, N, N, IPVT, X, 0) 29

  30. Still Not Easy Enough!  Cleve Moler, mathematician, C.S. Professor, and co- author of LINPACK, thought this is still too much work:  write FORTRAN, compile, debug, compile, run...  He wanted to give students easy access to LINPACK.  So, he wrote MATLAB ("Matrix Laboratory").  interactive  easy input, output  operations on a whole vector or matrix at once  Example: solve b = A*x in Matlab... x = A \ b 30

Recommend


More recommend