Computing Tools for Mathematics
Course code COMP1113
for
BS-Mathematics II
by
Dr. Abdul Majeed
Command Window
Command History
Current Directory
Workspace
Editor
Search bar
Run button
Work space
Open Editor
Editor
Current folder
Preview
Command window Command History
The most common such functions are:
x = cos(a); % cosine of a
x = sin(a); % sine of a
x = tan(a); % tangent of a
a = acos(x); % arc cosine of x
a = asin(x); % arc sine of x
a = atan(x); % arc tangent of x
y = exp(x); % exponential of x
y = log(x); % natural log of x
y = log10(x); % common log of x
y = sqrt(x); % square root of x
z = mod(x,y); % remainder of x/y
n = floor(x); % round x down
n = ceil(x); % round x up
n = round(x); round x up to nearest integer
n = num2str(x); % convert n to string
y = abs(x); % absolute value
+ Plus; addition operator.
- Minus; subtraction operator.
* Scalar and matrix multiplication operator.
.* Array multiplication operator.
^ Scalar and matrix exponentiation operator.
.^ Array exponentiation operator.
\ Left-division operator.
/ Right-division operator.
.\ Array left-division operator.
./ Array right-division operator.
: Colon; generates regularly spaced elements and
represents an entire row or column.
( ) Parentheses; encloses function arguments and
array indices; overrides precedence.
[ ] Brackets; enclosures array elements.
inf Infinity
NaN Not a number e.g. 0/0
realmin The smallest usable positive real
number
realmax The largest usable positive real number
MATLAB supports six relational operators.
Less Than <
Less Than or Equal <=
Greater Than >
Greater Than or Equal >=
Equal To ==
Not Equal To ~=
Thereare several ways to create matrices in
matlab. The general rule: to create a row,
enter entries
separated by spaces (or commas, although not
necessary). To start a new row, use a semi-
colon (;)
simple examples:
>>X = [5 3 7 1];
x =
5 3 7 1
>>X = [5;3;7;1];
X=
5
3
7
1
>>X = [1 2 3; 4 5 6; 7 8 9];
x =
1 2 3
4 5 6
7 8 9
matlab follows a subscript notation to access
matrix (or vector) entries.
X(i,j) : element at ith row, jth column in a 2-
D matrix
X(2,3)= 6 , X(3,1)=7
X(i) : ith element of a 1-D vector
X(8)=6
A(2,4)
A(17)
There are also special symbols and keywords
helpful for accessing the matrix entries “:” for
an index position, in general, means the entire
dimension (i.e. all elements) associated with
that index. end keyword means the last
element along a particular dimension.
B = [1:4; 5:8]
B =
1 2 3 4
5 6 7 8
X(i,:)
means ALL entries in ith row. X(i,end)
means the LAST entry in ith row.
X(2,:)= 4 5 6
X(:,j)means ALL entries in jth column.
X(end,j) means the LAST entry in j column.
X(:,2) = 2
5
8
X(end,j)
X(end,2) = 8
+ Plus; addition operator.
Increment all the elements of
a matrix by a single value
>> x = [1,2;3,4]
x=
1 2
3 4
>> y = x + 5
y=
6 7
8 9
similarly for “–”
* Scalar and matrix multiplication operator.
>> a = [1,2;3,4]; (2x2)
>> b = [1,1]; (1x2)
>> c = b*a
c=
4 6
.* Array multiplication operator.
>> a = [1,2;3,4];
>> b = [1,½;1/3,¼];
>> c = a.*b
c=
1 1
1 1
^ Scalar and matrix exponentiation operator.
>> a=2^2
4
.^ Array exponentiation operator.
>> a = [1,2;1,3];
>> b = [2,2;2,1];
>> c = a.^2
c=
1 4
1 9
>> c = a.*b
c=
2 4
2 3
./ Array right-division operator.
>> a = [1,2;1,3];
>> b = [2,2;2,1];
Element wise division
>> c = a./b
c=
0.5 1
0.5 3
.\ Array left-division operator.
c = a.\b
C= 2.0000 1.0000
2.0000 0.3333
Symbol color symbol marker symbol linestyle
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus – dashed
m magenta * star (none) no line
y yellow s square
k black d diamond