Matlab commands -- Assignment 1

The following is a brief introduction to some Matlab commands that can be used to write the programs in Assignment 1.

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 M-files, which end in the extension ".m" If your program is stored in the file program.m, then typing 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 program.m. If not, click on the tab labeled 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.

Matlab Commands:

format long -- arithmetic with 14 decimal places (the default is
format short -- 4 decimal places).

for 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 norm(u, inf) -- computes maximum of the absolute values of the components of the vector u. norm(u,1) -- computes the L_1 norm of the vector u, i.e, the sum of the absolute values of the components of u. norm(u, 'fro') -- computes the L_2 norm of the vector u, i.e the square root of the sum of the squares of the components of u. To enter a row vector into Matlab, 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, type x = A\b; Be careful about the direction of the slash symbol. The symbols <, <=, >, >= have the obvious meanings. * is multiplication, / is division. 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. sqrt(2) produces the square root of 2; log(2) gives the natural log of 2 To see the list of other elementary functions, type help elfun To plot a sequence of points stored in the vectors (x,y), type plot(x,y). Note that for Assignment 1, the approximate solution will be stored in a column vector u of length n-1, where n is the number of subintervals. If you wish to plot the solution, this may be done as follows. upic = [0; u; 0] % this adds the values of u at the endpoints of the interval, which are not included in the solution vector The coordinates of the corresponing x values can be obtained by using a for loop, or more simply by the statement. xpic = 0:1/n:1 % this produces a vector [0, 1/n, 2/n, ... , 1] Then plot(xpic,upic) produces the plot.