Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Lecture 1 Basics of Matlab

No description
by

Ari Asmi

on 20 September 2016

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Lecture 1 Basics of Matlab

Lecture 1 Basics of Matlab
Why we need data analysis?
Tool used in this course:
MATLAB - "Matrix Laboratory"
Figuring out the physics behind the measurements
Physical observation
Output
Data Analysis
Data input
Processing
mean = 0.6 nm
standard dev. = 0.2 nm
Increased understaning
http://www.mathworks.se/
MATLAB Plusses
Large user base
de facto
standard
Lots of ready made routines
Toolboxes for many purposes
Good on matrix calculations
MATLAB Minuses
Commercial and very expensive
Not as fast as "real" programming languages
Memory intensive (i.e. you will need better computer than some other choices
GNU Octave
Starting Matlab
Click on the "Matlab" icon
Matlab R2013 Desktop
(other versions slightly different)
Command window
Current path
Workspace
Command
History
Files in
current
folder
File
details
Command Window
Main area where you input your commands to MATLAB

You can use it as a calculator directly:
>> 1+1
ans =
2
>> 20*(123+44)-22
ans =
3318
>>
Basic arithmetic in Matlab
+ sum , 1+1 = 2
- substract, 2-1 = 1
* multiply, 2*2 = 4
/ divide right, 4/2 = 2
\ divide left, 4\2 = 0.5
() parenthesis (for ordering)
^ power, 10^2 = 100, 2^8 = 256

>> 1/2

ans =

0.5000

>> 1\2

ans =

2

>> 2^8

ans =

256

>>
Matrixes and arrays
Defining a matrix (or an array)
>> [1 2 3 ; 4 5 6]

ans =

1 2 3
4 5 6
Semicolon (;) divides the rows
Variables
Variable types (typical ones)
double floating point number, e.g. 2.293e10
int16 integer, e.g. -1242
char (string) a set of characters e.g. 'matlab'
logical 0 (false) or 1 (true)


Usually variables do not need to be defined:
>> A=[1 2.3 ; 2 2.2]
A =
1.0000 2.3000
2.0000 2.2000
is a double matrix
>> str = 'This is a string'
str =
This is a string

is a string
Workspace
Tells you which variables are in the memory
Same effect can be got by command
whos
>> whos
Name Size Bytes Class Attributes

A 2x2 32 double
ans 2x3 48 double
str 1x16 32 char

>>
Note on variable names:
ans
is a special variable name: the answer of
last operation:
>> 4+5
ans =
9
>> ans/2
ans =
4.5000
Do not use variable names
which have a special meaning:
e.g.

i = j = imaginary unit (i*i=-1)

of any built-in function name
Working with variables:
As long the variable is defined in the workspace, you can use them directly as a reference what the represent:

>> a = 23
a =
23
>> b = -12
b =
-12
>> a-b
ans =
35

>> A=[1 2.3 ; 2 2.2]
A =
1.0000 2.3000
2.0000 2.2000
>> b-A
ans =
-13.0000 -14.3000
-14.0000 -14.2000

Colon (:) and
end
operator
Working with Matrix (array) variables

Often you do not need the whole array. You can reference to subsets of the array by using colon (:) operator, or selecting the elements individually:
B =
1 2 3
4 5 6
7 8 9
>> B(2,1)
ans =
4
>> B(1,2)
ans =
2

>> B(:,2)
ans =
2
5
8
>> B(2,:)
ans =
4 5 6
C =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
You can create lists with : operator
start:(step):end
default step is 1
>> 1:6
ans =
1 2 3 4 5 6
>> 1:2:12
ans =
1 3 5 7 9 11
>> -12:0.1:-11.5
ans =
-12.0000 -11.9000 -11.8000 -11.7000 -11.6000 -11.5000
>> C(3:end,2:end)
ans =
10 11 12
14 15 16
>> C(1:2:end,1:2:end)
ans =
1 3
9 11
Some points on matrixes
Two different kinds of multiplication or division!
A*D
is Matrix multiplication
A.*D
is element-wise multiplication
Other Matrix commands:
A' = transpose(A)
Transpose
fliplr(A)
Flip horizontally
flipud(A)
Flip vertically
inv(A)
(numerical) inverse of A

Note: often it is better to use specific linear algebra routines instead of inv(A)
Same with division and exponentation!
A =
1.0000 2.3000
2.0000 2.2000

>> A*A
ans =
5.6000 7.3600
6.4000 9.4400

>> A.*A
ans =
1.0000 5.2900
4.0000 4.8400

Some useful arithmetic
log( ) natural logarithm, log(12) = 2.4849..
log10( ) 10-based logarithm, log10(12) = 1.0792..
exp( ) exponent, exp(10) = 2.2026..e+04
sin( ) sinus (in radians!), sin(pi./2) = 1 , similar: cos(), tan(),..
sind( ) sinus (in degrees)

pi = π = 3.1416..


Other useful array stuff
C =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> sum(C)
ans =
28 32 36 40
>> sum(C,2)
ans =
10
26
42
58


sum(array,dimension)
>> C>10
ans =
0 0 0 0
0 0 0 0
0 0 1 1
1 1 1 1

> sum(C(C>10))
ans =
81
Finding parts of array
You can refer to array indices two ways
By index numbers
(starting from 1)
By logical table
>> list=[1 2 3 40 12 11 5]
list
1 2 3 40 12 11 5
>> list([4 3 4 2])
ans =
40 3 40 2
>> list>4
ans =
0 0 0 1 1 1 1
>> list(ans)
ans =
40 12 11 5

find command and boolean logic
>> list=[1 2 3 40 12 11 5]
list
1 2 3 40 12 11 5
>> find(list>3 & list < 20)
ans =
5 6 7
NOTE: ORDER array(row, column)
Logical operators:
== equal to
<, > smaller, greater than
<=,>= smaller, greater or equal
& AND
| OR
~= not equal to
Returns indexes where this
is true
BASIC ARITHMETIC
MATRIXES AND VARIABLES
FLOW CONTROL
Repeating things: for loop
Repeating things: while loop
escaping loops
Warning on logical tests!
Flow control is a key point of programming
Repeating things
Making choices
Comparing things

if structure
Often you only want to do some command if some condition is met
General form of Matlab commands
output = command_name(input1, input2, ..)
Form:

if (logical test)
commands done only if true
else
commands done only if not true
end
a =
0
>> if (a>0); b=1 ; else ; b=-999;end
>> b
b =
-999
You can put many commands in one line
command1; command2; command3;
Messages and continuation
General
command
structure

Usually commands
provoke a output:
disable by putting
;
in end:
>> 1+2
ans =
3


>> 1+2;
>>
Continue lines
with
...
>> 1+2+3+53*...
(-2+13)
ans =
589
switch structure
If you have a limited set of choices, with different response to each
switch
variable_name
case
case_expression

commands
case
case_expression

commands
otherwise

commands
end
Example
a =
0
>> switch a
case -1
b=0;
case 0
b=-999;
case 1
b=12;
otherwise
b=-0.2;
end
>> b
b =
-999

Most important rule! Use HELP!
help
gives help
inside command window
doc
starts help
interface (more help!)
>> help help
help Display help text in Command Window.
help, by itself, lists all primary help topics. Each primary topic
corresponds to a folder name on the MATLAB search path.

help NAME displays the help for the functionality specified by NAME,
such as a function, operator symbol, method, class, or toolbox.
NAME can include a partial path.

Some classes require that you specify the package name. Events,
properties, and some methods require that you specify the class
name. Separate the components of the name with periods, using one
of the following forms:
for
index = values

commands
:
end

Most often
values

are of type 1:n
k=10;
for m = 1:k
for n = 1:k
hilbert(m,n) = 1/(m+n -1);
end
end
When you know how many times:
When you do not know how many times
while
logicaltest

commands
end
n = 1;
nFactorial = 1;
while nFactorial < 1e100
n = n + 1;
nFactorial = nFactorial * n;
end
Watch out for infinite repetitions!!
Especially in
while
loops..
continue repeat once more (no more commands this iteration)
break stop repetition (exit loop)

FILE SYSTEM
Problematic variables
Often you have some variables which are not fitting the normal behaviour:

NaN = not a number
inf = infinity
Detection:
isnan(x) TRUE if NaN
isfinite(x) TRUE if finite (not NaN or inf)
Some helping routines: (not standard!)
nanmean(x) Mean ignoring NaNs
nansum(x) sum ignoring NaNs, etc

similarly, mean(C) or median(C)
Do not EVER compare floating point numbers (i.e. decimal numbers) with each other with == operator.
Often this will never result in TRUE value
compare instead e.g. (a-b<sn) where sn is small
number (e.g. 1e-30)
Scripts
Usually you wish to repeat the things you have done later on
Also this is the way to send your exams and exercises!
path
Matlab editor
These are just text (ASCII) files with commands. You can directly copy from the command history your previous commands

Save with the
filename.m
, start with
filename

Note: Will work on your current workspace, not the saved one!
Easiest way to make scripts, although you can use whatever text editor you have
NOTE:
% is for comments
USE COMMENTS!
The commands and scripts must be either on your current folder, or in the path (list of folders where Matlab searches)
pwd
current folder
path
directories in the path
addpath
add folders to path
cd
change directory
ls, dir
directory con
what
matlab scripts and files in the current folder
which

cmd
where
cmd
is located in the path

What you should know now
Simple arithmetic in Matlab
Creating variables
Referencing variables
Flow control
Writing scripts


http://r4stats.com/articles/popularity/
ADDENDUM
"Data Science" : Methodological science
which concentrates on analysis of data
Very high demand currently
Open data
Open Science
Big Data
Internet of
things
Full transcript