MATLAB Basics
MATLAB Basics
>> 2+3 < Enter > >> 2-3 < Enter >
ans = ans =
5 -1
>> 5*3 < Enter > >> 5/4 < Enter >
ans = ans =
15 1.2500
>> 5\4 < Enter > >> 2^3 < Enter >
ans = ans =
0.8000 8
>> 1/0 < Enter > >> 0/0 < Enter >
ans = ans =
Inf NaN
>> a=2 < Enter > >> x=5; < Enter >
a= >> y=4; < Enter >
2 >> z=x+y < Enter >
>> b=5 < Enter > z=
b= 9
5 >> z=x-y < Enter >
>> c=a+b < Enter > z=
c= 1
7 >> z=x*y < Enter >
>> d=a*b < Enter > z=
d= 20
10 >> z=x/y < Enter >
z=
1.2500
Elementary math functions
a ans b c d x y z
Polynomials in MATLAB
p ( x) = 2 x + 3 >> p=[2 3]
p ( x) = 6 x 3 − 2 x 2 − 3 x + 1 >> p=[6 -2 -3 1]
p( x) = x 3 − 3x + 4 >> p=[1 0 -3 4]
Command Outcome Example
p ( x) = 2 x 3 − 5 x 2 − 14 x + 8
polyval(p,x) To find the value >> p=[2 -5 -14 8]; < Enter >
of the polynomial >> polyval(p,2) < Enter >
at a point x ans =
-24
p( x) = 2 x 2 + 3x + 1 , q( x) = x + 2
plot(x,y) Plotting a p( x) = 2 x 2 + 3x + 1
polynomial
x= Columns 1
3
Columns 1 through 9 2
through 9 1
0 -0.0800 0
0.7200 200
150
-0.1000 0 1.0000 1.3200
100
0.7000
Columns 19
Columns 19 through 21
through 21
4.6800
0.8000 5.3200 6.0000
0.9000 1.0000
>> ezplot('sin(x)',[0 4*pi]) < Enter > >> ezplot('cos(x)',[0 4*pi]) < Enter >
cos(x)
sin(x)
1
1
0.5
0.5
0
0
-0.5
-0.5
-1
-1
0 2 4 6 8 10 12
0 2 4 6 8 10 12
x
x
>> ezplot('tan(x)',[0 4*pi]) < Enter > >> ezplot('2*x^2+3*x+1',[-10 10]) < Enter >
tan(x) 2 x 2+3 x+1
200
4
2 150
0
100
-2
50
-4
-6 0
0 2 4 6 8 10 12 -10 -8 -6 -4 -2 0 2 4 6 8 10
x x
x2 + y2 = 4 x 2 + y 2 − 8 x + 10 y − 12 = 0
15
2
10
0
y
-2 0
y
-4 -5
-10
-6
-6 -4 -2 0 2 4 6 -15
x
-20
-20 -15 -10 -5 0 5 10 15 20
x
y 2 = 4x x 2 = −9 y
>> ezplot('y^2=4*x') < Enter > >> ezplot('x^2=-9*y') < Enter >
y 2=4 x x 2=-9 y
6 6
4 4
2 2
0 0
y
-2 -2
-4 -4
-6 -6
-6 -4 -2 0 2 4 6 -6 -4 -2 0 2 4 6
x x
x2 y2 36 x 2 + 4 y 2 = 144
+ =1
16 36
(x 2)/16+(y 2)/36=1
6 2
4 0
y
2 -2
0
y
-4
-2 -6
-6 -4 -2 0 2 4 6
x
-4
-6
-6 -4 -2 0 2 4 6
x
36 x 2 − 4 y 2 = 144
>> ezplot('36*(x^2)-4*(y^2)=144')
< Enter >
36 (x 2)-4 (y 2)=144
6
0
y
-2
-4
-6
-6 -4 -2 0 2 4 6
x
Differentiation and integration in MATLAB
f ( x) = x 2 >> syms x < Enter > >> syms x < Enter >
>> f=x^2 >> f=x^2
f= f=
x^2 x^2
>> diff(f) >> int(f)
ans = ans =
2*x x^3/3
f ( x) = sin x >> syms x < Enter > >> syms x < Enter >
>> f=sin(x) >> f=sin(x)
f= f=
sin(x) sin(x)
>> diff(f) >> int(f)
ans = ans =
cos(x) -cos(x)
f ( x) = x 3 − 27 >> syms x < Enter > >> syms x < Enter >
>> f=x^3-27 < Enter > >> f=x^3-27 < Enter >
f= f=
x^3 - 27 x^3 - 27
>> diff(f) < Enter > >> int(f) < Enter >
ans = ans =
3*x^2 (x*(x^3 - 108))/4
x +1 >> syms x < Enter > >> syms x < Enter >
f ( x) =
x >> f=(x+1)/x < Enter > >> f=(x+1)/x <Enter>
f= f=
(x + 1)/x (x + 1)/x
>> diff(f) < Enter > >> int(f)
ans = ans =
1/x - (x + 1)/x^2 x + log(x)
>> simplify(ans) <Enter>
ans =
-1/x^2
f ( x) = sin 2 ( x) >> syms x < Enter > >> syms x < Enter >
>> f=sin(x)^2 < Enter > >> f=sin(x)^2 <Enter>
f= f=
sin(x)^2 sin(x)^2
>> diff(f) < Enter > >> int (f) < Enter >
ans = ans =
2*cos(x)*sin(x) x/2 - sin(2*x)/4
f ( x) = 5 sec x + 4 cos x >> syms x < Enter > >> syms x < Enter >
>> f=5*sec(x)+4*cos(x) >> f=5*sec(x)+4*cos(x)
< Enter > <Enter>
f= f=
4*cos(x) + 5/cos(x) 4*cos(x) + 5/cos(x)
>> diff(f) < Enter > >> int (f) < Enter >
ans = ans =
(5*sin(x))/cos(x)^2 - 4*sin(x) *sin(x) - 10*atan(tan(x/2)*i)*i
3 − 1 3 1 2 3
B=
− 1 0 2 2 3 1
Find A+B and A-B. >> B=[3 -1 3;-1 0 2] < Enter >
B=
3 -1 3
-1 0 2
>> A+B < Enter >
ans =
4 1 6
1 3 3
>> A-B < Enter >
ans =
-2 3 0
3 3 -1
2 3 1 -2 3
B = 4 5 -4 2 5
2 1 >> B=[2 3;4 5;2 1] < Enter >
Find AB and BA. B=
2 3
4 5
2 1
>> A*B < Enter >
ans =
0 -4
10 3
>> B*A < Enter >
ans =
-10 2 21
-16 2 37
-2 -2 11
1 2 4 >> A=[1 2 4;-1 3 0;4 1 0] < Enter >
A = − 1 3 0
A=
4 1 0 1 2 4
Find the transpose of A, the determinant of -1 3 0
A and the inverse of A. 4 1 0
>> A' < Enter >
ans =
1 -1 4
2 3 1
4 0 0
>> det(A) < Enter >
ans =
-52
>> inv(A)
ans =
0 -0.0769 0.2308
0 0.3077 0.0769
0.2500 -0.1346 -0.0962