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, type quit.

If you type help, you will get a list of topics. Picking one of the topics, e.g., matfun and typing help matfun will give you a list of subtopics in that category. Picking, for example, eigs, and typing help eigs will give you information on the program eigs for finding a few eigenvalues and eigenvectors.

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.

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. a = zeroes(10,10) -- defines a to be a 10 x 10 matrix and places zeroes in all the entries. f = zeroes(10,1) -- defines f to a column vector with 10 rows and places zeroes in all entries. 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.