6 8 0 1>> k = find(A==0)k =29Thus, we find that A has elements equal to 0 inpositions 2 and 9. To interpret this result wehave to recognize that “find” first reshapes Ainto a column vector (see §15.1)—this is equivalentto numbering the elements of A by columnsas in1 4 7 102 5 8 113 6 9 12>> n = find(A > A(n)ans =-20-10Thus, n gives a list of the locations of the entriesin A that are apple 0 and then A(n) gives us thevalues of the elements selected.>> m = find( A’ == 0)m =511Since we are dealing with A’, the entries arenumbered by rows.22 Function m–filesWe can extend the number of Matlab built-infunctions by writing our own. They are specialcases of m–files (§7).Example 22.1 The area, A, of a triangle withsides of length a, b and c is given byA = p s(s a)(s b)(s c),where s =(a+b+c)/2. Write a Matlab functionthat will accept the values a, b and c as inputsand return the value of A as output.The main steps to follow when defining a Matlabfunction are:1. Decide on a name for the function, makingsure that it does not conflict with aname that is already used by Matlab. Inthis example the name of the function isto be area, so its definition will be savedin a file called area.m2. The first line of the file must have theformat:function [list of outputs]= function name(list of inputs)For our example, the output (A)isafunctionof the three variables (inputs) a, band c so the first line should readfunction [A] = area(a,b,c)3. Document the function. That is, describebriefly the purpose of the function andhow it can be used. These lines should bepreceded by % which signify that they arecomment lines that will be ignored whenthe function is evaluated.4. Finally include the code that defines thefunction. This should be interspersed withsu cient comments to enable another userto understand the processes involved.The complete file might look like:function [A] = area(a,b,c)% Compute the area of a triangle whose% sides have length a, b and c.% Inputs:% a,b,c: Lengths of sides% Output:% A: area of triangle% Usage:% Area = area(2,3,4);% Written by dfg, Oct 14, 1996.s = (a+b+c)/2;A = sqrt(s*(s-a)*(s-b)*(s-c));%%%%%%%%% end of area %%%%%%%%%%%33
The command>> help areawill produce the leading comments from thefile:Compute the area of a triangle whosesides have length a, b and c.Inputs:a,b,c: Lengths of sidesOutput:A: area of triangleUsage:Area = area(2,3,4);Written by dfg, Oct 14, 1996.To evaluate the area of a triangle with side oflength 10, 15, 20:>> Area = area(10,15,20)Area =72.6184where the result of the computation is assignedto the variable Area—the use of a capitalisedvariable name is critical here, otherwise therewould be confusion between the variable nameand the function name. If we inadvertently usea variable name that coincides with a functionname, as in>> sin = sin(pi/6)sin =0.5000>> sin(pi/2)??? Subscript indices must either bereal positive integers or logicals.Matlab now considers the name sin to refer toa variable and pi/2 in the command sin(pi/2)is interpreted as an index to a vector. To reclaimthe function name we clear the variablesin from memory with>> clear sinThe variable s used in the definition of the areafunction above is a “local variable”: its value islocal to the function and cannot be used outside:>> s??? Undefined function or variable s.If we were interested in the value of s as wellas A, then the first line of the file should bechanged tofunction [A,s] = area(a,b,c)where there are two output variables.This function can be called in several di↵erentways:1. No outputs assigned>> area(10,15,20)ans =72.6184gives only the area (first of the outputvariables from the file) assigned to ans;the second output is ignored.2. One output assigned>> Area = area(10,15,20)Area =72.6184again the second output is ignored.3. Two outputs assigned>> [Area, hlen] = area(10,15,20)Area =72.6184hlen =22.5000The previous examples illustrate the fact thata function may have a di↵erent number of outputs.It is also possible to write function filesthat accepts a variable number of inputs. Forexample, in the context of our area function,to calculate the area of a right angled triangle itis only necessary to specify the lengths of twoof the sides since the third (the hypotenuse)can be calculated by Pythagoras’s theorem. Soour amended function operates as previouslydescribed but, when only two input argumentsare supplied, it assumes the triangle to be rightangled. It does this by using the reserved variablenargin that holds the number of input arguments.The revised function, called area2,might then resemble the following code:34
- Page 1 and 2: An Introduction to MatlabVersion 3.
- Page 3 and 4: 1 MATLAB• Matlab is an interactiv
- Page 5: These are not allowable:Net-Cost, 2
- Page 8 and 9: Note that the components of x were
- Page 10 and 11: For example, if u = [10, 11, 12], v
- Page 12 and 13: a.*b -24, ans./cans =-18 -10 0 12 2
- Page 14 and 15: Colours Line Styles/symbolsy yellow
- Page 16 and 17: YData: [27 8 1]ZData:[1x0 double]Th
- Page 18 and 19: 14 Elementwise ExamplesExample 14.1
- Page 20 and 21: 0 00 0The second command illustrate
- Page 22 and 23: 9 10 11 1220 0 5 4>> J(1,1)ans =1>>
- Page 24 and 25: 15.12 Sparse MatricesMatlab has pow
- Page 26 and 27: It is well-known that the solution
- Page 28 and 29: the corresponding number, and two f
- Page 30 and 31: -2.0000 3.1416 5.0000-5.0000 -3.000
- Page 32 and 33: if logical test 1Commands to be exe
- Page 36 and 37: function [A] = area2(a,b,c)% Comput
- Page 38 and 39: In order to plot this we have to de
- Page 40 and 41: 100 2256200 4564300 3653400 6798500
- Page 42 and 43: ’string’,’lin-log’,...’po
- Page 44 and 45: Matrix analysis.cond Matrix conditi
- Page 46 and 47: imag, 42inner product, 8, 21, 22int