DOC PREVIEW
UT PSY 394U - Chapter 1 A sample MATLAB session

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 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 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PSY 394U – Do-It-Yourself Statistics 1 of 7 Chapter 1 A sample MATLAB session This tutorial assumes that you have purchased and installed the student version of MATLAB on your computer. The student version is currently cheaper than most college textbooks, and is well worth the investment. It is available directly from the Mathworks and at most university computer or bookstores. As we go through this tutorial, please try everything yourself (typing each command and seeing what it does). Remember, however, that we are mainly getting used to the look and feel of a MATLAB session, and seeing what kinds of things MATLAB can do. Some of the commands I’ll ask you to input will seem a little mysterious at present; relax, don’t worry, enjoy the test drive. Start MATLAB on your computer. You should see a window something like this: (Yours will look slightly different, but there should be a large “command window” on the right, and this is the window in which we will working in for this tutorial.) Note that the flashing cursor is next to a “>>”. This is called a “command prompt”. In this document, I will proceed all commands you should actually enter with this prompt. For example, let’s start out by using MATLAB as a calculator: >> 1+1 (always hit return or enter at the end of the line)PSY 394U – Do-It-Yourself Statistics 2 of 7 MATLAB should respond with the (hopefully) correct answer: ans = 2 An important aspect of programming languages is the ability to store values in variables, which are containers for data, similar to cells, rows, or columns in Excel, SPSS, etc. (The “ans” in the output above is actually a special variable that MATLAB creates when you don’t specify one). Store something into a variable called x: >> x = 1+1 x = 2 Note that the resulted is output back to the console. You can suppress this by ending the line with a semicolon. >> x = 1+1; This is very handy when the output of a calculation is large or, later, when you write programs that do lots of calculations. To see the value of a variable, just type the variable name: >> x x = 2 Variable names can be more than just a single letter so they can be a little more descriptive. >> nqp = 22/7 You can also make them really descriptive, but they get cumbersome to type: >> not_quite_pi = 22/7 Notice I was able to use underscores to make the variable name more readable. You can use numbers too, but avoid using anything else. Also, variable names must begin with a letter, and they are case sensitive (as is everything else in MATLAB). So X is not the same variable as x.PSY 394U – Do-It-Yourself Statistics 3 of 7 We can test the equality (or inequality) of variables. The variable “pi” is built into MATLAB, so: >> nqp == pi Note the double-equals, which means “are these equal?” Let’s see how bad the approximation is: >> pi – nqp Okay, then perhaps you can guess what this returns: >> nqp > pi (Note that command prompt is special and not part of the command per se, but the “>” you type is interpreted as “greater than”) You can see what variables you have created by listing them: >> who or you can get a little more detail with “whos”: >> whos And you can remove a variable like this: >> clear nqp Whoops, maybe we didn’t want to do that. To re-do a command, you can hit the “up arrow” on your keyboard to go back through previously entered commands. When you see the one you want, just hit return. (If you overshoot it, hit the down arrow.) Let’s clean up by removing all the variables: >> clear Don’t worry if this command, or others below, seem mysterious to you; we’re just taking MATLAB for a test drive. Variables can hold more than one number (note: a variable that holds a single value is often called a scalar, and one that holds multiple values is often called a vector): >> x = 1:5 You can also enter numbers into a variable by hand: >> y = [2, 3, 5, 8, 10]PSY 394U – Do-It-Yourself Statistics 4 of 7 which means “concatenate the numbers 2, 3, 5, 8, 10 and assign them to y”. When working with these kinds of variables, MATLAB behaves in a convenient fashion: >> x + 5 No need to add 5 to each and every element of x; MATLAB automatically interprets the command this way. If we add two vectors: >> x + y Again, the result is sensible; MATLAB adds each value of y to the corresponding value of x. Comparisons work the same: >> x > 3 >> x == y Now we can do some fun stuff: >> scatter(x, y) Looks like x and y are pretty highly correlated. Let’s see by how much: >> corrcoef(x, y) Note that this returns the entire correlation matrix but only the upper (or lower) triangle – in this case a single point - has meaning in this case. Some connecting lines might make the form of the relationship a little clearer: >> line(x, y) This looks sigmoidal (“s-shaped”) to me, but I’ll bet a straight line still does a pretty good job of describing the data. So let’s fit a linear model - y as a linear function of x - and plot it. One nice thing about MATLAB is that, even though at it’s heart it is a programming language, you can also do a lot with plots using the GUI (graphical user interface). From the menubar at the top of the figure, select Tools -> Basic Fitting. A new window similar to the one shown here should appear.PSY 394U – Do-It-Yourself Statistics 5 of 7 Now check the “linear” box, as well as “Show equations” and “Plot residuals” boxes to see the results of the fit on the plot. You can also press the large right-arrow button at the lower right of the page to see additional fitting information in this window. Visually, the fit isn’t bad, but it still looks like the data are departing from a straight line in a systematic fashion. If these were real data, I’d want to see if this was really a sigmoidal shape. Let’s clean up again: >> clear One of the great things about MATLAB is that it has lots of random number generators and statistical distributions built in. Let’s get a sample of size 100 from a normally distributed population. Here: >> x = randn(100, 1); And look at them (What do you expect the mean and standard deviation to be?): >> hist(x) This looks like a standard normal distribution in that the mean looks to be about zero, the s.d. looks to be about one, and most or all of it looks to be between +/- 3. Let’s check: >> mean(x)


View Full Document

UT PSY 394U - Chapter 1 A sample MATLAB session

Documents in this Course
Roadmap

Roadmap

6 pages

Load more
Download Chapter 1 A sample MATLAB session
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 Chapter 1 A sample MATLAB session 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 Chapter 1 A sample MATLAB session 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?