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

Lecture 1

The document provides an introduction to MATLAB, covering its key components such as the Current Folder, Editor, Workspace, Command Window, and Toolbar. It explains how to create and manage .m files, define variables, perform elementary algebra, and load data, along with useful functions for descriptive statistics and getting help. Additionally, it touches on the Poisson distribution and lists various functions learned in the session.

Uploaded by

kaisthc1004
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 1

The document provides an introduction to MATLAB, covering its key components such as the Current Folder, Editor, Workspace, Command Window, and Toolbar. It explains how to create and manage .m files, define variables, perform elementary algebra, and load data, along with useful functions for descriptive statistics and getting help. Additionally, it touches on the Poisson distribution and lists various functions learned in the session.

Uploaded by

kaisthc1004
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

BTM500

The Basic concepts of MATLAB

Lecture 1
Introduction of the MATLAB BTM500

(1) The Current Folder

• Role: Browse and manage files in the working directory


• Functions: Load and save script files, data files, and other
resources
Introduction of the MATLAB BTM500

(2) Editor

• Role: Write and edit MATLAB code


• Functions: Multiple file tabs, auto-completion, debugging support
Introduction of the MATLAB BTM500

(3) Workspace

• Role: Display a list of variables and their values


• Functions: Create variables, modify values, save and load
variables
Introduction of the MATLAB BTM500

(4) Command Window

• Role: Execute commands and view outputs


• Functions: Run code interactively, display results, check error
messages.
Introduction of the MATLAB BTM500

(5) Details Pane

• Role: Show detailed information about selected files or variables


• Functions: Display file properties and preview contents
Introduction of the MATLAB BTM500

(6) Toolbar

• Role: Provide shortcuts for essential functions


• Functions: Open/save files, manage workspace, run code, and
more
Introduction of the MATLAB BTM500

The MATLAB Prompt

• This is where you execute your commands.


• Type or paste commands, then hit Enter.
• If entering multiple commands, separate them with semicolons (;).
• If you want the output to be displayed, do not use a semicolon.
Introduction of the MATLAB BTM500

.m Files
• Primary way to save and execute your MATLAB code
• Scripts or documents where you can write multiple lines of MATLAB
commands
• Use a semicolon (;) at the end of a line to suppress output
• Use a percent sign (%) to add comments
• To divide your code into sections that can be run separately, use ‘%
%’
Creating a .m File:
1. In MATLAB, navigate to File > New script.
2. This opens the editor where you can start typing your MATLAB
code.
3. After writing your code, save the file with a ‘.m’ extension.
Introduction of the MATLAB BTM500

Variables
• Variables are the basic building blocks.
• Capitalization matters: ‘x’ is not the same as ‘X’.
• A matrix is a variable that contains multiple entries,
e.g., V=[.98 1.02 .99 1.07];.
• You will get errors if try to add vectors of different sizes of different
orientations.
• Always use brackets [ ] to define matrices.
• Always use parentheses ( ) to access elements of matrices.
• The row is always first, the column is always second:
M(3,2) is not the same as M(2,3).
Introduction of the MATLAB BTM500

Elementary Algebra
• +, -, *, / (addition, subtraction, multiplication, division) follow the
rules of matrix algebra.
• Use ./ and .* for element-wise operations on matrices and vectors.
• Use ‘ to transpose a matrix.
• You can always multiply a matrix by a scalar.

See Code – lecture1.m


Introduction of the MATLAB BTM500

Storing Data
1. Open new ‘.m’ file
2. Type dataset = [ ];
3. Copy all of the data from your Excel or text file and paste it inside
[ ].
4. Save the ‘.m’ file in a working directory; call it ‘filename.m’.
5. At the MATLAB prompt type: filename;
This executes any code in the file ‘filename.m’.

• Inside ‘filename.m’, you have defined a matrix called ‘dataset’. This


matrix now exists in MATLAB’s memory, just like any other matrix.
Introduction of the MATLAB BTM500

Loading Data
• I have created two daily return time series (year 2006) for British
Petroleum (BP) and Volcom (VLCM), which are stored in files
‘data_bp.m’ and ‘data_vlcm.m’.
• When you type ‘data_bp;’ into the MATLAB prompt, a variable
called ‘bp’ is loaded into MATLAB.
• ‘bp’ has a length of 251 (251 trading days in 2006) and 4 columns:
- Perm Number (Identifier number)
- Date
- Price
- Holding period return

Descriptive Statistics
• Useful functions: max, min, var, cov, sum, abs, exp, sqrt, round
Introduction of the MATLAB BTM500

See Code - lecture1.m


Introduction of the MATLAB BTM500

Getting Help
• ‘help’ lists topic area.
• ‘help graph2d’ lists functions within ‘graph2d’ (e.g., ‘plot’).
• ‘help plot’ gives help on plotting.
• ‘lookfor’ searches for keywords within help.

Potential Problems
• All rows must have the same size!
• MATLAB only understands numbers; it will give you errors if there
are any letters in what you pasted.
• Date formats (e.g., ‘28-Dec-1979’) may be problematic – use three
columns for day, month, and year.
• Don’t forget the semicolon; otherwise, all data will appear on
screen.
Introduction of the MATLAB BTM500

Poisson Distribution
• The Poisson distribution helps us predict how often an event is
likely to occur over a certain period of time.
• For example:
• The number of emails you receive per hour.
• The number of cars passing through a toll booth in a day.
• The number of defects in a length of wire.
• Poisson probability density function (pdf) − 𝜆 𝑥
𝑒 𝜆
𝑓 ( 𝑥| 𝜆 ¿=
𝑥!
• Poisson cumulative distribution function (cdf)
𝑓𝑙𝑜𝑜𝑟 (𝑥) 𝑖
𝜆
𝐹 ( 𝑥| 𝜆 ¿=𝑒
−𝜆
∑ 𝑖!
𝑖 =0
Introduction of the MATLAB BTM500

Functions we learned today


• General: whos, clear, size, load, save
• Matrix: zeros, ones
• Math: +, -, /, *, .*, ./, sum, exp, abs, sqrt, round
• Statistic: min, max, mean, std, corrcoef, var, cov
• Plots: plot, hold, subplot, xlabel, ylabel, title, grid

Next week
• Logic
• Loops
• Creating your own functions

You might also like