introduction to matlab. what is matlab? matlab is basically a high level language which has many...
TRANSCRIPT
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?
Assembly
High Level Languages such as
C, Pascal etc.
Matlab
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
VariablesNo 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;
Sym, syms, and vpaSymCreate the symbolic variablesEx:
x = sym('x'); y = sym('y','positive');
SymsShortcut for creating symbolic variables and
functionsEx:
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.
VpaVariable-precision arithmeticEx:
>> vpa(pi) ans = 3.1415926535897932384626433832795 >> vpa(pi,4) ans = 3.142
Exponential and LogarithmicExponential
exp(x) sqrt(x)Ex:
>> y=exp(x)>> y=sqrt(x)
Logarithmiclog(x) natural logarithm lnlog10(x)Ex:
>> log10(x)= log(x) / log(10)log2(x)Ex:
>> log2(x)= log(x) / log(2)
ceil, and floorceil(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 absround(x) round to nearest integerEx:
>>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) 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 degreesdegtoradEx:
>>x=degtorad(45)x = 0.7854>> cos(x)ans = 0.7071
*Inverse of trigonometric returns real values in the interval [0,pi].
Array, Matrixa vector x = [1 2 5 1]
x = 1 2 5 1
a matrix x = [1 2 3; 5 1 4; 3 2 -1]
x = 1 2 3 5 1 4 3 2 -1
transpose y = x’ y = 1
2 5
1
Inverse x = [1 2; 5 1]y=inv(x)y =
-0.1111 0.2222 0.5556 -0.1111
Determinant x = [1 2; 5 1]A=det(x)
A = -9
t =1:10
t = 1 2 3 4 5 6 7 8 9 10
k =2:-0.5:-1
k = 2 1.5 1 0.5 0 -0.5 -1
B = [1:4; 5:8]
x = 1 2 3 4 5 6 7 8
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 =
0 0 0
x = ones(1,3)
x =
1 1 1
x = rand(1,3)
x =
0.9501 0.2311 0.6068
Matrix IndexThe 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
y = A(3 ,:)
y= 3 4 -1
b = x .* y
b= 3 8 -3
c = x . / y
c= 0.33 0.5 -3
d = x .^2
d= 1 4 9
x = A(1,:)
x= 1 2 3
Factor, Expand, and Simplifyfactor(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
simplifyAlgebraic simplification
>>syms x>>f1=sin(x)^2 + cos(x)^2 +log(x);>> simplify(f1)ans=1+log(x)
Solve and dsolvesolveSolve equations and systems
>>syms x>>solve(x^2-3*x+2)ans =
12
>>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]
dsolveOrdinary differential equation
>>dsolve('Dx = -a*x')ans = C2*exp(-a*t)
Differentiation and IntegrationdiffDifferences 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
intIntegrate symbolic expression
>>int(sym('x')) ans = x^2/2
limitCompute limit of symbolic expression
>>syms x>>limit(sin(x)/x)ans=1
Sum of seriesSymsumSum of seriesS= 1+2^2+3^2+…+n^2 =
>>syms k >>symsum(k^2, 1, 10)ans = 385
Questions?????