Kirchner - Matlab Demo

50
MATLAB Tutorial Getting Started with Calculations, Graphing and Programming Nicholas R. Kirchner University of Minnesota Thursday, August 30, 2012

Transcript of Kirchner - Matlab Demo

Page 1: Kirchner - Matlab Demo

MATLAB TutorialGetting Started with Calculations, Graphing and Programming

Nicholas R. Kirchner

University of Minnesota

Thursday, August 30, 2012

Page 2: Kirchner - Matlab Demo

Outline

1 MATLAB installation

2 The MATLAB UI

3 Calculations with MATLABStandard Calculations and VariablesMatrices and Vectors

4 Graphing

5 ProgrammingFunctionsScripts

6 Yahoo! Finance

NRK (University of Minnesota) MATLAB 2012.08.30 2 / 28

Page 3: Kirchner - Matlab Demo

Outline

1 MATLAB installation

2 The MATLAB UI

3 Calculations with MATLABStandard Calculations and VariablesMatrices and Vectors

4 Graphing

5 ProgrammingFunctionsScripts

6 Yahoo! Finance

NRK (University of Minnesota) MATLAB 2012.08.30 2 / 28

Page 4: Kirchner - Matlab Demo

Outline

1 MATLAB installation

2 The MATLAB UI

3 Calculations with MATLABStandard Calculations and VariablesMatrices and Vectors

4 Graphing

5 ProgrammingFunctionsScripts

6 Yahoo! Finance

NRK (University of Minnesota) MATLAB 2012.08.30 2 / 28

Page 5: Kirchner - Matlab Demo

Outline

1 MATLAB installation

2 The MATLAB UI

3 Calculations with MATLABStandard Calculations and VariablesMatrices and Vectors

4 Graphing

5 ProgrammingFunctionsScripts

6 Yahoo! Finance

NRK (University of Minnesota) MATLAB 2012.08.30 2 / 28

Page 6: Kirchner - Matlab Demo

Outline

1 MATLAB installation

2 The MATLAB UI

3 Calculations with MATLABStandard Calculations and VariablesMatrices and Vectors

4 Graphing

5 ProgrammingFunctionsScripts

6 Yahoo! Finance

NRK (University of Minnesota) MATLAB 2012.08.30 2 / 28

Page 7: Kirchner - Matlab Demo

Outline

1 MATLAB installation

2 The MATLAB UI

3 Calculations with MATLABStandard Calculations and VariablesMatrices and Vectors

4 Graphing

5 ProgrammingFunctionsScripts

6 Yahoo! Finance

NRK (University of Minnesota) MATLAB 2012.08.30 2 / 28

Page 8: Kirchner - Matlab Demo

Installing MATLAB

1 As a MFM student, you pay the CSE Labs fee, and are thereforeentitled to a free MATLAB license for your personal computer.

2 Visit https://wwws.cs.umn.edu/account-management/ and openyour CSE Labs account.

3 Now, visit https://wwws.cs.umn.edu/matlab/student/, log in,and follow the steps for downloading MATLAB.

NRK (University of Minnesota) MATLAB 2012.08.30 3 / 28

Page 9: Kirchner - Matlab Demo

Installing MATLAB (cont.)

(Place holder slide)

NRK (University of Minnesota) MATLAB 2012.08.30 4 / 28

Page 10: Kirchner - Matlab Demo

MATLAB window

1 Command Window. Input commands (2+8), get output (ans = 10).

2 Current Folder. Contents of current directory.

NRK (University of Minnesota) MATLAB 2012.08.30 5 / 28

Page 11: Kirchner - Matlab Demo

MATLAB window (cont.)

3 Workspace. Lists your variables and functions as you define them.

4 Command History. Shows previous commands.

NRK (University of Minnesota) MATLAB 2012.08.30 6 / 28

Page 12: Kirchner - Matlab Demo

Math on Real Numbers

Arithmetic in MATLAB works roughly the way you’d expect it to, in thesense that the operations +, -, *, /,ˆ and paraentheses obey the usualorder of operations.

Expression to compute Input to MATLAB Output

2(6−10)2

+ 4 2/(6-10)^2 + 4 ans=4.1250

ln(34) log(3^4) ans=4.3944

e5 exp(5) ans=148.4132

log10(34) log10(3^4) ans=1.9085

3e0.6 − 4

3*exp(0.6)-4 ans=1.4664

Note that in MATLAB, log(x) means the natural logarithm (base e).

NRK (University of Minnesota) MATLAB 2012.08.30 7 / 28

Page 13: Kirchner - Matlab Demo

Math on Real Numbers

Arithmetic in MATLAB works roughly the way you’d expect it to, in thesense that the operations +, -, *, /,ˆ and paraentheses obey the usualorder of operations.

Expression to compute Input to MATLAB Output

2(6−10)2

+ 4 2/(6-10)^2 + 4 ans=4.1250

ln(34) log(3^4) ans=4.3944

e5 exp(5) ans=148.4132

log10(34) log10(3^4) ans=1.9085

3e0.6 − 4 3*exp(0.6)-4 ans=1.4664

Note that in MATLAB, log(x) means the natural logarithm (base e).

NRK (University of Minnesota) MATLAB 2012.08.30 7 / 28

Page 14: Kirchner - Matlab Demo

Variables

MATLAB has been reporting its results in the form ans = ....

ans is a variable. For example, the input 2*21 sets ans to 42.

To add three to the previous result, we can issue the commandans+3. MATLAB will look up the value of ans and add 3 to it.

The output ans = 45 indicates that the variable ans has now beengiven the value 45.

A variable is a place MATLAB sets aside in memory to hold a value. Wecan make our own.

NRK (University of Minnesota) MATLAB 2012.08.30 8 / 28

Page 15: Kirchner - Matlab Demo

Variables

MATLAB has been reporting its results in the form ans = ....

ans is a variable. For example, the input 2*21 sets ans to 42.

To add three to the previous result, we can issue the commandans+3. MATLAB will look up the value of ans and add 3 to it.

The output ans = 45 indicates that the variable ans has now beengiven the value 45.

A variable is a place MATLAB sets aside in memory to hold a value.

Wecan make our own.

NRK (University of Minnesota) MATLAB 2012.08.30 8 / 28

Page 16: Kirchner - Matlab Demo

Variables

MATLAB has been reporting its results in the form ans = ....

ans is a variable. For example, the input 2*21 sets ans to 42.

To add three to the previous result, we can issue the commandans+3. MATLAB will look up the value of ans and add 3 to it.

The output ans = 45 indicates that the variable ans has now beengiven the value 45.

A variable is a place MATLAB sets aside in memory to hold a value. Wecan make our own.

NRK (University of Minnesota) MATLAB 2012.08.30 8 / 28

Page 17: Kirchner - Matlab Demo

Variables (cont.)

Issuing the command fred = log(42) defines a new variable namedfred and gives it the value 3.7377.

The command 5*fred then multiplies 5 by whatever fred is. In thiscase, the output is 18.6883.

MATLAB shows you a list of values in the Workspace.

Variable names are case sensitive. The commands a=2 and A=7 definetwo different variables.

NRK (University of Minnesota) MATLAB 2012.08.30 9 / 28

Page 18: Kirchner - Matlab Demo

Matrices and Vectors, Definitions

MATLAB is short for MATrix LABoratory. It was built for high-speedmatrix calculations. We will define the following

A =

1 2 34 5 67 8 9

,B =

2 0 4−3 5 1−2 1 0

,C =[−4 2 8

],D =

126−7

Issue the following commands:

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

>> B = [2,0,4;-3,5,1;-2,1,0]

>> C = [-4,2,8]

>> D = [12;6;-7]

NRK (University of Minnesota) MATLAB 2012.08.30 10 / 28

Page 19: Kirchner - Matlab Demo

Matrices and Vectors, Definitions

MATLAB is short for MATrix LABoratory. It was built for high-speedmatrix calculations. We will define the following

A =

1 2 34 5 67 8 9

,B =

2 0 4−3 5 1−2 1 0

,C =[−4 2 8

],D =

126−7

Issue the following commands:

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

>> B = [2,0,4;-3,5,1;-2,1,0]

>> C = [-4,2,8]

>> D = [12;6;-7]

NRK (University of Minnesota) MATLAB 2012.08.30 10 / 28

Page 20: Kirchner - Matlab Demo

Matrices and Vectors, Standard Operations

Issue the commands A*B and B*A. What do you notice?

Try the commands A*C and A*D.

Compute the matrix inverse of B, using the inv command.

Compute the determinant of A.

Compute B−1D.

Define E as below, without using E = [7,0,0;0,7,0;0,0,7]. Getacquainted with the help menu if you need to.

E =

7 0 00 7 00 0 7

NRK (University of Minnesota) MATLAB 2012.08.30 11 / 28

Page 21: Kirchner - Matlab Demo

Matrices and Vectors, Editing ComponentsYou can edit a matrix variable by double-clicking that variable name in theWorkspace. This brings up a spreadsheet-like interface to edit the matrix.

In the Command Window, issue the following commands:

>> A(3,2)

>> A(3,2) = 37

>> A(3,:)

>> A(:,2)

>> A(:,2) = [10;11;12]

What did they do?NRK (University of Minnesota) MATLAB 2012.08.30 12 / 28

Page 22: Kirchner - Matlab Demo

Matrices and Vectors, Elementwise Operations

Often, we want to do the same thing to each component of a matrix.MATLAB makes this straightforward. Issue the command log(D) to takethe natural log of each element of D.

Exercise: Multiply each element of C by −3, and add 9

MATLAB interprets * and ^ as matrix multiplication and exponentiation.For elementwise operations:

Multiplying C and D elementwise is done with the command C .* D.

Squaring each element of C is done with the command C .^ 2.

Exercise: Find the dot product of C and D.

Exercise: Compare A * B and A .* B.

Exercise: Compare B ^ 3 and B .^ 3.

NRK (University of Minnesota) MATLAB 2012.08.30 13 / 28

Page 23: Kirchner - Matlab Demo

Matrices and Vectors, Elementwise Operations

Often, we want to do the same thing to each component of a matrix.MATLAB makes this straightforward. Issue the command log(D) to takethe natural log of each element of D.

Exercise: Multiply each element of C by −3, and add 9

MATLAB interprets * and ^ as matrix multiplication and exponentiation.For elementwise operations:

Multiplying C and D elementwise is done with the command C .* D.

Squaring each element of C is done with the command C .^ 2.

Exercise: Find the dot product of C and D.

Exercise: Compare A * B and A .* B.

Exercise: Compare B ^ 3 and B .^ 3.

NRK (University of Minnesota) MATLAB 2012.08.30 13 / 28

Page 24: Kirchner - Matlab Demo

Graphing Data

MATLAB draws graphs by taking two vectors, one for the independentvariable and one for the dependent variables, and plotting thecorresponding points. Try the following sequence of commands:

>> x = [1,2,3,4,5,6]

>> y = [2,5,4,4,7,11]

>> plot(x,y,’--rs’)

Exercise: Try omitting various parts of the ’–rs’. Can you make your graphuse green squares and no lines?

NRK (University of Minnesota) MATLAB 2012.08.30 14 / 28

Page 25: Kirchner - Matlab Demo

Graphing Data

MATLAB draws graphs by taking two vectors, one for the independentvariable and one for the dependent variables, and plotting thecorresponding points. Try the following sequence of commands:

>> x = [1,2,3,4,5,6]

>> y = [2,5,4,4,7,11]

>> plot(x,y,’--rs’)

Exercise: Try omitting various parts of the ’–rs’. Can you make your graphuse green squares and no lines?

NRK (University of Minnesota) MATLAB 2012.08.30 14 / 28

Page 26: Kirchner - Matlab Demo

Graphing Functions

Let’s make a graph of y = sin(x2) on the interval −5 ≤ x ≤ 5.

First create an x vector: x = [-5:0.1:5]. What did this commanddo?

plot(x,sin(x.^2)) gives us the graph. Notice the elementwiseexponentiation. (Exercise: replace x.^2 with x^2, and understandthe cause of the error.)

Exercise: Plot f (x) = −x2 + 7x − 6 on −1 ≤ x ≤ 8. Make it a blackdotted line.

NRK (University of Minnesota) MATLAB 2012.08.30 15 / 28

Page 27: Kirchner - Matlab Demo

Graphing Functions

Let’s make a graph of y = sin(x2) on the interval −5 ≤ x ≤ 5.

First create an x vector: x = [-5:0.1:5]. What did this commanddo?

plot(x,sin(x.^2)) gives us the graph. Notice the elementwiseexponentiation. (Exercise: replace x.^2 with x^2, and understandthe cause of the error.)

Exercise: Plot f (x) = −x2 + 7x − 6 on −1 ≤ x ≤ 8. Make it a blackdotted line.

NRK (University of Minnesota) MATLAB 2012.08.30 15 / 28

Page 28: Kirchner - Matlab Demo

Function basicsRecall the quadratic formula: ax2 + bx + c = 0 has the solutions

x =−b ±

√b2 − 4ac

2a

We will define a function to solve the quadratic equation. It will takeinputs a, b, c , and will output the results of the above formula.

How to doit:

Go to File → New → Function

Type in the following:

function [positive,negative]=quadratic(a,b,c)

%Solves a quadratic equation using the quadratic formula

discriminant = b^2-4*a*c;

positive = (-b + sqrt(discriminant))/(2*a);

negative = (-b - sqrt(discriminant))/(2*a);

end

NRK (University of Minnesota) MATLAB 2012.08.30 16 / 28

Page 29: Kirchner - Matlab Demo

Function basicsRecall the quadratic formula: ax2 + bx + c = 0 has the solutions

x =−b ±

√b2 − 4ac

2a

We will define a function to solve the quadratic equation. It will takeinputs a, b, c , and will output the results of the above formula. How to doit:

Go to File → New → Function

Type in the following:

function [positive,negative]=quadratic(a,b,c)

%Solves a quadratic equation using the quadratic formula

discriminant = b^2-4*a*c;

positive = (-b + sqrt(discriminant))/(2*a);

negative = (-b - sqrt(discriminant))/(2*a);

end

NRK (University of Minnesota) MATLAB 2012.08.30 16 / 28

Page 30: Kirchner - Matlab Demo

Function basics (cont.)

Save the file as “quadratic.m” The filename must match the functionname.

Close the editor window, and test your function on x2 − 7x + 6 bytyping [x1,x2]=quadratic(1,-7,6) Your answers should bex1 = 6 and x2 = 1.

Explanation: Line 1 tells MATLAB this function takes three inputsa,b,c and will output two values positive, negative.

So, we fulfill our promise to MATLAB by defining positive andnegative within the function.

The semicolons at the end of each line tell MATLAB to suppressoutput. Exercise: take them out to see what happens.

MATLAB ignores anything after a % sign. Use this feature to writenotes to yourself.

NRK (University of Minnesota) MATLAB 2012.08.30 17 / 28

Page 31: Kirchner - Matlab Demo

Function basics (cont.)

Save the file as “quadratic.m” The filename must match the functionname.

Close the editor window, and test your function on x2 − 7x + 6 bytyping [x1,x2]=quadratic(1,-7,6) Your answers should bex1 = 6 and x2 = 1.

Explanation: Line 1 tells MATLAB this function takes three inputsa,b,c and will output two values positive, negative.

So, we fulfill our promise to MATLAB by defining positive andnegative within the function.

The semicolons at the end of each line tell MATLAB to suppressoutput. Exercise: take them out to see what happens.

MATLAB ignores anything after a % sign. Use this feature to writenotes to yourself.

NRK (University of Minnesota) MATLAB 2012.08.30 17 / 28

Page 32: Kirchner - Matlab Demo

Write Your Own Function

Exercise: Recall that the present value P of an annuity is

P = R

(1− (1 + i)−n

i

)Where R is the payment per period, i is the interest rate per period, and nis the number of periods.Write a function called annuity_value to compute this.

Check that your function works: A 10 year annuity at 5% annual interestwith payments of $500 has a present value of $3860.87.

NRK (University of Minnesota) MATLAB 2012.08.30 18 / 28

Page 33: Kirchner - Matlab Demo

Write Your Own Function

Exercise: Recall that the present value P of an annuity is

P = R

(1− (1 + i)−n

i

)Where R is the payment per period, i is the interest rate per period, and nis the number of periods.Write a function called annuity_value to compute this.

Check that your function works: A 10 year annuity at 5% annual interestwith payments of $500 has a present value of $3860.87.

NRK (University of Minnesota) MATLAB 2012.08.30 18 / 28

Page 34: Kirchner - Matlab Demo

Anonymous Functions

Functions don’t need to be saved to files. We can create them on thefly, if needed.

For example, finding the mean of the elements of a vector: we wantto sum the elements, and divide by the length.

Issue the command mean = @(x) sum(x)/length(x);

“mean =” tells MATLAB to store the value (function, in this case) tothe variable mean.

“@(x)” indicates to MATLAB that you are defining a function whichwill take one argument called x.

“sum(x)/length(x);” tells MATLAB to take that x, calculate itssum and length, and divide them. Oh yeah... and suppress the output.

The whole expression “@(x) sum(x)/length(x);” actually is thefunction. It does not have a name until we assign it to mean.

NRK (University of Minnesota) MATLAB 2012.08.30 19 / 28

Page 35: Kirchner - Matlab Demo

Anonymous Functions

Functions don’t need to be saved to files. We can create them on thefly, if needed.

For example, finding the mean of the elements of a vector: we wantto sum the elements, and divide by the length.

Issue the command mean = @(x) sum(x)/length(x);

“mean =” tells MATLAB to store the value (function, in this case) tothe variable mean.

“@(x)” indicates to MATLAB that you are defining a function whichwill take one argument called x.

“sum(x)/length(x);” tells MATLAB to take that x, calculate itssum and length, and divide them. Oh yeah... and suppress the output.

The whole expression “@(x) sum(x)/length(x);” actually is thefunction. It does not have a name until we assign it to mean.

NRK (University of Minnesota) MATLAB 2012.08.30 19 / 28

Page 36: Kirchner - Matlab Demo

Anonymous Functions

Functions don’t need to be saved to files. We can create them on thefly, if needed.

For example, finding the mean of the elements of a vector: we wantto sum the elements, and divide by the length.

Issue the command mean = @(x) sum(x)/length(x);

“mean =” tells MATLAB to store the value (function, in this case) tothe variable mean.

“@(x)” indicates to MATLAB that you are defining a function whichwill take one argument called x.

“sum(x)/length(x);” tells MATLAB to take that x, calculate itssum and length, and divide them. Oh yeah... and suppress the output.

The whole expression “@(x) sum(x)/length(x);” actually is thefunction. It does not have a name until we assign it to mean.

NRK (University of Minnesota) MATLAB 2012.08.30 19 / 28

Page 37: Kirchner - Matlab Demo

Anonymous Functions

Functions don’t need to be saved to files. We can create them on thefly, if needed.

For example, finding the mean of the elements of a vector: we wantto sum the elements, and divide by the length.

Issue the command mean = @(x) sum(x)/length(x);

“mean =” tells MATLAB to store the value (function, in this case) tothe variable mean.

“@(x)” indicates to MATLAB that you are defining a function whichwill take one argument called x.

“sum(x)/length(x);” tells MATLAB to take that x, calculate itssum and length, and divide them. Oh yeah... and suppress the output.

The whole expression “@(x) sum(x)/length(x);” actually is thefunction. It does not have a name until we assign it to mean.

NRK (University of Minnesota) MATLAB 2012.08.30 19 / 28

Page 38: Kirchner - Matlab Demo

Anonymous Functions

Functions don’t need to be saved to files. We can create them on thefly, if needed.

For example, finding the mean of the elements of a vector: we wantto sum the elements, and divide by the length.

Issue the command mean = @(x) sum(x)/length(x);

“mean =” tells MATLAB to store the value (function, in this case) tothe variable mean.

“@(x)” indicates to MATLAB that you are defining a function whichwill take one argument called x.

“sum(x)/length(x);” tells MATLAB to take that x, calculate itssum and length, and divide them. Oh yeah... and suppress the output.

The whole expression “@(x) sum(x)/length(x);” actually is thefunction. It does not have a name until we assign it to mean.

NRK (University of Minnesota) MATLAB 2012.08.30 19 / 28

Page 39: Kirchner - Matlab Demo

Anonymous Functions (cont.)

Now try the command mean([2,4,6]). Did you get 4?

Try the command average = mean and use the newly namedaverage function to calculate the average of some vector.

Exercise: Write an anonymous function for present value of anannuity. It will take three arguments.

P = R

(1− (1 + i)−n

i

)

NRK (University of Minnesota) MATLAB 2012.08.30 20 / 28

Page 40: Kirchner - Matlab Demo

Anonymous Functions (cont.)

Now try the command mean([2,4,6]). Did you get 4?

Try the command average = mean and use the newly namedaverage function to calculate the average of some vector.

Exercise: Write an anonymous function for present value of anannuity. It will take three arguments.

P = R

(1− (1 + i)−n

i

)

NRK (University of Minnesota) MATLAB 2012.08.30 20 / 28

Page 41: Kirchner - Matlab Demo

Anonymous Functions (cont.)

Now try the command mean([2,4,6]). Did you get 4?

Try the command average = mean and use the newly namedaverage function to calculate the average of some vector.

Exercise: Write an anonymous function for present value of anannuity. It will take three arguments.

P = R

(1− (1 + i)−n

i

)

NRK (University of Minnesota) MATLAB 2012.08.30 20 / 28

Page 42: Kirchner - Matlab Demo

Working with Excel

Downloadhttp://www.math.umn.edu/~kirc0076/Orientation.xls

This file is stock price data from Exxon and Chevron from 2009-2011obtained from Yahoo! Finance.

In MATLAB, type[num,txt,raw] = xlsread(’Z:\Orientation.xls’)

(Replace the pathname with whereever you put yours).

Scroll up to see what num, txt, and raw are.

NRK (University of Minnesota) MATLAB 2012.08.30 21 / 28

Page 43: Kirchner - Matlab Demo

Working with Excel

Downloadhttp://www.math.umn.edu/~kirc0076/Orientation.xls

This file is stock price data from Exxon and Chevron from 2009-2011obtained from Yahoo! Finance.

In MATLAB, type[num,txt,raw] = xlsread(’Z:\Orientation.xls’)

(Replace the pathname with whereever you put yours).

Scroll up to see what num, txt, and raw are.

NRK (University of Minnesota) MATLAB 2012.08.30 21 / 28

Page 44: Kirchner - Matlab Demo

Plotting the Data

We are going to plot this data and determine the correlation coefficientbetween the two stocks. Hypothesis: Since Exxon and Chevron are in thesame business (oil), the correlation coefficient will be positive (i.e. aChevron gain will correspond with an Exxon gain since they’re bothsubject to the whims of the oil market).

Since num holds the stock prices, create vectors of them with

>> exxon = num(:,1)

>> chevron = num(:,2)

Now, plot with plot(exxon,chevron,’s’).

NRK (University of Minnesota) MATLAB 2012.08.30 22 / 28

Page 45: Kirchner - Matlab Demo

Scripts

For the correlation coefficient, we’ll write a script. A script is a functionwith no input or output. Recall that the correlation coefficient is given by

ρ =Cov(X ,Y )√Var(X )Var(Y )

where

Cov(X ,Y ) =

∑(Xi − X )(Yi − Y )

n, Var(X ) = Cov(X ,X )

Go to File → New → Script.

Type the text on the following slide.

NRK (University of Minnesota) MATLAB 2012.08.30 23 / 28

Page 46: Kirchner - Matlab Demo

Our script

%Import exxon, chevron data from Excel

[num,txt,raw]=xlsread(’Z:\Orientation.xls’);

exxon = num(:,1);

chevron = num(:,2);

%Define anonymous functions for mean, variance, covariance

mean = @(x) sum(x)/length(x);

cov = @(x,y) sum((x-mean(x)).*(y-mean(y)))/length(x);

var = @(x) cov(x,x);

%Compute Correlation coefficient

correl = cov(exxon,chevron)/sqrt(var(exxon)*var(chevron));

correl

NRK (University of Minnesota) MATLAB 2012.08.30 24 / 28

Page 47: Kirchner - Matlab Demo

Running the script

Save the file as “correl.m”

Type correl in the Command Window

MATLAB will execute each of the commands in that script. It willsuppress output (note the semicolons).

There is no semicolon on the last line. Therefore MATLAB prints theoutput for that line. I got .7966.

Note the comments we typed. When we go back to this script, we’llhave no trouble figuring out what’s going on.

NRK (University of Minnesota) MATLAB 2012.08.30 25 / 28

Page 48: Kirchner - Matlab Demo

Final Exercise

The best fit line to a scatter plot has slope

m =Cov(X ,Y )

Var(X )

and intercept b = Y −mX . Write a script to determine these quantities ifX represents Exxon and Y represents Chevron.

NRK (University of Minnesota) MATLAB 2012.08.30 26 / 28

Page 49: Kirchner - Matlab Demo

Import Yahoo! Finance Data to Excel

1 The ticker symbol for Exxon is XOM, so go tohttp://finance.yahoo.com/q/hp?s=XOM+Historical+Prices.

2 At the bottom of the screen, click the “Download to Spreadsheet”link.

NRK (University of Minnesota) MATLAB 2012.08.30 27 / 28

Page 50: Kirchner - Matlab Demo

Import Yahoo! Finance Data to MATLAB

1 Download http://math.umn.edu/~dodso013/fm503/0910/docs/

MATLAB/yahoo_prices.m and put it in your MATLAB path.

2 The MATLAB commandexxon=yahoo_prices({’XOM’},’01-Jan-2009’,’31-Dec-2011’);

will grab Exxon stock price data from January 1, 2009 until December31, 2011. It will get stored in the variable exxon.

NRK (University of Minnesota) MATLAB 2012.08.30 28 / 28