There are many built-in functions in MATLAB, we will go through some of them.
sin(0)cos(0)tan(0)
ans = 0
ans = 1
ans = 0
Asin(A)
ans =
0.8415 0.9093
0.1411 -0.7568
sin(1)sin(2)sin(3)sin(4)
ans = 0.8415
ans = 0.9093
ans = 0.1411
ans = -0.7568
exp(1)exp(2)
ans = 2.7183
ans = 7.3891
log(1)log(2)log(exp(1))
ans = 0
ans = 0.6931
ans = 1
sqrt(25)sqrt(11)
ans = 5
ans = 3.3166
There are also some built-in constants, for example, pi.
pi
ans = 3.1416
pi = 2
pi = 2
pi
pi = 2
clearpi
ans = 3.1416
For some reason, MATLAB does not have a variable e, so if you want it, you can create it manually and assign it to exp(1)
.
e = exp(1)
e = 2.7183
i
. If we do sqrt(-1)
, we get 0 plus 1 times i
.
sqrt(-1)
ans = 0 + 1i
i
and it would give the same answer:
i
ans = 0 + 1i
i^2
, that also gives -1.
i^2
ans = -1
There are also some important functions for initializing matrices. The first one is the I
function where I
is the identity matrix. It is a square matrix with ones on the diagonal and zeros everywhere else. Suppose we want to create 3-by-3 identity matrix. We can do that manually.
I = [1 0 0; 0 1 0; 0 0 1]
I =
1 0 0
0 1 0
0 0 1
eye(3)
which is much more convenient.
eye(3)
ans =
Diagonal Matrix
1 0 0
0 1 0
0 0 1
zeros()
.
zeros(4)
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
zeros(3,2)
ans =
0 0
0 0
0 0
ones(3,2)
ans =
1 1
1 1
1 1