




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





What does Matlab do: Matlab is very powerful math computation software. Matlab can solve very complicated systems of equations, plot 3-D images and much more. Matlab is one of the best programs for working with matrices/vectors, and for writing simple programs that do very difficult math computations. For this class, we will be working a lot with matrices and operations on matrices; hence Matlab will be a very useful tool. What you see: After opening Matlab we see three windows:
EDU>> help zeros ZEROS Zeros array. ZEROS(N) is an N-by-N matrix of zeros. ZEROS(M,N) or ZEROS([M,N]) is an M-by-N matrix of zeros. ZEROS(M,N,P,...) or ZEROS([M N P ...]) is an M-by-N-by-P-by-... array of zeros. ZEROS(SIZE(A)) is the same size as A and all zeros. ZEROS with no arguments is the scalar 0. ZEROS(M,N,...,CLASSNAME) or ZEROS([M,N,...],CLASSNAME) is an M-by-N-by-... array of zeros of class CLASSNAME. Example: x = zeros(2,3,'int8'); See also eye, ones.
To make an M-file go to file->new->M-file or click the little piece of paper in the upper left hand corner. Now we need to name our program/function, label our output, and label any variable inputs. If we need to add a comment in our code we use the % symbol. See the sample M-file code for swapping the last two rows of a matrix. Swap.m function s = swap(N) %This function swaps that last two rows of input matrix N. If input %matrix N has only one row then we do nothing. %First we need to get the number of roows of N [rows, columns] = size(N); %Let's assign our output and then manipulate it s = N; %Now we check if it has at least two rows if rows > 1 %If we are here then N has more then 1 row so we swap the last two. %Let’s save the last and second to last rows lastRow = s(rows,:); secondLastRow = s(rows-1,:); %Now replace the last two rows in N s(rows,:) = secondLastRow; s(rows-1,:) = lastRow; %end the if statement end %our output will be s, which is N but with the last two rows now swapped. Any command above that you do not know, try it in the command window on your own to see what is does. Let’s see if swap works. EDU>> swap(M) ans = 1 0 0 0 0 1 0 1 0
And the text file sample.txt now contains: N = diag([1 2 3 4 5]) N = 1 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 4 0 0 0 0 0 5 swap(N) ans = 1 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 0 5 0 0 0 4 0 diary off Matlab can do a lot more too. We can write programs that use “for” loops or “while” loops. We can add, subtract and multiply matrices and vectors, and much more. Helpful web references: http://www.mathworks.com/academia/student_center/tutorials/launchpad.html http://www.math.ufl.edu/help/matlab-tutorial/
Your assignment: