0% found this document useful (0 votes)
13 views

MIT15 075JF11 Tutorial

Uploaded by

sibandzevuyani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

MIT15 075JF11 Tutorial

Uploaded by

sibandzevuyani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

M$7/$% Tutorial 1

September 16, 2010


Dimitrios Bisias

M$7/$% Desktop
The Command Window is where you type the commands or instructions into M$7/$%.
The Command History contains a record of all the commands entered in the Command Window.
The Workspace is where your data is stored in M$7/$% during the current session.

M$7/$B Fundamentals
MATLAB can be used as a calculator performing simple and complex calculations.

>> t = 2;
>> x = sin(3*t+pi/2)^2

x=

0.9219

MATLAB saves data in variables. You can think of MATLAB variables as data containers.

All variables are arrays. Even scalar variables are treated as 1x1 arrays.

There are two ways to create a sequence of equally spaces values

>> x = 1:2:11

x=

1 3 5 7 9 11

>> x = linspace(1,11,6)

x=

1 3 5 7 9 11

To transpose it

>> x =x'

x=
1
3
5
7
9
11

To check the size of an array:

>> size(x)

ans =

6 1

The semicolon can be used to suppress output in the command window

>> t = 0:0.01:100;

To create matrices

>> A = [1 2 3;4 5 6;7 8 9]

A=

1 2 3
4 5 6
7 8 9

Row separator: ; or enter

>> ones(2)

ans =

1 1
1 1

>> zeros(3,2)

ans =

0 0
0 0
0 0

>> eye(3)

ans =

1 0 0
0 1 0
0 0 1

To access the (2,3) element of an array use A(2,3)


To access the second row, use A(2,: )
What do we access by A(2,[1 2]) ?

To concatenate matrices
>> A =

1 2 3
4 5 6
7 8 9

>> B = [A A]

B=

1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9

>> B = A + 2

B=

3 4 5
6 7 8
9 10 11

>> B = 3*A

B=

3 6 9
12 15 18
21 24 27

What’s the difference between A*C and A.*C?


Script Files
A collection of commands that you would execute in the Command Window.
Automate repetitive tasks instead of typing the same commands over and over again.

Writing Functions

function output = function_name(input)


The name of the file has to be the same as the name of the function.
Make repetitive tasks more modular and the code more reusable.
They have a separate workspace which is automatically cleared on exit of the function.

Flow and Loop Control

If-Elseif-Else
While. Run multiple iterations of an operation until a condition is no valid
For. Repeat an operation for a known number of iterations.

x = 0;
for i=1:10
x = x+i;
end

z = 1:10;
x = sum(z)

Probability/Statistics Built-in functions


rand
randn
mean
median
std
var
quantile
prctile
normpdf
normcdf
norminv
v= 1:20;

Suppose we wanted to randomly choose ten elements of v.

>> v = 1:20;
>> vsample10 = randsample(v,10)

vsample10 =

14 4 16 17 12 15 20 10 11 3

Compute the mean and standard deviation of this sample.

>> mean(vsample10)

ans =

12.2000
>> std(vsample10)

ans =

5.4528

We can also compute quantiles

>> quantile(vsample10,0.5)

ans =

13

>> median(vsample10)

ans =

13

Let’s generate ten random numbers from the normal distribution, say with mean 5 and variance 4.

>>r =5 + 2.*randn(10,1)

We can compute the cdf with normcdf

>> normcdf(5,5,2)
ans =

0.5000

There are several related functions for other probability distributions, for example:
Binomial – binornd, binopdf
Poisson – poissrnd, poisspdf
Uniform – unifrnd, unifpdf
Gamma – exprnd, exppdf

Help

There may be times when you forget how to use a certain function, even though you remember
the function’s name. In this case, simply type help function_name

>> help norminv

For documentation you can type doc


MIT OpenCourseWare
http://ocw.mit.edu

15.075J / ESD.07J Statistical Thinking and Data Analysis


Fall 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like