This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MATLAB GUIDE UMD PHYS375 FALL 2010 DIRECTORIES Find the current directory you are in: >> pwd ans = C:\Documents and Settings\ian\My Documents\MATLAB [Note that Matlab assigned this string of characters to a variable name “ans”. It does this every time it is asked to produce something without being told where to assign the value. For instance, >> 1+1 ans = 2 ] You can also assign the value to a variable of your choice: >> a=pwd a = C:\Documents and Settings\ian\My Documents\MATLAB Matlab will echo the explicit command unless you end your statement with a semicolon: >> a=pwd; All the usual MSDOS directory manipulation applies: >> cd ../../Desktop >> dir . CLASSES PROPOSALS .. PRESENTATION PROJECTS Note that Matlab is case-sensitive, and all built-in commands we will be using are lowercase. Change directories to C:\Users\p375-0X\Documents\MATLAB (“X” varies at each lab station) and make a subdirectory with your last name. You will be using this directory all semester. It is also recommended that you back up this data on a USB thumb drive.MATRICES AND ARRAYS From the Matlab “Getting Started” Guide (http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf): “In the MATLAB environment, a matrix is a rectangular array of numbers. Special meaning is sometimes attached to 1-by-1 matrices, which are scalars, and to matrices with only one row or column, which are vectors. MATLAB has other ways of storing both numeric and nonnumeric data, but in the beginning, it is usually best to think of everything as a matrix. The operations in MATLAB are designed to be as natural as possible. Where other programming languages work with numbers one at a time, MATLAB allows you to work with entire matrices quickly and easily.” You can create an array of data with square brackets: >> a=[3 5 9 6] a = 3 5 9 6 Here, it is a row vector. You can create a column vector as well: >> a=[3; 5; 9; 6] a = 3 5 9 6 Or just use the transpose operator on a row vector: >> a=[3 5 9 6]' a = 3 5 9 6 Note that it is sometimes useful to add comments to your commands. You can do this with “%”: >> a=[3 5 9 6] %here is my comment a = 3 5 9 6 At any time you can see the variables in the workspace and their properties including dimensions etc: >> whos Name Size Bytes Class Attributes a 1x4 32 double You can save this array to a delimited text file named e.g. “data.dat” >> save -ascii data.dat a You can load data from a space/tab/etc delimited file of numbers:>> load data.dat This creates an array named the same as the file, without the extension: >> whos Name Size Bytes Class Attributes a 1x4 32 double data 1x4 32 double You can also get information about a specific variable: >> length(a) ans = 4 You can concatenate these elements to other arrays: >> data=[[1 5 3 8]' data'] data = 1 3 5 5 3 9 8 6 Note that these are all logical statements, not algebraic. They can therefore be self-referential. Since this is a 2-dimensional array (4 rows and 2 columns), we can see the length is >> length(data) ans = 4 but the number of elements is >> numel(data) ans = 8 And the size reports the length of the rows and columns: >> size(data) ans = 4 2 (BTW, there exists documentation on all the functions available in MATLAB; for instance http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/refbook.pdf, which is just for functions starting with A-E and takes up >1500 pages. The other two volumes for the rest of the alphabet are refbook2.pdf and refbook3.pdf. Rather than burden you with sorting through them, we will focus on the functions of greatest utility that will be used the most in the lab.) You can address individual elements of this array (row index first, column index second): >> data(1,1)ans = 1 >> data(4,1) ans = 8 You can also use this address syntax to assign values: >> data(2,1)=18 data = 1 3 18 5 3 9 8 6 The colon “:” is extremely useful in Matlab. You can use it to define subsets of elements of arrays: >> data(1:2,1:2) ans = 1 3 5 5 Or, use it to create arrays of evenly spaced numbers. For unit spacing: >> 1:4 ans = 1 2 3 4 Or, for other spacing values e.g. >> 0:5:15 ans = 0 5 10 15 Q: How would you create an array of all odd numbers up to 99? To define simple arrays, you can use these two self-explanatory commands: >> ff=zeros(2,1) ff = 0 0 >> ff=ones(2,1) ff = 1 1To create an array of random values equally distributed between 0 and 1: >> ff=rand(2,1) ff = 0.8147 0.9058 [Note that rand, by itself, is equivalent to rand(1,1)] If you want to erase all variables from the workspace use “clear”, >> clear Then there are no available variables: >> whos >> OPERATIONS ON ARRAYS Matlab can do statistical analysis: >> a=rand(10,1); >> mean(a) % find the mean value of this array of random numbers ans = 0.5777 >> std(a) % find the standard deviation of this array of random numbers ans = 0.2985 Q: The example above is for only 10 numbers, and if you run the commands again you will get a slightly different answer. What values do the mean and standard deviation approach in the limit of a large number of random variables evenly distributed between 0 and 1? Prove that this is correct analytically. Be aware that one of the most useful MATLAB commands is “help”, e.g. >> help mean MEAN Average or mean value. For vectors, MEAN(X) is the mean value of the elements in X. For matrices, MEAN(X) is a row vector containing the mean value of each column. For N-Dimensional arrays, MEAN(X) is the mean value of the elements along the first non-singleton dimension of X. MEAN(X,DIM) takes the mean along the dimension DIM of X. Example: If X = [0 1 2 3 4 5] then mean(X,1) is [1.5 2.5 3.5] and mean(X,2) is [1 4] Class support for input


View Full Document

UMD PHYS 375 - MATLAB GUIDE

Documents in this Course
Lecture 1

Lecture 1

10 pages

Load more
Download MATLAB GUIDE
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MATLAB GUIDE and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MATLAB GUIDE 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?