Introduction to Matlab

advertisement
Introduction to Matlab
What is Matlab?
 Matlab is basically a high level language which has many
specialized toolboxes for making things easier for us.
 How high?
Matlab
High Level
Languages such as
C, Pascal etc.
Assembly
Matlab Screen

Command Window
 type commands

Current Directory
 View folders and m-files

Workspace
 View program variables
 Double click on a variable
to see it in the Array Editor

Command History
 view past commands
Variables
 No need for types. i.e.,
int a;
double b;
float c;
 All variables are created with double precision unless
specified and they are matrices.
Example:
>>x=5;
>>x1=2;
 After these statements, the variables are 1x1 matrices with
double precision
Sym, syms, and vpa
 Sym
Create the symbolic variables
Ex:
x = sym('x');
y = sym('y','positive');
Syms
Shortcut for creating symbolic variables and functions
Ex:
syms x y
syms x y real
*SYM and SYMS are essentially the same; SYMS is often used in the
command form, while SYM is often used in the function form.

 Vpa
Variable-precision arithmetic
Ex:
>> vpa(pi)
ans =
3.1415926535897932384626433832795
>> vpa(pi,4)
ans =
3.142
Exponential and Logarithmic
 Exponential
 exp(x)
 sqrt(x)
 Ex:
>> y=exp(x)
>> y=sqrt(x)
 Logarithmic
 log(x) natural logarithm ln
 log10(x)
 Ex:
>> log10(x)= log(x) / log(10)
 log2(x)
 Ex:
>> log2(x)= log(x) / log(2)
ceil, and floor
 ceil(x)
round to nearest integer towards +
Ex:
ceil(3.2)
ans =
4
 floor(x) round to nearest integer towards –
Ex:
>> floor(3.2)
ans =
3
Round and abs
 round(x) round to nearest integer
Ex:
>>round(2.4)
ans=
2
>>round(2.6)
ans=
3
 abs(x)
absolute value
Ex:
>>abs(-5)
ans=
5
Trigonometric and their inverse






cos(x)
sin(x)
tan(x)
cot(x)
csc(x)
sec(x)
acos(x)
asin(x)
atan(x)
acot(x)
acsc(x)
asec(x)
All trigonometric functions require the use of radians and not degrees
 degtorad
Ex:
>>x=degtorad(45)
x=
0.7854
>> cos(x)
ans =
0.7071
*Inverse of trigonometric returns real values in the interval [0,pi].
Array, Matrix
 a vector
x = [1 2 5 1]
x =
1
 a matrix
2
5
1
x = [1 2 3; 5 1 4; 3 2 -1]
x =
1
5
3
 transpose
2
1
2
3
4
-1
y = x’
y =
1
2
5
1
 Inverse
x = [1 2; 5 1]
y=inv(x)
y =
-0.1111
0.5556
 Determinant
A=det(x)
A =
-9
0.2222
-0.1111
x = [1 2; 5 1]
 t
=1:10
t =

10
1
2
B
x =
3
4
1
0.5
5
6
7
8
k =2:-0.5:-1
k =

2
1
5
1.5
= [1:4; 5:8]
2
6
3
7
4
8
0
-0.5
-1
9
 zeros(M,N) MxN matrix of zeros
x = zeros(1,3)
x =
0
0
0
 ones(M,N) MxN matrix of ones
x = ones(1,3)
x =
1
1
1
 rand(M,N) MxN matrix of uniformly
distributed random
x = rand(1,3)
numbers on (0,1)
x =
0.9501
0.2311 0.6068
Matrix Index
 The matrix indices begin from 1 (not 0 (as in C))
 The matrix indices must be positive integer
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
 x = [1 2], y = [4 5], z=[ 0 0]
A = [ x y]
1
2
4
5
B = [x ; y]
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
Given A and B:
Addition
Subtraction
Product
Transpose
Operators (Element by Element)
.*element-by-element multiplication
./element-by-element division
.^element-by-element power
A = [1 2 3; 5 1 4; 3 2 1]
A=
1 2 3
5 1 4
3 2 -1
x = A(1,:)
x=
y = A(3 ,:)
y=
1 2 3
3 4 -1
b = x .* y
c=x./y
d = x .^2
b=
c=
0.33 0.5 -3
d=
3 8 -3
1 4 9
Factor, Expand, and Simplify
 factor(f)
>>syms x
>>f=x^3-6*x^2+11*x-6;
>>y=factor(f)
y=
(x-1)*(x-2)*(x-3)
 expand
>>syms x
>> expand((x-1)*(x-2))
ans =
x^2 - 3*x + 2
 simplify
Algebraic simplification
>>syms x
>>f1=sin(x)^2 + cos(x)^2 +log(x);
>> simplify(f1)
ans=
1+log(x)
Solve and dsolve
 solve
Solve equations and systems
>>syms x
>>solve(x^2-3*x+2)
ans =
1
2
>>syms x y
>>S = solve('x + y = 1','x - 11*y = 5')
S=
x: [1x1 sym]
y: [1x1 sym]
>>S = [S.x S.y]
S=
[ 4/3, -1/3]
 dsolve
Ordinary differential equation
>>dsolve('Dx = -a*x')
ans =
C2*exp(-a*t)
Differentiation and Integration
 diff
Differences and approximate derivatives
>> x=[2 5 9 1 3];
>> diff(x)
ans =
3 4 -8 2
>> diff(sym('x^3+2*x^2-x+1'))
ans =
3*x^2 + 4*x - 1
 int
Integrate symbolic expression
>>int(sym('x'))
ans =
x^2/2
 limit
Compute limit of symbolic expression
>>syms x
>>limit(sin(x)/x)
ans=
1
Sum of series
 Symsum
Sum of series
S= 1+2^2+3^2+…+n^2 =
>>syms k
>>symsum(k^2, 1, 10)
ans =
385
Questions
 ?
 ?
 ?
 ?
 ?
Download