Thursday, 17 March 2011

Miscellaneous Programming Items

Miscellaneous Programming Items

It is possible to place a matrix valued function as the condition of a branching construction or a while loop. Thus the condition might be a matrix like ones(2),zeros(2), or eye(2). How would a construction like
if <condition>, < program1>, 
else <program2>, end

behave if condition=eye(2)? The program1 will execute if all of the entries of condition are not 0. Thus if condition=magic(2), program1 will execute while if condition=eye(2) control will pass to the "else" part and program2 will execute.
A problematic construction occurs when you have

if A ~= B, <program>, end.

You would like program to execute if the matrices A and B differ on some entry. Under the convention, program will only execute when they differ on all entries. There are various ways around this. One is the construction

if A ==B  else <program>, end

which will pass control to the "else" part if A and B differ on at least one entry. Another is to convert A==B into a binary valued function by using all(all(A==B)). The inside all creates a binary vector whose i--th entry is 1 only if the i--th column of A is the same as the i--th column of B. The outside all produces a 1 if all the entries of the vector are 1. Thus if A and B differ on at least one entry, then all(all(A==B))=0. The construction

if ~ all(all(A==B)), <program>, end

then behaves in the desired way.

Essentially, the same convention holds for the while construction.
while <condition>, <program>, end.

The program program will execute successively as long as every entry in condition is not 0, and the control passes out of the loop when at least one entry of condition is 0.
Another problem occurs when you have a conjunction of conditions, as in

if  <condition1> & < condition2>, 
<program>,  end

Of course, program will execute if both condition1 and condition2 are nonzero. Suppose that condition1=0 and condition2 causes an error message. This might happen for
i<=m &  A(i,j)==0 
where m is the number of columns of A. If i>m, then you would like to pass the control, but since A(i,j) makes no sense if i>m an error message will be dished up. Here you can nest the conditions.

if i<=m,
   if A(i,j)==0,
      <program>
   end
end

Scripts

A script is an m-file without the function declaration at the top. A script behaves differently. When you type who you are given a list of the variables which are in force during the current session. Suppose that x is one of those variables. When you write a program using a function file and you use the variable x inside the program, the program will not use the value of x from your session (unless x was one of the input values in the function), rather x will have the value appropriate to the program. Furthermore, unless you declare a new value for x, the program will not change the value of x from the session. This is very convenient since it means that you do not have to worry too much about the session variables while your program is running. All this has happened because of the function declaration. If you do not make that function declaration, then the variables in your session can be altered. Sometimes this is quite useful, but I usually recommend that you use function files.

Suggestions

These are a few pointers about programming and programming in MATLAB in particular.
1) I urge you to use the indented style that you have seen in the above programs. It makes the programs easier to read, the program syntax is easier to check, and it forces you to think in terms of building your programs in blocks.
2) Put lots of comments in your program to tell the reader in plain English what is going on. Some day that reader will be you, and you will wonder what you did.
3) Put error messages in your programs like the ones above. As you go through this manual, your programs will build on each other. Error messages will help you debug future programs.
4) Always structure your output as if it will be the input of another function. For example, if your program has "yes-no" type output, do not have it return the words "yes" and "no," rather return 1 or 0, so that it can be used as a condition for a branch or while loop construction in the future.
5) In MATLAB, try to avoid loops in your programs. MATLAB is optimized to run the built-in functions. For a comparison, see how much faster A*B is over mult(A,B). You will be amazed at how much economy can be achieved with MATLAB functions.
6) If you are having trouble writing a program, get a small part of it running and try to build on that. With reference to 5), write the program first with loops, if necessary, then go back and improve it.

MATLAB demonstrations

Matlab is shipped with a number of demonstration programs. Use help demos to find out more about these (the number of demos will depend upon the version of Matlab you have).
Some of the standard demos may be especially useful to users who are beginners in linear algebra:
  • demo - Demonstrate some of MATLAB's capabilities.
  • matdemo - Introduction to matrix computation in MATLAB.
  • rrefmovie - Computation of Reduced Row Echelon Form

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...