The following is a brief introduction to some *Matlab* commands that
can be used to write the computer programs in Math 373.

To start *Matlab*, type `matlab` at the unix prompt.
When you see the prompt ">>", you can start entering
*Matlab* commands. To quit *Matlab*, select **Exit Matlab**
from the **File** menu.

Help on using *Matlab* commands by clicking on **Help**
and following the links.

Matlab programs should be stored in files, called `program` at the *Matlab* prompt ">" will execute all the
statements in the program. However, before doing so, be sure that
your current directory is the one containing the file **Current Directory** and change
to that directory.

To place comments in your program, use the % sign
before any line you wish as a comment. To avoid intermediate output
produced by *Matlab*, place a semicolon at the end of any statement
whose output you do NOT want *Matlab* to display on the screen.

General Commandsdiary('filename') writes a copy of all subsequent keyboard input and the resulting output (except it does not include graphics) to the named file, where filename is the full pathname or filename is in the current MATLAB directory. If the file already exists, output is appended to the end of the file. The command diary off suspends the diary and the command diary on resumes diary mode using the current filename. format long -- arithmetic with 14 decimal places (the default is format short -- 4 decimal places). The symbols <, <=, >, >= have the obvious meanings. * is multiplication, / is division. sqrt(2) produces the square root of 2; log(2) gives the natural log of 2; exp(2) gives e^2; abs(g) produces the absolute value of the expression g; max(a,b) gives the maximium of a and b. To see the list of other elementary functions, typehelp elfunTo plot a sequence of points stored in the vectors (x,y), typeplot(x,y).Displaying output on the screenIf the variablexhas been given a value (say x=2) in a previous statement, then typingxwill result in the outputx=2. If the variablesxandyhave previously been assigned values (say 2 and 3), then typing[x,y]will result in the outputans = 2 3. To label this displayed output, theMatlabcommandsdispandsprintfmay be used. The command disp(x) displays the value of x without displaying x= first. disp('x=') can be used to display the text (enclosed in single quotes) x=. If we have defined x=1.23456 and y=2.34567, then the command disp(sprintf('x=%5.4f y=%4.3f', x,y)) will produce x=1.2346 y=2.346 Note that the text part is enclosed in single quotes, along with the format in which x and y are displayed. In this case, %5.4f means that x should be displayed as a floating point number with 5 digits, 4 of which should be to the right of the decimal point. The final x and y are the variables whose contents are displayed. See the help screen for additional details.Loopsfor loops: These have the form: for i=1:10 statements end if, else statements: These have the form if a <= b statement else statement end while loops: These have the form: while (abs(xnew-xold) > tolx ) & (N < Nmax) statements end while (abs(xnew-xold) > tolx ) | (abs(fnew) > tolf ) statements end The symbol & is the logicalandsymbol, meaning that the loop will continue if both statements are true. The symbol | is the logicalorsymbol, meaning that the loop will continue if either statement is true.Vectors and Matricesf = zeroes(10,1) -- defines f to a column vector with 10 rows and places zeroes in all entries. a = zeroes(10,10) -- defines a to be a 10 x 10 matrix and places zeroes in all the entries. To enter a row vector intoMatlab, type v = [1 2 3] or v = [1,2,3] If you wish v to be a column vector, then enter v= [1 2 3]' or v = [1; 2; 3] If v = [1 2 3], then w = [v 4] gives the row vector [1 2 3 4] To solve the linear system A x = b, where b is a row vector, type x = A/b; If b is a column vector, type x= A\b. Be careful about the direction of the slash symbol. If v is a row vector, v*v' produces a scalar, v'*v produces a matrix and v.*v produces another row vector in which the multiplication is performed separately on each component. In general, placing a period before an algebraic operation means to perform the operation on each component of the vector.Defining an inline function within a Matlab programThe command f=inline('x*y','x','y'); can be used to create the function f(x,y) = xy. Note that both the expression x*y for the function and the function arguments x and y must be enclosed in single quotes. Then typing f(2,3) will then give the value of this function at the point x=2, y=3.Defining a function in an M-fileSyntax: function [out1, out2, ...] = funname(ini, in2, ...) defines a function funname that accepts inputs in1, in2, etc. and returns outputs out1, out2, etc. Examples: function f = fcn(x) f = 3*x - exp(x); In this example, the input is a number x and the output is a number f computed by the given formula. function b = fcnv(x) b(1) = x(1) - x(1)^2 - x(2)^2; b(2) = x(2) - x(1)^2 + x(2)^2; In this example, the input x is a vector with 2 components and the output b is a vector with two components. Note that b will be returned as a row vector, when x is either a row or column vector. function A = fcnm(x) A(1,1) = 1 - 2*x(1); A(1,2) = -2*x(2); A(2,1) = -2*x(1); A(2,2) = 1 + 2*x(2); In this example, the input x is a vector with 2 components and the output A is a 2 x 2 matrix.