Matlab Intro
Matlab Intro
Command window:
Type your
instructions here
and press ENTER to
execute them.
2
Basic MATLAB Interface
3
Simple calculations
4
Some special variables and constants are:
5
Some Common Mathematical Function:
6
To get help
7
Click here
8
Example: search for
function mean
9
Create an m-file
To create an m-file,
1) type edit at the command
window, or
2) Press this button.
10
Create an m-file
11
Create an m-file
12
Save an m-file Note the file name will be changed …
13
Run an m-file
14
Run an m-file
15
Run an m-file
18
Arrays operators
+ Addition
- Subtraction
* Multiplication
*. Element-by-element multiplication
/ division
/. Element-by-element division
\ left division
\. Element-by-element left division
^ power
^. Element-by-element power
' array transpose
'. Unconjugated array transpose
19
Arrays operators Example
A = [1 2 3;4 5 6;7 8 0]
A=
1 2 3
4 5 6
7 8 0
A*A = A.*A =
30 36 15 1 4 9
66 81 42 16 25 36
39 54 69 49 64 0
20
Special Matrices and Vectors
Function Description
zeros All zeros
ones All ones
rand Uniformly distributed random elements
randne Normally distributed random elements
Eye(n) Returns the n-by-n identity matrix.
Z = zeros(2,4) F = 5*ones(3,3)
Z = F =
0 0 0 0 5 5 5
0 0 0 0 5 5 5
5 5 5
21
Basic Operations and Descriptive Statistics
Function Description
brush Interactively mark, delete, modify, and save observations
in graphs
cumprod Cumulative product
cumsum Cumulative sum
linkdata Automatically update graphs when variables change
prod Product of array elements
sort Sort array elements in ascending or descending order
sortrows Sort rows in ascending order
sum Sum of array elements
corrcoef Correlation coefficients
max Largest elements in array
mean Average or mean value of array
median Median value of array
22
Basic Operations and Descriptive Statistics
Function Description
cov Covariance matrix
min Smallest elements in array
mode Most frequent values in array
std Standard deviation
var Variance
23
Example
A = [1 2 3; 3 3 6; 4 6 8; 4 7 7]; B = [2,7,8,3];
prod(B)
ans =
336
mean(A) % mean of each column
ans =
3.0000 4.5000 6.0000
24
Polynomials
Representing Polynomials
p = [1 0 -2 -5];
25
Polynomial Functions
Function Description
conv Multiply polynomials
deconv Divide polynomials
poly Polynomial with specified roots
polyder Polynomial derivative
polyfit Polynomial curve fitting
polyval Polynomial evaluation
polyvalm Matrix polynomial evaluation
residue Partial-fraction expansion (residues)
roots Find polynomial roots
26
Examples
X = [2 4 5; -1 0 3; 7 1 5];
Y = polyvalm(p,X)
Y=
377 179 439
111 81 136
490 253 639
27
Examples
Roots of Polynomials
To find the roots p:
r = roots(p)
r=
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i
To find a polynomial that have the roots r:
p2 = poly(r)
p2 =
1.0000 -0.0000 -2.0000 -5.0000
28
Examples
Derivatives
To obtain the derivative of the polynomial p = [1 0 -2 -5];
q = polyder(p)
q=
3 0 -2
p=
0.0238 -0.0262 -0.4343 1.2840 -0.0096 29
Linear Algebra Functions
Function Description Function 2 Description 3
1 2 3
A 4 0 6
2 1 3 U=
32
Plot
Two-Dimensional Graphics
Suppose we want to graph the function y = 5x3 over the
x domain -1 to 1 .
x = -1:0.1:1;
y = 5*x.^3;
plot(x,y)
34
Changing the Appearance
Symbo Color Symbol Marker Symbol Line style
l 2 3
b blue . point - solid line
g green o circle : dotted lin
r red x x-mark .- dash-dot line
c cyan + plus -- dashed line
m magenta * star
y yellow s square
k black d diamond
w white ^ triangle
< triangle left
> triangle right
P pentagram
h hexagram
35
To add a title and axis labels write
36
Three-Dimensional Graphics
1. Line plotes
The plot3 function displays a three-dimensional plot of a set of
data points. Here is an example of a three-dimensional helix:
37
Operators
Often we need some relational or logical operators to companion if
statement. Operators are shown in the following table:
38
for loop
for i = 1:4
x(i) = i^2
end
for k = 2:5:20, y = k^3 - 7, end
39
while loop
c = 0; i=1;
while c==0
i=i+2
s=1/i
if s<=0.1 c=1
end
end
40
Bisection Method Example
Here is a complete program, illustrating while, if, else, and end,
which uses interval bisection to find a zero of a polynomial:
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if fx == 0
break
elseif sign(fx) == sign(fa)
a = x; fa = fx;
else
b = x; fb = fx;
end
end
x 41
Functions
You will often need to build your own MATLAB functions
as you use MATLAB to solve problems.
•
Inline •
function_handle (@)
g(3) Sqr(5)
f(3,pi/2) srtpy(2,1)
43
Functions
44
ODE function
45
... Solving first-order ODEs
Example
46
47
48