Docsity

Getting Started with Matlab for Math 313: A Comprehensive Guide, Study notes of Mathematics

Toggle sidebar
bg1
Introduction to Matlab for Math 313
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:
- The main window on the left is called the Command Window, and this is
where math operations, function calls, variable creation, and much more can
be done.
- The window in the upper left is your Current Directory or Workspace
depending on which tab at the bottom of this window is selected. The Current
Directory is usually the work subfolder contained in the Matlab folder and it
shows what your work directory contains such as data files, M-files, and
figure files. The Workspace lists all your current objects/variables that have
been created.
- The last window on the bottom left is the Command History Window. This
window remembers all the commands that you have typed into the Command
Window.
How do you use Matlab: The best way to learn Matlab is to use it. Let’s start with
something simple.
- Type help <command> at the Matlab command prompt to see useful
information about certain Matlab commands.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Getting Started with Matlab for Math 313: A Comprehensive Guide and more Study notes Mathematics in PDF only on Docsity!

Introduction to Matlab for Math 313

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:

  • The main window on the left is called the Command Window, and this is where math operations, function calls, variable creation, and much more can be done.
  • The window in the upper left is your Current Directory or Workspace depending on which tab at the bottom of this window is selected. The Current Directory is usually the work subfolder contained in the Matlab folder and it shows what your work directory contains such as data files, M-files, and figure files. The Workspace lists all your current objects/variables that have been created.
  • The last window on the bottom left is the Command History Window. This window remembers all the commands that you have typed into the Command Window. How do you use Matlab: The best way to learn Matlab is to use it. Let’s start with something simple.
  • Type help at the Matlab command prompt to see useful information about certain Matlab commands.

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.

  • Let’s make a simple row vector. We use [] with numbers separated by spaces or commas to distinguish entries in a row. EDU>> X = [1 2 3] X = 1 2 3
  • To make a column vector we separate the entries by semicolons. We also can use a semicolon at the end of a statement to suppress output. EDU>> Y = [3;4;5]; EDU>> Y Y = 3 4 5
  • Now lets make a matrix using the zeros command which we got help information on earlier. EDU>> M = zeros(3) M = 0 0 0 0 0 0 0 0 0
  • Other useful matrix creation commands are: eye, diag, and ones. We can edit a specific entry of M by indexing it. EDU>> M(1,1)=1; M(2,2)=1; M(3,3)=1; M M = 1 0 0 0 1 0 0 0 1
  • The size command is useful to get a matrix or vector size when one is unknown. EDU>> size(X) ans = 1 3 EDU>> size(M) ans = 3 3 But when is the size of a matrix or vector unknown to you? Well, Matlab allows you to write simple programs that take arguments in the form of matrices, vectors and other things (like strings such as a file name). You may want to write a program which takes any size matrix and swaps the last two rows. To do this we need to make an M-file.

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:

  1. Create the following matrices: H = 5 0 0 0 5 0 0 0 5 K = 1 2 3 4 5 6 L = 1 4 2 5 3 6
  2. Compute G = KHL
  3. Compute J = G^(-1) (The inverse of an n x n matrix A (if it exists) is the n x n matrix A-1^ with A*A-1^ = I).
  4. Compute GJ and JG
  5. Compute K + L’. What happens if you try to compute K + L?
  6. Write an M-file that uses a for loop with function and file name “even” (file will be even.m and the function name will be even(n)) that prints the even numbers between 1 and n, where n is a positive integer input (you do not need to check that n is a positive integer). (Use help to see how to use a for loop).
  7. Test your M-file with n = 10 and 11. Record all your output (problems 1,2,3,4,5,7) in a dairy file labeled yourname1.txt (example WilBrady1.txt). Turn in a printout of your dairy file and a printout of your M- file. You should staple everything together and leave it in the top box on the door outside 4C13. Please be sure that your name is on everything you hand in! This assignment is due by 4pm on Thursday, February 2, and forms a part of Homework Set 3.