Matlab Language Fundamentals
Basic Arithmetic and order of operations:
We can demonstrate all
the operations of the basic calculator and the scientific calculator using
MATLAB, but MATLAB is a case sensitive tool which mean we have to enter or
expressions or functions in the right way that MATAB understands and to get the
right required answers.
So we can use addition
(+), subtraction (-), multiplication (*), and division (/) symbols to solve our
basic calculations.
The order of operations
is also an important thing we should take care of while we entering our
equations or functions, that because "5+6*7" doesn't equal
"(5+6)*7", because like mathematics said the multiplication and
division operations has a higher order than addition and subtraction.
Exponents and Scientific Notation:
Exponents in MATLAB are
represented by "^" the exponentiation symbol the number after the
symbol represents the power, for example:
5^3 means 53 = 5 * 5 * 5 = 5 x 5 x 5
Exception; if we enter " -5^2 " in
simple its equal "25" in positive, but MATLAB it get it like this
" -1*5^2 " and gives " -25 " as the answer and that because
MATLAB will always do exponents first, so to get the appropriate answer we
should but the expression like this " (-5)^2 ".
The scientific notations
are widely used in advanced calculations and it represented like this:
Scientific notation
|
In simple
|
Other format
|
other
|
8.522e+003
|
8.522 x 103
|
8.522e+3
|
8.522*10^3
|
8.522e-003
|
8.522
x 10-3
|
8.522e-3
|
8.522*10^-3
|
-8.522e+002
|
-8.522 x 102
|
-8.522e+2
|
-8.522*10^2
|
Working with Fractions and the Symbolic Math Toolbox
Fraction is the number
which has a numerator and denominator values, MATLAB always give answers in
decimal form, but sometimes when we working with fractions we need the answers
in fraction, and MATLAB doesn't do that until we tell it to do!
So if we have a need to
do any calculations to fractions and we need the answer in a fraction of a
symbolic form we use the symbolic toolbox commands, for example:
With symbolic form
|
Sym(1/2+1/4) = 3/4 symbolic form
|
Without symbolic form
|
1/2+1/4 = 0.7500 numeric form
|
We can
use the symbolic command "sym( )" to convert any numeric or
decimal value into a symbolic or fraction, and to convert from symbolic form to
decimal form we use the command "double( )", for example:
Command
|
Answer
|
Sym(0.125)
|
1/8
|
double(1/8)
|
0.1250
|
Defining and Using Variables and comments:
We can define variables
and assign any value to this variable very easy, but MATLAB is case sensitive
to the variable name letters, we can add some line of comments into our MATLAB
code without affecting any part of the code but simply by typing these comments
after "%" symbol until the end of the line, for example:
Some variable definition
|
Some variable definition
|
X = 10
|
x=1.6
|
Y = sin(x)
|
Z= 0:0.1:10; z=0:10
|
z=(15+2)^2 + (23-3)*10
|
B=[1 2 3;4 5 6]
|
Example1:
>> Mass=37; % define mass variable and
assign its value.
>> Accel=25; % define acceleration variable and
assign its value.
>> Force=Mass * Accel; % define force variable and
assign its value by the equation
Example2:
% this is circle area calculation code, which
equal π x R2 .
>> R = 22; % define the radius variable
and assign its value.
% π is already defined in MATLAB in the name
"pi".
>> Area = pi * R^2; % calculate the
value of the area.
Note[1]: we can manage and look for all the variables defined in our
session by using the commands "who" and "whos",
try it by yourself.
Note[2]: we can clear any variable by the command "clear
var_name", or we can clear all the variables by the command "clear
all".
Adjusting the Display Precision:
We can adjust the
display precision by using the command "format style", format sets the display of floating-point numeric
values to the default display format, which is the short fixed decimal format.
This format displays 5-digit scaled, fixed-point values, format Style changes
the display format to the specified Style.
Style
|
Result
|
example
|
short (default)
|
Short fixed decimal format, with 4
digits after the decimal point.
|
3.1416
|
Long
|
Long fixed decimal format, with 15 digits after the
decimal point for double values, and 7 digits after the decimal point for
single values.
|
3.141592653589793
|
bank
|
Currency format, with 2 digits after
the decimal point.
|
3.14
|
shortE
|
Short scientific notation, with 4 digits after the
decimal point.
|
3.1416e+00
|
rat
|
||
hex
|
There is
other style you can try it by yourself by using help documentation of the
command.
Creating and Storing Values in Symbolic Variables:
We can create symbolic
variables to assign its fraction value at any time, by using the commands:
>> x = sym(x); % define symbolic
variable called "x".
>> syms y; % define symbolic variable called
"y".
We can assign the
fraction value to the symbolic variable directly by using the command:
>> x = sym(1/8); % create "x" symbolic value and
assign its value "1/8".
Now we can do any calculations with
fractions and keep the answer in the symbolic form, for example:
>> R = sym(15/2); % define radius variable in
symbolic form and
assign its value.
>> Area = pi * R^2; % calculate
area value.
Answer: Area = (225*pi)/4
"the result in symbolic form".
Note[1]: try the command "pretty (area)" to see the
other fraction form of the area value.
Note[2]: you can get the decimal answer again
using the command double(ans) or vpa(ans), where ans is the name
of the variable that hold the non-decimal value format.
Example: calculate the volume of circle which "Vol = (4/3)πR3
>> R = sym(19/5); % define radius and
assign a value to it.
>> Vol = (4/3)*pi*R^3; % the volume
equation.
Answer: Vol = (27436*pi)/375 "the result in symbolic form".
Essential Mathematical Functions:
Factorial, Square Roots, and nth Roots:
We can find the factorial of any
number simply by "factorial (X)", where "X" is the
number we want to get its factorial.
We can find the square root of any
number simply by "sqrt (X)", or we can rise the number to the (1/2)
power, and if we want the nth root we use "nthroot (X,n)", where
"n" is nth root degree, for example:
Command
|
Answer
|
factorial (18)
|
6.4024e+15
|
sqrt (169)
|
13
|
169^(1/2)
|
13
|
nthroot (8,3)
|
2 "the third root of 8"
|
nthroot (20736,4)
|
12
|
sym(sqrt(40))
|
2*10^(1/2)
"try pretty and double command".
|
Trigonometric Functions and their Inverses:
Function command
|
Result
|
sin (x) , y = sin (x)
|
returns the circular sine of the elements of X.
|
cos (x) , y = cos (x)
|
returns
the circular cosine of the elements of X.
|
tan (x) , y = tan (x)
|
returns the circular tangent of each element of X.
|
sec (x) , y = sec (x)
|
returns
the circular secant of each element of X.
|
cot (x) , y = cot (x)
|
returns the cotangent for each element of X.
|
csc (x) , y = csc (x)
|
returns
the cosecant for each element of x.
|
asin (x) y = asin (x)
|
returns the inverse sine (arcsine) for each element of X
|
acos (x) , y = acos (x)
|
returns
the inverse cosine (arccosine) for each element of X.
|
atan (x) , y = atan (x)
|
returns the inverse tangent (arctangent) for each element
of X.
|
asec (x) , y = asec (x)
|
returns
the inverse secant (arc secant) for each element of X.
|
acot (x) , y = acot (x)
|
returns the inverse cotangent (arc cotangent) for each
element of X.
|
acsc (x) , y = acsc (x)
|
returns
the inverse cosecant (arc cosecant) for each element of X.
|
By default MATLAB interrupt the value
of X in radian, if we want it in degree we simply use (x*pi/180).
Example
|
Command
|
sine(-π ≤ X ≤ π)
|
X = -pi:0.01:pi; plot(X,sin(X));
|
cosine(-π ≤ X ≤ π)
|
X = -pi:0.01:pi; plot(X,cos(X));
|
tangent (-π/2 ≤ X ≤ π/2)
|
X = (-pi/2) : 0.01 : (pi/2);
plot (X, tan(X) );
|
Hyperbolic Functions and their Inverses:
Function command
|
Result
|
sinh (x) , y = sinh (x)
|
returns the hyperbolic sine of the elements of X.
|
cosh (x) , y = cosh (x)
|
returns the hyperbolic cosine of the elements of X.
|
tanh (x) , y = tanh (x)
|
returns the hyperbolic tangent of each element of
X.
|
sech (x) , y = sech (x)
|
returns the hyperbolic secant of each element of X.
|
coth (x) , y = coth (x)
|
returns the hyperbolic cotangent for each element
of X.
|
csch (x) , y = csch (x)
|
returns the hyperbolic cosecant for each element of x.
|
asinh (x) y = asinh (x)
|
returns the inverse hyperbolic sine (arcsine) for
each element of X
|
acosh (x) , y = acosh (x)
|
returns the inverse hyperbolic cosine (arccosine) for
each element of X.
|
atanh (x) , y =atanh (x)
|
returns the inverse hyperbolic tangent (arctangent)
for each element of X.
|
asech (x) , y = asech (x)
|
returns the inverse hyperbolic secant (arc secant) for
each element of X.
|
acoth (x) , y = acoth (x)
|
returns the inverse hyperbolic cotangent (arc
cotangent) for each element of X.
|
acsch (x) , y = acsch (x)
|
returns the inverse hyperbolic cosecant (arc cosecant)
for each element of X.
|
Exponentials and Logarithms:
The exponential function which
represented as "e" is a commonly used element in mathematics, so we
can find it always raised to some power for e.g. "e3",
"e-6", or "e2π" and other, in MATLAB we define
the exponential function by "exp (power)", for example:
In Mathematics
|
In MATLAB
|
e3
|
exp
(3)
|
e-6
|
exp (-6)
|
e2π
|
exp
(2*pi)
|
For logarithms we have different
types depending on the base of the logarithms, see the table:
Commands
|
Result
|
example
|
log (x)
|
returns the natural logarithm of X
|
Log (5)
|
log10 (x)
|
returns the logarithm to the base 10 of X.
|
Log10 (100)
|
log2 (x)
|
returns the logarithm to the base 2 of X.
|
Log2 (64)
|
Complex numbers:
Basic Calculations with Complex Numbers:
A Complex number is
consist of two parts the real part and the imaginary part "i" as we
know " i = 0 + i " which is "-11/2" or "sqrt(-1)"
, in MATLAB we can enter or define any complex number as easy as in
mathematics, and we can also apply any of the basic calculations ( + , - , * ,
/ ) with these complex number, some examples:
Examples
|
Examples
|
15i
|
(23+5i)+(30-6i)
|
10+2i
|
(23+5i)-(30-6i)
|
A = 23+5i
|
(23+5i)*(30-6i)
|
B =30-6i
|
(23+5i)/(30-6i)
|
A+B, A-B, A*B, A/B
|
12+10i-15i
|
We can also apply other mathematics
calculations with complex number using MATLAB, for example:
Operation
|
Example
|
Square root
|
sqrt (1+3i)
|
Exponents
|
(-1+12i)^3 , (10-3i)^-2
|
Calculating the Magnitude and Angle of Complex Numbers:
We can get the magnitude
of any complex number in MATLAB using the Absolute value function "abs
(x)" which equal (R2+I2)1/2, where
"x" is the complex number "x = R ± I". And if we want the angle of
any complex number we use the built-in MATLAB command "angle(x)"
and we get the angle of the complex number "x" in radians, if we want
it in degrees we simply use "angle(x)*180/pi".
There is other built-in
MATLAB command used with complex numbers, some of these commands are:
Command
|
Result
|
real (x)
|
Returns the real part of a complex number x.
|
imag (x)
|
Returns the imaginary part of a complex number x.
|
Examples
|
|
a = 3 + 3i , b = 5 - 9i ,
c = -12 – 1/2i , d = 4i
, e = -2i
|
|
abs(a) , abs(b)
, exp(c) , exp(d)
, exp(e)
|
|
angle(a) , angle (b)
, angle (c) , angle (d) , angle (e)
|
|
angle(a)*180/pi
, angle(b)*180/pi , angle(d)*180/pi
|
|
angle(c)*180/pi
, angle(e)*180/pi
|
|
real(a), real(d), imag(b), imag(c), real(e)
|
We can
use the exponential function to convert a complex number from polar form to
rectangular form, by the command:
exp (θ*i) , which equal e^( θ *i) = cos(θ) + (sin(θ)) i , for
example :
>>
exp (pi*i) = -1.0000 +
0.0000i
-
So we can get the magnitude and the angle from the
polar form:
>>
abs ( exp (pi*i) ) % which equal "1"
>>
angle (exp (pi*i) ) %which equal "pi = 3.14" radian
>>
angle (exp (pi*i) )*180/pi % which equal "180" degrees
e.g. which is:
>> 3*exp
(0.4*i) %try it and find the magnitude and the angle.
We can also get the conjugate of a complex number
using "conj(x)" function, for e.g.
>>
conj(5+3i) % ans = 5-3i
Trig Functions, Logarithms, and Exponentials with Complex Numbers:
We can use the trigonometric, logarithms, exponential, and hyperbolic
functions with complex numbers in the proper way we should, try the examples
below:
Examples
|
Examples
|
exp(2-3i)
|
log(5i)
|
log(-3)
|
log(3+3i)
|
sin(3+3i)
|
cos(3+3i)
|
tan(3+3i)
|
asinh(3+3i)
|
acos(3-3i)
|
acsch(3+3i)
|
Complex Numbers and the Symbolic Math Toolbox:
Symbolic math toolbox commands help us to get the
answer in non-decimal forms and sometimes we need it, so try these examples:
Examples
|
sym ( exp( 3+3i ) )
|
sym ( abs (3+3i) ) , sym ( abs (1+i) ) , sym ( abs
(1+2i) )
|
sym ( angle (3+3i) ) , sym ( angle (1+i) ) , sym ( angle (2i) )
|
z=2-1/2i
, syms z , z
= sym (2-1/2i ) , z+z , z*z, z^3, …
|
s=(z^5)+(2*z^3)-(5z^-2) , real(s) , imag(s) ,
conj(s), double(s)
|
Some MATLAB functions and their usage:
Function
|
Result
|
sign(X)
|
Returns -1 if
X<0, 0 if X=0, 1 if X>0.
|
rem(x,y)
|
Returns the remainder of x/y. called
the modulus function.
|
Some MATLAB special characters:
Character
|
means
|
…
|
The code completes into the next line.
|
Inf
|
Infinity.
|