![Page 1: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/1.jpg)
Introduction to Matlab
![Page 2: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/2.jpg)
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
![Page 3: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/3.jpg)
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
![Page 4: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/4.jpg)
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;
![Page 5: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/5.jpg)
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.
![Page 6: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/6.jpg)
VpaVariable-precision arithmeticEx:
>> vpa(pi) ans = 3.1415926535897932384626433832795 >> vpa(pi,4) ans = 3.142
![Page 7: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/7.jpg)
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)
![Page 8: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/8.jpg)
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
![Page 9: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/9.jpg)
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
![Page 10: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/10.jpg)
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].
![Page 11: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/11.jpg)
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
![Page 12: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/12.jpg)
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
![Page 13: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/13.jpg)
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
![Page 14: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/14.jpg)
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
![Page 15: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/15.jpg)
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.
![Page 16: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/16.jpg)
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.
![Page 17: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/17.jpg)
Operators (arithmetic)+ addition- subtraction* multiplication/ division^ power
![Page 18: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/18.jpg)
Given A and B:
Addition
Subtraction
Product Transpose
![Page 19: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/19.jpg)
Operators (Element by Element).*element-by-element multiplication./ element-by-element division.^ element-by-element power
![Page 20: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/20.jpg)
![Page 21: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/21.jpg)
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
![Page 22: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/22.jpg)
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
![Page 23: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/23.jpg)
simplifyAlgebraic simplification
>>syms x>>f1=sin(x)^2 + cos(x)^2 +log(x);>> simplify(f1)ans=1+log(x)
![Page 24: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/24.jpg)
Solve and dsolvesolveSolve equations and systems
>>syms x>>solve(x^2-3*x+2)ans =
12
![Page 25: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/25.jpg)
>>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]
![Page 26: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/26.jpg)
dsolveOrdinary differential equation
>>dsolve('Dx = -a*x')ans = C2*exp(-a*t)
![Page 27: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/27.jpg)
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
![Page 28: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/28.jpg)
intIntegrate symbolic expression
>>int(sym('x')) ans = x^2/2
![Page 29: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/29.jpg)
limitCompute limit of symbolic expression
>>syms x>>limit(sin(x)/x)ans=1
![Page 30: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/30.jpg)
Sum of seriesSymsumSum of seriesS= 1+2^2+3^2+…+n^2 =
>>syms k >>symsum(k^2, 1, 10)ans = 385
![Page 31: Introduction to Matlab. What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for us](https://reader035.vdocuments.site/reader035/viewer/2022062314/56649f1a5503460f94c2f770/html5/thumbnails/31.jpg)
Questions?????