Download presentation
1
Introduction to Matlab
2
What is Matlab? Matlab Assembly
Matlab is basically a high level language which has many specialized toolboxes for making things easier for us. How high? Assembly High Level Languages such as C, Pascal etc. Matlab
3
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
4
Variables No need for types. i.e.,
All variables are created with double precision unless specified and they are matrices. After these statements, the variables are 1x1 matrices with double precision int a; double b; float c; Example: >>x=5; >>x1=2;
5
Sym, syms, and vpa Sym Create the symbolic variables Ex: Syms
x = sym('x'); y = sym('y','positive'); Syms Shortcut for creating symbolic variables and functions 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.
6
Vpa Variable-precision arithmetic Ex: >> vpa(pi) ans = >> vpa(pi,4) 3.142
7
Exponential and Logarithmic
exp(x) sqrt(x) Ex: >> y=exp(x) >> y=sqrt(x) Logarithmic log(x) natural logarithm ln log10(x) >> log10(x)= log(x) / log(10) log2(x) >> log2(x)= log(x) / log(2)
8
ceil, and floor ceil(x) round to nearest integer towards + Ex:
ans = 4 floor(x) round to nearest integer towards – >> floor(3.2) 3
9
Round and abs round(x) round to nearest integer Ex: >>round(2.4)
ans= 2 >>round(2.6) 3 abs(x) absolute value >>abs(-5) 5
10
Trigonometric and their inverse
cos(x) acos(x) sin(x) asin(x) tan(x) atan(x) cot(x) acot(x) csc(x) acsc(x) sec(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].
11
Array, Matrix a vector x = [1 2 5 1]
a matrix x = [1 2 3; 5 1 4; ] transpose y = x’ y = 1 2 5
12
Inverse x = [1 2; 5 1] Determinant x = [1 2; 5 1] y=inv(x) y =
Determinant x = [1 2; 5 1] A=det(x) A = -9
13
t =1:10 t = k =2:-0.5:-1 k = B = [1:4; 5:8] x =
14
zeros(M,N) MxN matrix of zeros
ones(M,N) MxN matrix of ones rand(M,N) MxN matrix of uniformly distributed random numbers on (0,1) x = zeros(1,3) x = x = ones(1,3) x = rand(1,3)
15
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.
16
x = [1 2], y = [4 5], z=[ 0 0] A = [ x y] 1 2 4 5 B = [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.
17
Operators (arithmetic)
+ addition - subtraction * multiplication / division ^ power
18
Given A and B: Addition Subtraction Product Transpose
19
Operators (Element by Element)
.* element-by-element multiplication ./ element-by-element division .^ element-by-element power
21
A = [1 2 3; 5 1 4; 3 2 1] A = b = x .* y b= c = x . / y c= d = x .^2 d= x = A(1,:) x= y = A(3 ,:) y=
22
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 >> expand((x-1)*(x-2)) ans = x^2 - 3*x + 2
23
simplify Algebraic simplification >>syms x >>f1=sin(x)^2 + cos(x)^2 +log(x); >> simplify(f1) ans= 1+log(x)
24
Solve and dsolve solve Solve equations and systems >>syms x
>>solve(x^2-3*x+2) ans = 1 2
25
>>S = solve('x + y = 1','x - 11*y = 5') S = x: [1x1 sym]
>>syms x y >>S = solve('x + y = 1','x - 11*y = 5') S = x: [1x1 sym] y: [1x1 sym] >>S = [S.x S.y] [ 4/3, -1/3]
26
dsolve Ordinary differential equation >>dsolve('Dx = -a*x') ans = C2*exp(-a*t)
27
Differentiation and Integration
Differences and approximate derivatives >> x=[ ]; >> diff(x) ans = >> diff(sym('x^3+2*x^2-x+1')) 3*x^2 + 4*x - 1
28
int Integrate symbolic expression >>int(sym('x')) ans = x^2/2
29
limit Compute limit of symbolic expression >>syms x >>limit(sin(x)/x) ans= 1
30
Sum of series Symsum Sum of series S= 1+2^2+3^2+…+n^2 = >>syms k
>>symsum(k^2, 1, 10) ans = 385
31
Questions ?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.