L ó p e z MATLAB Linear Algebra MATLAB is a high-level language and environment for numerical computation, visualization, and pro- gramming. Using MATLAB, you can analyze data, develop algorithms, and create models and appli- M cations. The language, tools, and built-in math functions enable you to explore multiple approaches and reach a solution faster than with spreadsheets or traditional programming languages, such as A C/C++ or Java. T MATLAB Linear Algebra introduces you to the MATLAB language with practical hands-on instruc- tions and results, allowing you to quickly achieve your goals. In addition to giving an introduction L to the MATLAB environment and MATLAB programming, this book provides all the material needed A to work in linear algebra with ease. In addition to exploring MATLAB’s matrix algebra capabilities, B it describes the MATLAB commands that are used to create two- and three-dimensional graphics, including explicit, implicit and parametric curve and surface plotting, and various methods of data representation. Methods for manipulating and simplifying algebraic expressions are covered and L computational techniques for solving algebraic equations and systems of equations are detailed. i n · Learn to use the MATLAB environment e · Program the MATLAB language from first principles · Produce high quality plots of explicit, implicit and parametric curves and surfaces a · Create exploratory data plots such as histograms, pie charts, scatter plots, etc. r · Manipulate and simplify algebraic expressions · Use MATLAB for linear algebra problems, including techniques for matrix decomposition A and the general study of linear and quadratic forms l g e b r a ISBN 978-1-4842-0323-1 54999 Shelve in Applications/Mathematical & 9781484203231 Statistical Software For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance About the Author �����������������������������������������������������������������������������������������������������������������ix ■ Chapter 1: MATLAB Introduction and Working Environment ���������������������������������������������1 ■ Chapter 2: Variables, Numbers, Operators and Functions �����������������������������������������������25 ■ Chapter 3: Curves in Explicit, Parametric and Polar Coordinates� Surfaces ��������������������79 ■ Chapter 4: Algebraic Expressions, Polynomials, Equations and Systems ���������������������137 ■ Chapter 5: Matrices, Vector Spaces and Linear Applications ����������������������������������������201 iii Chapter 1 MATLAB Introduction and Working Environment The MATLAB Working Environment The following table summarizes the main components of the MATLAB working environment. Tool Description Command History This presents a history of the functions introduced in the Command Window and allows you to copy and execute them (see the lower right part of Figure 1-2). Command Window This is the window in which you execute MATLAB commands (see the central part of Figure 1-2). Workspace This shows the present contents of the workspace and allows you to make changes to it (see the upper right part of Figure 1-2). Help This allows you to search and read the documentation for the complete family of MATLAB products. Start button This runs tools and gives you access to documentation for all MathWorks products currently installed on your computer (Figure 1-3). Figure 1-1 shows the screen in which you enter MATLAB programs. This is MATLAB’s primary work environment. 1 Chapter 1 ■ MatLaB IntroduCtIon and WorkIng envIronMent Figure 1-1. 2 Chapter 1 ■ MatLaB IntroduCtIon and WorkIng envIronMent Figure 1-2. 3 Chapter 1 ■ MatLaB IntroduCtIon and WorkIng envIronMent Figure 1-3. Any MATLAB commands are entered in the Command Window to the right of the user input prompt “>>” and the response will appear in the lines immediately below, after pressing Enter. After the command has been executed the user input prompt will reappear, allowing you to enter more commands (Figure 1-4). 4 Chapter 1 ■ MatLaB IntroduCtIon and WorkIng envIronMent Figure 1-4. If the result of a command is not assigned to a variable, MATLAB will return the response using the expression ans =, as shown at the beginning of Figure 1-4. If the result is assigned to a variable then we can use that variable as an argument in subsequent commands. This is the case for the variable v in Figure 1-4, which is subsequently used as input for an exponential. To execute a MATLAB command, simply press Enter once the command is written. If at the end of the input we put a semicolon, the program will execute the command and keep the result in memory (Workspace), but it will not display the result on screen (see the first input in Figure 1-13). The input prompt “>>” will then reappear to indicate that you can enter a new command. Like the C programming language, MATLAB is case sensitive; for example, Sin(x) is not the same as sin(x). The names of all built-in functions begin with a lowercase character. There should be no spaces in the names of commands, variables or functions. In other cases, spaces are ignored, and they can be used to make the input more readable. Multiple entries can be entered in the same command line by separating them with commas, pressing Enter at the end of the last entry (see Figure 1-4). If you use a semicolon at the end of one of the entries in the line, its corresponding output will not be displayed. 5 Chapter 1 ■ MatLaB IntroduCtIon and WorkIng envIronMent Figure 1-5. Descriptive comments can be entered in a command input line by starting them with the “%” symbol. When you run the input, MATLAB ignores the comment and processes the rest of the code (see Figure 1-6). Figure 1-6. 6 Chapter 1 ■ MatLaB IntroduCtIon and WorkIng envIronMent To simplify the process of entering script to be evaluated by the MATLAB interpreter (via the Command Window prompt), you can use the arrow keys. For example, if you press the up arrow key once, you will recover the last entry you submitted. If you press the up key twice, you will recover the penultimate entry you submitted, and so on. If you type a sequence of characters in the input area and then press the up arrow key, you will recover the last entry you submitted that begins with the specified string. Commands entered during a MATLAB session are temporarily stored in the buffer (Workspace) until you end the session, at which time they can be permanently stored in a file or are permanently lost. Below is a summary of the keys that can be used in MATLAB’s input area (command line), together with their functions: Up arrow (Ctrl-P) Retrieves the previous entry. Down arrow (Ctrl-N) Retrieves the following entry. Left arrow (Ctrl-B) Moves the cursor one character to the left. Right arrow (Ctrl-F) Moves the cursor one character to the right. CTRL-left arrow Moves the cursor one word to the left. CTRL-right arrow Moves the cursor one word to the right. Home (Ctrl-A) Moves the cursor to the beginning of the line. End (Ctrl-E) Moves the cursor to the end of the current line. Escape Clears the command line. Delete (Ctrl-D) Deletes the character indicated by the cursor. Backspace Deletes the character to the left of the cursor. CTRL-K Deletes (kills) the current line. The command clc clears the command window, but does not delete the contents of the work area (the contents remain in the memory). Help in MATLAB You can find help for MATLAB via the help button in the toolbar or via the Help option in the menu bar. In addition, support can also be obtained via MATLAB commands. The command help provides general help on all MATLAB commands (see Figure 1-7). By clicking on any of them, you can get more specific help. For example, if you click on graph2d, you get support for two-dimensional graphics (see Figure 1-8). 7
Description: