%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Following: MATLAB An Introduction with
% Applications: Amos Gilat
%
% Chapter 1 Starting with Matlab
%
% Note: To ensure portability, all of the commands
% below were actually executed within octave,
% and as a result there are some minor differences
% in some of the results relative to what Matlab
% generates.
%
% For our purposes Matlab and octave
% are to be viewed as synonymous.
%
% We start with a brief look at the use of Matlab to do
% simple calculations (i.e. using it essentially as a
% calculator).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Introductory notes
%
% - First, observe that Matlab is primarily a language
% for performing numerical calculations, as
% opposed, for example, to Maple which fundamentally
% supports symbolic/algebraic (as well as numerical)
% computations. Matlab does provide
% support for symbolic manipulation, but does so
% by incorporating the core components of Maple.
% In addition, there is support for strings and
% some other non-numerical data types in Matlab.
%
% However, our study of the language will focus almost
% exclusively on its use for numerical computations.
%
% - The Matlab command prompt is >>
%
% - Matlab has a help facility, with syntax
%
% >> help format
%
% to get help on output formats, for example.
%
% - The comment character in Matlab is %
%
% - You can use command line editing facilities as
% in the shell and Maple, using the arrow keys,
% backspace etc.
%
% - THERE IS NO REQUIRED STATEMENT TERMINATOR IN Matlab!!
%
% An end-of-line (newline) character itself
% represents the end of a statement
%
% - If you wish to enter a long statement that
% continues across more than one line, end the
% line with ... (see example below)
%
% - Ending a statement with a semi-colon (;)
% suppresses output in Matlab!!
%
% - Ending a statement with a colon (:) is likely
% to generate a syntax error, since the colon is
% a basic OPERATOR in Matlab
%
% - The command clc clears the command window
% using either 'command-line' Matlab (or octave)
% or GUI-based Matlab
%
% - Note that Matlab and octave sometimes differ slightly
% in how they output the result of a calculation
%
% Matlab
%
% ans =
% 1.234
%
%
% Octave
%
% ans = 1.234
%
% Again, since the results below were generated with
% octave, the form of the output from Matlab will be
% different at times
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1.3 Arithmetic Operations With Scalars
%
% NOTE: If the result of a calculation is not
% explicitly assigned to a variable, Matlab assigns
% the result to the default variable ans
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab has the usual suite of arithmetic operators
% plus one more that is useful in the context of
% linear algebra
%
% Addition: +
% Subtraction: -
% Multiplication: *
% Right Division: /
% Left Division: \ (advanced topic)
% Exponentiation: ^
%
% with precedence as follows
%
% 1) Parenthesized expressions (inside -> outside)
% 2) Exponentiation
% 3) Multiplication & Division (equal precedence)
% 4) Addition & Subtraction (equal precedence)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
7 + 8/2
ans = 11
>>
(7+8)/2
ans = 7.5000
>>
4 + 5/3 + 2
ans = 7.6667
>>
5^3 / 2
ans = 62.500
>>
27^(1/3) + 32^0.2
ans = 5
>>
27^1/3 + 32^0.2
ans = 11
>>
0.7854-(0.7854)^3/(1*2*3) + 0.785^5/(1*2*3*4*5)...
-(0.785)^7/(1*2*3*4*5*6*7)
ans = 0.70710
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1.4 Display Formats
%
% Type help format for full details
%
% The format command controls how numbers
% resulting from Matlab computations appear on the
% screen.
%
% Here there is again a slight difference in how
% Matlab and octave behave
%
% MATLAB
%
% The start-up default is format short which
% displays numbers in the range 0.001 <= number <= 1000
% in fixed-point form with 4 digits to the right of the
% decimal point, otherwise using format short e
%
% octave
%
% octave tries to be more consistent with the
% total number of digits that are displayed.
% Thus, for example, when format short is in
% effect, numbers will be displayed with a total of
% 5 significant digits
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Examples
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
format long; 290/7
ans = 41.4285714285714
>>
format short e; 290/7
ans = 4.1429e+01
>>
format long e; 290/7
ans = 4.14285714285714e+01
>>
format bank; 290/7
ans = 41.43
>>
format short; 290/7
ans = 41.429
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1.5 Elementary Math Built-in Functions
%
% Elementary math functions
%
% sqrt(x) Square root
% nthroot(x,n) Real nth root of a real number
% If x < 0, n must be an odd integer
% exp(x) Exponential
% abs(x) Absolute value
% log(x) Natural (base e) logarithm (ln)
% log10(x) Base 10 logarithm
% factorial(x) Factorial function x! (x must be a
% non-negative integer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
sqrt(64)
ans = 8
>>
sqrt(50+14*3)
ans = 9.5917
>>
sqrt(54+9*sqrt(100))
ans = 12
>>
(15+600/4)/sqrt(121)
ans = 15
>>
sqrt(81)
ans = 9
>>
nthroot(80,5)
ans = 2.4022
>>
exp(5)
ans = 148.41
>>
abs(-24)
ans = 24
>>
log(1000)
ans = 6.9078
>>
log10(1000)
ans = 3
>>
factorial(5)
ans = 120
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Trigonometric math functions
%
% sin(x) sine of angle x (x in radians)
% sind(x) sine of angle x (x in degrees)
% cos(x) cosine of angle x (x in radians)
% cosd(x) cosine of angle x (x in degrees)
% tan(x) tangent of angle x (x in radians)
% tand(x) tangent of angle x (x in degrees)
% cot(x) cotangent of angle x (x in radians)
% cotd(x) cotangent of angle x (x in degrees)
% asin(x) arcsine of angle x (returns radians)
% asind(x) arcsine of angle x (returns degrees)
% acos(x) arccosine of angle x (returns radians)
% acosd(x) arccosine of angle x (returns degrees)
% atan(x) arctangent of angle x (returns radians)
% atand(x) arctangent of angle x (returns degrees)
% acot(x) arcotangent of angle x (returns radians)
% acotd(x) arcotangent of angle x (returns degrees)
%
% sinh(x) hyperbolic sine of x
% cosh(x) hyperbolic cosine of x
% tanh(x) hyperbolic tangent of x
% coth(x) hyperbolic cotangent of x
% asinh(x) inverse hyperbolic sine of x
% acosh(x) inverse hyperbolic cosine of x
% atanh(x) inverse hyperbolic tangent of x
% acoth(x) inverse hyperbolic cotangent of x
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Note that pi is pre-set to the correct value when
% a Matlab session is started, but that it is NOT
% protected, as it is in maple
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
sin(pi/6)
ans = 0.50000
>>
cosd(30)
ans = 0.86603
>>
tan(pi/6)
ans = 0.57735
>>
cotd(30)
ans = 1.7321
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Rounding functions
%
% NOTE: Gilat's definitions aren't quite correct for
% some of these!
%
% round(x) round to the nearest integer
% fix(x) take integer part of x
% ceil(x) smallest integer >= x
% floor(x) largest integer <= x
% rem(x,y) returns the remainder after x is
% divided by y
% sign(x) signum function. Returns 1, 0, -1
% for x > 0, x = 0, x < 0 respectively
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
round(17/5)
ans = 3
>>
fix(13/5)
ans = 2
>>
ceil(11/5)
ans = 3
>>
floor(-9/4)
ans = -3
>>
rem(13,5)
ans = 3
>>
sign(5)
ans = 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1.6 Defining Scalar Variables
%
% Rules for Matlab variable names
%
% 1. Must begin with a letter
% 2. Can be up to 63 characters long
% 3. Can contain letters, digits, underscore
% 4. Cannot contain special characters (. , ; etc.)
% 5. Names ARE case sensitive
% 6. Names cannot contain embedded whitespace
% 7. Some predefined names, such as sin, can be
% overwritten, so be careful!
%
% Assignment Operator
%
% The assignment operator is the equals sign: =
%
% General Syntax of Variable Assignment
%
% <variable name> = <value>
%
% Reserved Words (keywords)
%
% The following names are reserved by Matlab and cannot
% be used as variable names:
%
% break case catch
% continue else elseif
% end for function
% global if otherwise
% persistent return switch
% try while
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
x = 15
x = 15
>>
x = 3*x - 12
x = 33
>>
a = 12
a = 12
>>
B = 4
B = 4
>>
C = (a - B) + 40 - a/B * 10
C = 18
>>
a = 12;
>>
B = 4;
>>
C = (a - B) + 40 - a/B * 10
C = 18
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Predefined Variables
%
% ans Variable containing the value of the last
% expression not explicitly assigned to
% another variable
% pi The numeric value of the special number 'pi'
% eps "Machine epsilon": The largest number such
% that 1 + eps = 1 in the floating point
% domain. Equal to 2^(-52) or about 2.22e-16
% Inf Used for infinity (also inf)
% i sqrt(-1)
% j sqrt(-1)
% NaN "Not a Number". Used when an expression does
% not evaluate to a valid numeric value
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
pi
ans = 3.1416
>>
eps
ans = 2.2204e-16
>>
Inf
ans = Inf
>>
i
ans = 0 + 1i
>>
j
ans = 0 + 1i
>>
NaN
ans = NaN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1.7 Useful Commands for Managing Variables
%
% clear Removes all vbls from memory
% clear x y z Removes only vbls x, y, z from
% memory
% who Displays a list of all vbls
% currently in memory
% whos Displays a list of the vbls
% currently in memory and their size
% and other information
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Multiple statements on line, and output suppression
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Multiple statements can be entered on a single
% line by separating them with
%
% 1) commas
% 2) semi-colons
%
% In the first case, output from the statements will be
% displayed. In the second, output is suppressed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
a1 = 1, b1 = pi, c1 = sqrt(101)
a1 = 1
b1 = 3.1416
c1 = 10.050
>>
a2 = 2; b2 = 2 * pi; c2 = 2 * sqrt(101);
% Output was suppressed, but values were assigned as
% expected ...
>>
a2
a2 = 2
>>
b2
b2 = 6.2832
>>
c2
c2 = 20.100