Online Help 15 Missing multiplication operators and parentheses are among the most common errors. You can edit an input line by using the UP-ARRoW key to redisplay the pre- vious command,editing the command using the LEFT-and RIGHT-ARROW keys, and then pressing RETURN or ENTER.The UP.and DOWN-ARROW keys allow you to scroll back and forth through all the commands you've typed in a MATLAB session,and are very useful when you want to correct,modify,or reenter a previous command. Online Help There are several ways to get online help in MATLAB.To get help on a particu- lar command,enter help followed by the name of the command.For example, help solve will display documentation for solve.Unless you have a large monitor,the output of help solve will not fit in your MATLAB command window,and the beginning of the documentation will scroll quickly past the top of the screen.You can force MATLAB to display information one screen- ful at a time by typing more on.You press the space bar to display the next screenful,or ENTER to display the next line;type help more for details.Typing more on affects all subsequent commands,until you type more off. The command lookfor searches the first line of every MATLAB help file for a specified string (use lookfor -all to search all lines).For example, if you wanted to see a list of all MATLAB commands that contain the word "factor"as part of the command name or brief description,then you would type lookfor factor.If the command you are looking for appears in the list,then you can use help on that command to learn more about it. The most robust online help in MATLAB 6 is provided through the vastly improved Help Browser.The Help Browser can be invoked in several ways:by typing helpdesk at the command prompt,under the View item in the menu bar,or through the question mark button on the tool bar.Upon its launch you will see a window with two panes:the first,called the Help Navigator,used to find documentation;and the second,called the display pane,for viewing documentation.The display pane works much like a normal web browser.It has an address window,buttons for moving forward and backward (among the windows you have visited),live links for moving around in the documentation, the capability of storing favorite sites,and other such tools. You use the Help Navigator to locate the documentation that you will ex- plore in the display pane.The Help Navigator has four tabs that allow you to
Online Help 15 ➱ Missing multiplication operators and parentheses are among the most common errors. You can edit an input line by using the UP-ARROW key to redisplay the previous command, editing the command using the LEFT- and RIGHT-ARROW keys, and then pressing RETURN or ENTER. Th e UP- and DOWN-ARROW keys allow you to scroll back and forththroughall the commands you’ve typed in a MATLAB session, and are very useful when you want to correct, modify, or reenter a previous command. Online Help There are several ways to get online help in MATLAB. To get help on a particular command, enter help followed by the name of the command. For example, help solve will display documentation for solve. Unless you have a large monitor, the output of help solve will not fit in your MATLAB command window, and the beginning of the documentation will scroll quickly past the top of the screen. You can force MATLAB to display information one screenful at a time by typing more on. You press the space bar to display the next screenful, or ENTER to display the next line; type help more for details. Typing more on affects all subsequent commands, until you type more off. The command lookfor searches the first line of every MATLAB help file for a specified string (use lookfor -all to searchall lines). For example, if you wanted to see a list of all MATLAB commands that contain the word “factor” as part of the command name or brief description, then you would type lookfor factor. If the command you are looking for appears in the list, then you can use help on that command to learn more about it. The most robust online help in MATLAB 6 is provided through the vastly improved Help Browser. The Help Browser can be invoked in several ways: by typing helpdesk at the command prompt, under the View item in the menu bar, or through the question mark button on the tool bar. Upon its launch you will see a window with two panes: the first, called the Help Navigator, used to find documentation; and the second, called the display pane, for viewing documentation. The display pane works much like a normal web browser. It has an address window, buttons for moving forward and backward (among the windows you have visited), live links for moving around in the documentation, the capability of storing favorite sites, and other such tools. You use the Help Navigator to locate the documentation that you will explore in the display pane. The Help Navigator has four tabs that allow you to
16 Chapter 2:MATLAB Basics arrange your search for documentation in different ways.The first is the Con- tents tab that displays a tree view of all the documentation topics available. The extent of that tree will be determined by how much you (or your system administrator)included in the original MATLAB installation (how many tool- boxes,etc.).The second tab is an Index that displays all the documentation available in index format.It responds to your key entry of likely items you want to investigate in the usual alphabetic reaction mode.The third tab pro- vides the Search mechanism.You type in what you seek,either a function or some other descriptive term,and the search engine locates corresponding documentation that pertains to your entry.Finally,the fourth tab is a roster of your Favorites.Clicking on an item that appears in any of these tabs brings up the corresponding documentation in the display pane. The Help Browser has an excellent tutorial describing its own operation. To view it,open the Browser;if the display pane is not displaying the "Begin Here"page,then click on it in the Contents tab;scroll down to the "Using the Help Browser"link and click on it.The Help Browser is a powerful and easy-to-use aid in finding the information you need on various components of MATLAB.Like any such tool,the more you use it,the more adept you become at its use. If you type helpwin to launch the Help Browser,the display pane will contain the same roster that you see as the result of typing help at the command prompt,but the entries will be links. Variables and Assignments In MATLAB,you use the equal sign to assign values to a variable.For instance, >>X=7 X 7 will give the variable x the value 7 from now on.Henceforth,whenever MAT- LAB sees the letter x,it will substitute the value 7.For example,ify has been defined as a symbolic variable,then >>x^2-2*x*y+y ans 49-13*y
16 Chapter 2: MATLAB Basics arrange your search for documentation in different ways. The first is the Contents tab that displays a tree view of all the documentation topics available. The extent of that tree will be determined by how much you (or your system administrator) included in the original MATLAB installation (how many toolboxes, etc.). The second tab is an Index that displays all the documentation available in index format. It responds to your key entry of likely items you want to investigate in the usual alphabetic reaction mode. The third tab provides the Search mechanism. You type in what you seek, either a function or some other descriptive term, and the search engine locates corresponding documentation that pertains to your entry. Finally, the fourth tab is a roster of your Favorites. Clicking on an item that appears in any of these tabs brings up the corresponding documentation in the display pane. The Help Browser has an excellent tutorial describing its own operation. To view it, open the Browser; if the display pane is not displaying the “Begin Here” page, then click on it in the Contents tab; scroll down to the “Using the Help Browser” link and click on it. The Help Browser is a powerful and easy-to-use aid in finding the information you need on various components of MATLAB. Like any such tool, the more you use it, the more adept you become at its use. ✓ If you type helpwin to launch the Help Browser, the display pane will contain the same roster that you see as the result of typing help at the command prompt, but the entries will be links. Variables and Assignments In MATLAB, you use the equal sign to assign values to a variable. For instance, >> x = 7 x = 7 will give the variable x the value 7 from now on. Henceforth, whenever MATLAB sees the letter x, it will substitute the value 7. For example, if y has been defined as a symbolic variable, then >> xˆ2 - 2*x*y + y ans = 49-13*y
Solving Equations 17 To clear the value of the variable x,type clear x. You can make very general assignments for symbolic variables and then manipulate them.For example, >clear x;syms x y >>z=x^2-2*x*y+Y Z= x^2-2*x*y+y >>5*y*z ans 5*y*(x^2-2*x*y+y) A variable name or function name can be any string of letters,digits,and underscores,provided it begins with a letter (punctuation marks are not al- lowed).MATLAB distinguishes between uppercase and lowercase letters.You should choose distinctive names that are easy for you to remember,generally using lowercase letters.For example,you might use cubicsol as the name of the solution of a cubic equation. A common source of puzzling errors is the inadvertent reuse of previously defined variables. MATLAB never forgets your definitions unless instructed to do so.You can check on the current value of a variable by simply typing its name. Solving Equations You can solve equations involving variables with solve or fzero.For exam- ple,to find the solutions of the quadratic equation x2-2x-4=0,type >>8o1ve('x^2-2*x-4=0') ans [5^(1/2)+1] [1-5(1/2)] Note that the equation to be solved is specified as a string;that is,it is sur- rounded by single quotes.The answer consists of the exact(symbolic)solutions
Solving Equations 17 ➱ To clear the value of the variable x, type clear x. You can make very general assignments for symbolic variables and then manipulate them. For example, >> clear x; syms x y >> z = xˆ2 - 2*x*y + y z = x^2-2*x*y+y >> 5*y*z ans = 5*y*(x^2-2*x*y+y) A variable name or function name can be any string of letters, digits, and underscores, provided it begins witha letter (punctuation marks are not allowed). MATLAB distinguishes between uppercase and lowercase letters. You should choose distinctive names that are easy for you to remember, generally using lowercase letters. For example, you might use cubicsol as the name of the solution of a cubic equation. ➱ A common source of puzzling errors is the inadvertent reuse of previously defined variables. MATLAB never forgets your definitions unless instructed to do so. You can check on the current value of a variable by simply typing its name. Solving Equations You can solve equations involving variables with solve or fzero. For example, to find the solutions of the quadratic equation x2 − 2x − 4 = 0, type >> solve(’xˆ2 - 2*x - 4 = 0’) ans = [ 5^(1/2)+1] [ 1-5^(1/2)] Note that the equation to be solved is specified as a string; that is, it is surrounded by single quotes. The answer consists of the exact (symbolic) solutions
18 Chapter 2:MATLAB Basics 1+v5.To get numerical solutions,type double(ans),or vpa(ans)to dis- play more digits. The command solve can solve higher-degree polynomial equations,as well as many other types of equations.It can also solve equations involving more than one variable.If there are fewer equations than variables,you should spec- ify (as strings)which variable(s)to solve for.For example,type solve ('2*x log(y)=1','y')to solve 2x-logy =1 for y in terms of x.You can specify more than one equation as well.For example, >>[x,Y]=solve("x^2-y=2','y-2*x=5') X [1+2*2^(1/2)] [1-2*2^(1/2)] y= [7+4*2^(1/2)] [7-4*2^(1/2)] This system of equations has two solutions.MATLAB reports the solution by giving the two x values and the two y values for those solutions.Thus the first solution consists of the first value ofx together with the first value of y.You can extract these values by typing x(1)and y(1): >>x(1) ans 1+2*2^(1/2) >>y(1) ans 7+4*2^(1/2) The second solution can be extracted with x(2)and y(2). Note that in the preceding solve command,we assigned the output to the vector [x,y].If you use solve on a system of equations without assigning the output to a vector,then MATLAB does not automatically display the values of the solution: >>8o1=so1ve('x^2-y=2','y-2*x=5')
18 Chapter 2: MATLAB Basics 1 ± √ 5. To get numerical solutions, type double(ans), or vpa(ans) to display more digits. The command solve can solve higher-degree polynomial equations, as well as many other types of equations. It can also solve equations involving more than one variable. If there are fewer equations than variables, you should specify (as strings) which variable(s) to solve for. For example, type solve(’2*x - log(y) = 1’, ’y’) to solve 2x − log y = 1 for y in terms of x. You can specify more than one equation as well. For example, >> [x, y] = solve(’xˆ2 - y = 2’, ’y - 2*x = 5’) x = [ 1+2*2^(1/2)] [ 1-2*2^(1/2)] y = [ 7+4*2^(1/2)] [ 7-4*2^(1/2)] This system of equations has two solutions. MATLAB reports the solution by giving the two x values and the two y values for those solutions. Thus the first solution consists of the first value of x together with the first value of y. You can extract these values by typing x(1) and y(1): >> x(1) ans = 1+2*2^(1/2) >> y(1) ans = 7+4*2^(1/2) The second solution can be extracted with x(2) and y(2). Note that in the preceding solve command, we assigned the output to the vector [x, y]. If you use solve on a system of equations without assigning the output to a vector, then MATLAB does not automatically display the values of the solution: >> sol = solve(’xˆ2 - y = 2’, ’y - 2*x = 5’)
Solving Equations 19 sol x:[2x1 sym] y:[2x1 sym] To see the vectors ofx and y values of the solution,type sol.x and sol.y.To see the individual values,type sol.x(1),sol.y(1),etc. Some equations cannot be solved symbolically,and in these cases solve tries to find a numerical answer.For example, >solve('sin(x)2 -x') ans 1.1060601577062719106167372970301 Sometimes there is more than one solution,and you may not get what you expected.For example, >solve('exp(-x)=sin(x)') ans -2.0127756629315111633360706990971 +2.7030745115909622139316148044265*1 The answer is a complex number;the i at the end of the answer stands for the number v-1.Though it is a valid solution of the equation,there are also real number solutions.In fact,the graphs of exp(-x)and sin(x)are shown in Figure 2-3;each intersection of the two curves represents a solution of the equation e-*sin(x). You can numerically find the solutions shown on the graph with fzero, which looks for a zero of a given function near a specified value ofx.A solution of the equation e-*=sin(x)is a zero of the function e-*-sin(x),so to find the solution near x=0.5 type >fzero(inline('exp(-x)-sin(x)'),0.5) ans 0.5885 Replace 0.5 with 3 to find the next solution,and so forth. In the example above,the command inline,which we will discuss further in the section User-Defined Functions below,converts its string argument to a
Solving Equations 19 sol = x: [2x1 sym] y: [2x1 sym] To see the vectors of x and y values of the solution, type sol.x and sol.y. To see the individual values, type sol.x(1), sol.y(1), etc. Some equations cannot be solved symbolically, and in these cases solve tries to find a numerical answer. For example, >> solve(’sin(x) = 2 - x’) ans = 1.1060601577062719106167372970301 Sometimes there is more than one solution, and you may not get what you expected. For example, >> solve(’exp(-x) = sin(x)’) ans = -2.0127756629315111633360706990971 +2.7030745115909622139316148044265*i The answer is a complex number; the i at the end of the answer stands for the number √−1. Though it is a valid solution of the equation, there are also real number solutions. In fact, the graphs of exp(−x) and sin(x) are shown in Figure 2-3; each intersection of the two curves represents a solution of the equation e−x = sin(x). You can numerically find the solutions shown on the graph with fzero, which looks for a zero of a given function near a specified value of x. A solution of the equation e−x = sin(x) is a zero of the function e−x − sin(x), so to find the solution near x = 0.5 type >> fzero(inline(’exp(-x) - sin(x)’), 0.5) ans = 0.5885 Replace 0.5 with 3 to find the next solution, and so forth. ☞ In the example above, the command inline, which we will discuss further in the section User-Defined Functions below, converts its string argument to a