Question? Leave a message!




Introduction to Matlab Programming with Applications

Introduction to Matlab Programming with Applications 4
Introduction to Matlab Programming with Applications ZhengLiang Lu Department of Computer Science and Information Engineering National Taiwan University Matlab249 Winter Break, 2015Class Information Ocial Web page http://www.csie.ntu.edu.tw/d00922011/matlab.html Lecture notes will be uploaded right before class. Note that the lecture notes are organized in English. Contact information d00922011ntu.edu.tw If you have any question, please contact me via email. facebook ZhengLiang Lu 1/48Prerequisites No programming experience required. It would be helpful if you have some programming experiences. Linear Algebra is strongly recommended. MATLAB refers to MATrix LABoratory. Linear Algebra is widely used in numerical analysis and quantitative methods. Please be informed that you should consider other classes if you want to learn any advanced computer science courses. I promise to keep everythingsimple in this class. ZhengLiang Lu 2/48Grading To pass the class, you need at least60 pts in the end of class. Programming assignment (40 pts) Final project/exam (60 pts) 1 Please be noted that you preserve the right to refund before the 5th lecture. 1 http://www.csie.ntu.edu.tw/train/page=refund.html ZhengLiang Lu 3/48Final Project Topic: anything if you are interested in or working on. Requirement: you should use MATLAB as the main programming language. Your grade depends on the following aspects: Problem de nition 2 Innovation Completion Goal: Solve a problem by formulating the problem, organizing into an algorithm, implementing a program by MATLAB Get familiar with programming concepts Gain the experiences in order to learn more programming language by yourself. 2 No copyandpaste. ZhengLiang Lu 4/48Final Exam Onsite programming 10 questions Open everything No communication with classmates and your friends ZhengLiang Lu 5/48Who/How/Why ZhengLiang Lu 6/48Articles about Learning Programming 5 REASONS YOU NEED TO LEARN TO PROGRAM The reason everyone should learn to code Why Learn to Program ºÀ¼x. ž ZhengLiang Lu 7/48Programs A (computer) program is a sequence of instructions, written in an arti cial language, to perform a speci ed task with a computer. They are almost everywhere such that you can name it. For example, Computer virus Games Operating systems, such as Windows 8.1, UNIX/Linux, Android (family), OS X and iOS Social networking, such as facebook, twitter, and so on The di erences are the goal and functionality. ZhengLiang Lu 8/48How and Where The Programs Run The programs are activated from the disk into the main memory. Now we call them the processes. All the instructions with data are executed in the registers in CPUs. The results are written back to the main memory and further stored into the disk after the processes are terminated. ZhengLiang Lu 9/483 Memory Hierarchy 3 See Bryant, p. 14. ZhengLiang Lu 10/48Programming Languages A programming language is an arti cial language designed to communicate with a machine by instructions. Programming languages syntax + semantics Used to express algorithms Used to control the behavior of a machine ZhengLiang Lu 11/48How Many Programming Languages More than 100 In historical perspective, 1st generation: machine code 2nd generation: assembly code 3rd generation: highlevel programming languages Beyond 3rd generation 4 MATLAB belongs to post3rdgeneration programming languages. You can refer to TIOBE Programming Community Index. 4 1984 ZhengLiang Lu 12/48ZhengLiang Lu 13/481stGeneration Programming Languages A 1stgeneration programming language is a machinelevel programming language. Machinedependent The instructions are made of binary numbers, represented by 1s and 0s. There was originally no compiler or assembler to process the instructions. ZhengLiang Lu 14/48Pros: Most ecient for machines Cons: Hard to program for human Not portable Still widely (and occasionally) used in programming lower level functions of the system, such as drivers, interfaces with rmware and hardware devices. Aka machine code. ZhengLiang Lu 15/482ndGeneration Programming Languages 2ndgeneration programming language is a generational way to categorize assembly languages. The code can be read and written by a programmer. Yet, it is still machinedependent. To run on a computer, it must be converted into a machine readable form, a process called assembly. More often nd use in extremely intensive processing such as games, video editing, graphic manipulation/rendering. ZhengLiang Lu 16/483rdGeneration Programming Languages A 3rdgeneration language aims to be closer to the human domain. Instructions operate at a higher, abstract level, closer to the human way of thinking. Each individual instruction can be translated into a number of machinelevel instruction via a speci c compiler. Summary: More friendly to understand, easier to learn Portable, machineindependent Support structured programming 5 6 7 Such as C , C++ , and Java Aka highlevel programming languages 5 1973 6 1983 7 1995 ZhengLiang Lu 17/48What Can A Program Do A program is an implementation of an algorithm expressed in a speci c programming language. ZhengLiang Lu 18/48Algorithm In A Nutshell 8 An algorithm is a sequence of unambiguous instructions for solving a problem, that is, for obtaining a required output for any legitimate input in a nite amount of time. Simply put, an algorithm is a procedure that solves a particular type of problems, such as a cookbook. 8 See Algorithm in Wikipedia. ZhengLiang Lu 19/48Properties of Algorithms Typical properties of any algorithm are summarized as follows: Input Statements: de niteness Instructions: e ectiveness 9 Number of steps: niteness Output: correctness Note that an algorithm is not necessarily expressed in a speci c programming language. Instead, one can express an algorithm in some human language using a programming language format. 9 Alan Turing (19121954) ZhengLiang Lu 20/48Example Organize an algorithm that nds the greatest element in the input list. Input: A (a list of n integers) Output: x (the greatest element in A) 1 x A(1) 2 for i 1 n 3 if A(i) y 4 x A(i) 5 end 6 end 7 return x ZhengLiang Lu 21/48\Computers are good at following instructions, but not at reading your mind." Donald Knuth (1938) ZhengLiang Lu 22/48MATLAB In A Nutshell 10 In my view, MATLAB is a powerful and simple math software: 11 Powerful: programmable, fruitful toolboxes available Simple: don't need to worry about the machine, easy to draw a graph The only thing you need to provide is ... 10 Simple is not easy. 11 http://www.mathworks.com/products/ ZhengLiang Lu 23/48Algorithms ZhengLiang Lu 24/48To Whom It May Concern, If MATLAB is your rst programming language, Pros: easy to learn Cons: may feel frustrated when learning other (lower) languages If MATLAB is not your rst programming language, Pros: extremely easy to learn (Warning: It may be boring.) Cons: cannot get rid of it because it is too easy... The core concepts of programming are the same among various programming languages. The most important thing is your algorithm. ZhengLiang Lu 25/48Conclusions Program design algorithm + programming language Algorithm idea, steps Programming language Chinese, English In this class, we will learn MATLAB and some algorithms which are the core of programming world. ZhengLiang Lu 26/48Father of MATLAB Cleve Moler ZhengLiang Lu 27/48Get started to implement your idea with MATLAB. ZhengLiang Lu 28/481 Lecture 1 2 3 Overview of MATLAB 4 ZhengLiang Lu 29/48\Learning a new language is harmless. It gives you new ideas and insights." Yukihiro Matsumoto (,L) (1965) ZhengLiang Lu 30/48Contents Environment Script editor Variable types Scalar variable Numeric arrays Character arrays (String) Cell arrays Structure arrays Builtin arithmetic functions ZhengLiang Lu 31/48Working Environment ZhengLiang Lu 32/48Command Window You can give a command in the Command Window. Let's try a greeting, \Hello, MATLAB." 1 disp('Hello, MATLAB.'); disp prints the string as input in the parenthesis. A string is singlequoted in MATLAB. Convention in the slides: Boxes are the listings for programs. Words and sentences highlighted in red are important. Words in blue are reserved words. Bold words in black are functions. ZhengLiang Lu 33/48Tips for Learning MATLAB Don't be frustrated:W/œKï ¯//º† „Ÿ\ MATLAB interrupts your program when a syntax error occurs. lookfor is used to look for the commands related to the key word you give. 1 lookfor display 2 ... 3 disp Display array. 4 display Display array. 5 error Display message and abort function. 6 warning Display warning message; disable or ... enable warning messages. 7 ... help returns the usage of a command ZhengLiang Lu 34/481 help disp 2 ... Help browser provides detail of commands with examples. Google is your friend. ZhengLiang Lu 35/48Script Editor We often write a program in the script editor. Save the program in .m le. .m les are used as scripts and functions. A script is a complete program which contains the input data and can be executed directly. A function is a program which may need input data when being called in other programs. (why) ZhengLiang Lu 36/48ZhengLiang Lu 37/48helloworld.m Let's create a .m le with the following code lines: 1 This is the comment section. 2 clear; Clear all variables stored in Workspace. 3 clc; Clear the screen. 4 main program 5 disp('Hello, world.'); Note that The lines which begins with are regarded as the comments. Comments are not executed. clear is used to release the variables. clc cleans the command window. ZhengLiang Lu 38/4812 13 Save and press F5 to run the script. Do not give a script le the same name as a variable. (Why) Do not give a lename with blanks in the middle. If the .m le is not at the current folder when you execute it, MATLAB will ask: 1 Change folder 2 Add to Path Pool You can run helloworld.m later just by calling helloworld in the command window. 1 helloworld 2 3 Hello, world. 12 ctrl + s 13 Autosave before execution. ZhengLiang Lu 39/48Comments In Scripts Anything following a is seen as a comment. Press ctrl + r to comment. ctrl + t to decomment. To comment a block of lines, f g. The rst contiguous comment becomes the script's help document. 1 help helloworld 2 3 This is the comment section. Why comments Document is helpful for both the programmers and users. Comment thoroughly to avoid wasting time later. ZhengLiang Lu 40/48Variables Variable is used to store a value, assigned by variable name = expression: x = x + 1 is correct. (Why) Actually, the assignment operator is used to put a value in the variable, from right to left. For example, let a = 10;b = 20. Then a +b = 30. 1 a = 10; 2 b = 20; 3 a + b 4 5 ans = 6 30 ans is the default variable if you don't assign one for it. ZhengLiang Lu 41/48If you drop the semicolon (;) in the line, then the result will appear in the command window, say, 1 a = 10 2 3 a = 4 5 10 Note that you may not drop semicolons in other programming languages. The variable name is suggested to begin with a letter of the alphabet. Cannot begin with a number, e.g. 1x is wrong. Cannot have a blank, e.g. x 1 is wrong. Cannot have +n, e.g. x1 ZhengLiang Lu 42/4814 MATLAB is casesensitive , e.g. A and a are di erent. 15 The length of name is limited to 63 characters . namelengthmax returns what this maximum length is. 16 We avoid to use names of reserved words and builtin functions. The reserved words cannot be used as a variable, e.g. for, if, and end. p p i = 1 and j = 1 by default. If you set i = 1, then i = 1 until you clear i. 17 Variable names should always be mnemonic . 14 In general, programming languages are casesensitive, but some are not, e.g. Fortran. 15 Windows 7 (64bit) in this case 16 Try iskeyword. 17 To assist your memory. ZhengLiang Lu 43/48Data Type and Casting Di erent types of variables occupy di erent size of memory. 18 All numeric variables are stored as the type double by default. Variables de ned in type char are of size 2 bytes. Variables de ned in type logical are of size 1 bytes. whos shows variables that have been de ned in the command window. cast converts one variable to another type. int8, int16, int32, and int64 are the integer types. 18 8 bytes = 64 bits. ZhengLiang Lu 44/48Computational Limits Over ow occurs when the value is beyond the limits. ZhengLiang Lu 45/48Scalar Variables A scalar is a single number. A scalar variable is a variable that contains a single number. 1 1 x is said to be a scalar variable if x2R orC . The complex eld, denoted byC, contains all the complex numbers, in form of a +bi; p 1 where i = 1, and a;b2R . Try x = 1 +i in the command window. 1 2 In math,C is equivalent toR . So, x is stored as the type double with 16 = 8 2 bytes. ZhengLiang Lu 46/4819 Scalar Arithmetic Operators 19 See Table 2.1 Arithmetic Operations Between Two Scalars in Moore, p. 21. ZhengLiang Lu 47/4820 Display Format In MATLAB, it is a common convention to use e to identify a power of 10, e.g. 1e5 = 100; 000. 20 See Table 1.15 Numeric display formats in Palm, p. 15. ZhengLiang Lu 48/48
Website URL
Comment