%% Glossary
% A Comprehensive MATLAB Glossary for MVCwMATLAB By R. Lipsman & J. Rosenberg Springer, 2017

% The list of commands and options is identical to those in the static 
% version of this document. In every instance here, one or more examples 
% of a use of the item is supplied in input form for the user to evaluate.

%
%% MATLAB Punctuation
% @ Creates a function handle or anonymous function

f = @(x) x^2 + sin(x); f(pi)
g = @(x, y) x.^3 + 3.*x.^2.*y +3.*x.*y.^2 + y.^3; g(1,1)

% &
% Logical AND operator
[1,1,0] & [1,1,1]

% |
% Logical OR operator
[1,1,0] | [ 1,1,1]

% ~
% Logical NOT operator
~0, ~1

% =
% Assignment operator
syms x, y = x + 7, subs(y,x,5)

% ==
% Operator for representing a symbolic equation
solve(x^2 + x + 1 == 0, x)

% %
% Introduces a comment (not to be executed)
% Matlab will not execute this line.

% :
% Range operator
X=-3:0.1:2; plot(X, X.^2 + 3.*X -1)

% ,
% Separates commands without suppressing output
g = @(t) t.^3 - t + 2, g(0), fplot(g, [-2 2])
% ;
% Suppresses output and can also be used to separate commands
h = @(t) t.^2 - 3.*t + 2; h(0); syms t; diff(h,t), fplot(h, [-2 3])

% '
% Used to deliminate a string
'This is a character string'

% +
% Addition operator, works on vectors also
[1, 2, 3] + [4, 5, 6]

% -
% Subtraction operator, works on vectors also
[1, 2, 3] - [4, 5, 6]

% *
% Multiplication operator
syms t, expand(g(t)*h(t))

% .*
% Vectorized multiplication operator
u = [1, 2, 3]; v = [4, 5, 6]; u.*v

% /
% Division operator
quot = g(t)/h(t), subs(quot, t, 1)
% Explain why Matlab chokes! Does it help to replace the '1' by a '2'?

% ./
% Vectorized division operator
u./v

% ^
% Exponentiation operator
g(t)^2, expand(ans)

% .^
% Vectorized exponentiation operator
u.^2

% .
% Decimal point, also operator for accessing or defining fields of a 
% structure array
sa = struct; sa.integers = [1 2 3 4 5]; sa.squares = [1 4 9 16 25];
sa.description = 'perfect squares';
['These ' num2str(sa.squares) ' are the squares of these '...
    num2str(sa.integers)])
% ...
% Line continuation operator (to split a command over more than one line)
% See the above entry
syms x y z real; [xsol, ysol, zsol] = solve(x^2 + y^2 + z^2 == 1, ...
    z == x^2 + y^2, x == 1/3)

%
%% MATLAB Constants
% pi

pi % is the length of arc of a circle whose diameter is 1 unit

% i
i % is the imaginary unit, if not overwritten by a variable name

% inf
inf % represnts infinity
syms x; limit(x*sin(x)/x^2, x, inf)

%
%% MATLAB Commands
% abs The absolute value function

syms x; fplot(abs(x^3 - x - 1), [-3 3])

% acos
% The inverse cosine function
syms x; diff(acos(x), x)

% asin
% The inverse sine function
syms x; int(asin(x), [0, 1])

% assume
% Set assumptions on a symbolic variable (e.g., real, positive, integer)
syms x real; assume(x>0); solve(x^2 - 1 == 0, x)

% atan
% The inverse tangent function
X = -pi/2:0.01:pi/2; plot(X, atan(X))

% atan2
% Finds the polar coordinates r, theta in terms of y and x
z = 4 + 3i; r = abs(z); theta = atan2(imag(z),real(z)); [r, theta]

% axis
% Selects the ranges of x and y (also z if a 3D plot) to show in a plot
syms x; fplot(x^2 + sin(x)), axis([-2 2 -1 5])
syms x y; fsurf(x^2 -y^2 +x + y -2), axis([-2 2 -2 2 -10 10])

% clear
% Clears values and definitions for variables and functions. If you specify
% one or more variables, then only those variables are cleared.
syms x; z = x^2, clear x; subs(z, x, 1)

% conj
% Complex conjugation
conj(4 + 5*i)

% copyobj
% Copy a graphics object; useful for combining graphs.
fplot(@sin, [-pi, pi], 'b'); sincurv = get(gca, 'Children') 
figure; fplot(@cos, [-pi, pi], '--'); copyobj(sincurv, gca)

% cos
% The cosine function
syms x; int(cos(x^2)*x, x)

% cosh
% The hyperbolic cosine function (e^x + e^(-x))/2
syms x; diff(cosh(x), x)

% cross
% The cross product of two vectors
a = [1 2 3]; b = [4 5 6]; cross(a,b)

% curl
% The curl of a vector field
syms x y z; F = [cos(y), cos(z) - x*sin(y), -y*sin(z)], curl(F, [x, y, z])

% det
% Computes the determinant of a square matrix
A = [1 2 3; 4 5 6; 7 8 9], det(A) 
% Try making A symbolic instead of numeric,
% and see how the answer changes

% diff
% Computes the derivative
syms u; diff(cos(u^2)*exp(-u), u)

% disp
% Displays the value of a variable or string. Useful for formatting output.
name = 'Bob'; age = 9; X = [name,' will be ',num2str(age),' this year.'];
disp(X)
% Compare with the result if you just type X.

% divergence
% The divergence of a vector field
divergence(F)

% dot
% The dot product of two vectors
a = [3 2 1]; b = [6 5 4]; dot(a, b)

% double
% Converts the (possibly symbolic) expression for a number to a numerical 
% (double-precision) value
syms x real; solve(x^3 - x -1 == 0, x), double(ans)

% dsolve
% Symbolic differential equation solver
syms t, y(t); dsolve(diff(y, 2) + diff(y) + y == 0, t)

% edit
% Opens a script in the Editor
edit newfile

% eig
% Computes the eigenvalues of a square matrix
A = [1 2.4 3 4; 4 -3 2 -1.5; 5 6 7 8; -8 -6 -4 -2]
eig(A)
[e, v] = eig(A)

% erf
% The error function (integral of a Gaussian)
syms t, x; int(exp(-t^2), 0, x)

% exp
% The exponential function e^x
syms x y(x); dsolve(diff(y) - y == 0, x)
fplot(exp(x), -10, 10)

% ezpolar
% Easy plotter in polar coordinates
syms theta; ezpolar(theta, [0 4*pi])

% fcontour
% Plots the contour curves of a symbolic expression f(x,y)
syms x y; fcontour(x^3 -x*y - y^3, [-2 2 -2 2])

% figure
% Start a new graphic
 figure; syms x y; fsurf(x^2 + y^2, [-2 2 -2 2])
 
% fimplicit
% Produces a plot of an implicit equation f(x,y)=0
syms x y; fimplicit(x^3 - y^3 - sin(x*y), [-1 1 -1 1])

% fimplicit3
% Plots a contour surface of a symbolic expression f(x,y,z)
syms x y z; fimplicit3(x^3 - z^3 - sin(x*y*z), [-1 1 -1 1 -1 1])

% fmesh
% Easy 3D mesh surface plotter
fmesh(sin(x) + sin(y), [-1 1 -1 1])

% fminbnd
% Find minimum of single-variable function on fixed interval
f = @(x) x^4 +x^3 - 2*x^2 -x +4; fminbnd(f, -10, 10)

% fminsearch
% Searches numerically for a minimum of a function of a single (possibly vector) variable
fminsearch(f, -1)

% fminunc
% In the Optimization Toolbox. Searches for an unconstrained minimum of a 
% function of several variables
fminunc(f, -1)

% format
% Changes the format for displaying numbers
format long

% fplot
% Easy function plotter
 syms x; fplot(exp(x)*sin(4*x), [0 3])
 
% fplot3
% Easy 3D plotter for curves in 3-space
 syms t; fplot3(t,t*2, t^3, [0 2]), axis([0 8 0 8 0 8]), view([1 1 1])
 
% fsurf
% Easy 3D surface plotter
h = @(x, y) x.^3.*y - x.*y + x.*y.^3; fsurf(h, [-1 1 -1 1])

% fzero
% Finds (numerically) a zero of a function near a given starting value
g =@(x) -x^4 + x^3 + x + 1; fzero(g, 0)

% gca
% Get current axes
set(gca, 'Color', [1 1 0])

% gcf
% Get current figure
set(gcf, 'Color', [1 0 0])

% heaviside
% A step function that is +1 for x > 0 and 0 for x < 0
figure; fplot(@heaviside, [-1 1]), axis([-1 1 -2 2])

% hold on
% Retain current figure; combine new graph with previous one
figure; fplot(@sin, [-2*pi 2*pi]); hold on;  fplot(@cos, [-2*pi 2*pi])

% hold off
% Returns to the default where new figures overwrite old ones
figure; fplot(@sin, [-2*pi 2*pi]); hold off;  fplot(@cos, [-2*pi 2*pi])

% int
% Computes the integral (symbolically)
syms u t real; int(2*u*exp(-u^2), 0, t)

% integral
% Integrates numerically
f = @(u) exp(-u.^2); integral(f, 0, 10)

% integral2
% Computes a numerical integral in two variables
g = @(u, v) exp(-u.^2-v.^2); integral2(g, 0, 10, 0, 10)
% Why is the answer the square of the answer to the previous example?

% integral3
% Computes a numerical integral in three variables
h = @(u,v, w) exp(-u.^2 - 2.*v.^2 - 3.*w.^2); integral3(h, 0, 1, 0, 2, 0, 3)

% jacobian
% Finds the gradient of a function of several variables. When applied to 
% the symbolic expression for the gradient, can be used to compute the 
% Hessian
f = @(x, y) x*3 + y^2; jacobian(f(x, y))
syms x y f(x, y); jacobian(gradient(f(x, y)))

% laplacian
% Computes the Laplacian of a scalar field
f = @(x, y, z) x^2 + y^3 + z^4; laplacian(f(x, y, z))

% legend
% Attaches a legend to a combination of plots
figure; syms x; fplot(sin(x), [-2*pi 2*pi]), hold on; ...
    fplot(cos(x), [-2*pi 2*pi])
legend('sin x', 'cos x')

% limit
% Computes the limit
syms x; limit((x^3 - 1)/(x -1), x, 1)

% linspace
% Generates a linearly spaced vector
X = linspace(0, 5);

% log
% The natural logarithm
log(exp(34))

% loglog
% Log-log plotter
Y = logspace(0, 5); % see below
loglog(Y)

% logspace
% Generates a logarithmically spaced vector
Y = logspace(0, 5);

% matlabFunction
% Converts a symbolic expression to a vectorized numerical function
xycurv = x^2 + 4*y^2 == 4; ysol = solve(xycurv, y); ...
    yfunc = matlabFunction(ysol(1))

% max
% Computes the maximum of the entries of a vector
u = [sin(1), sin(2), sin(3)]; max(u)

% meshgrid
% Returns grid coordinates. Needed for quiver
[X, Y] = meshgrid(-1:0.1:1, -1:0.1:1);
quiver(X, Y, X.^2 +Y.^2, X.^2 - Y.^2)

% min
% Computes the minimum of the entries of a vector
min(u)

% norm
% Norm of a vector or matrix
u = [1 2 3]; v = [-3 -2 -1]; norm(u- v)

% ode45
% Numerical differential equation solver
[t, y] = ode45(@(t, y) y^3 -y + t, [0 1.5], 0.5); plot(t, y, '-')

% ones
% Creates a matrix of all ones
ones(2)
ones(1,2) % creates a vector, rather than matrix array of ones

% plot
% Basic plotting command for numerical data
X= 0:0.1:4*pi; plot(X, sin(X))

% plot3
% Basic plotting command in 3D for numerical data
T = 0:0.1:2; plot3(T, T.^2, T.^3)

% polarplot
% Plots a curve in polar coordinates
theta = 0:0.01:2*pi; rho = sin(2*theta).*cos(2*theta); polarplot(theta,rho)

% potential
% Computes a potential for a conservative vector field
syms x y z real; F = [cos(y), cos(z) - x*sin(y), -y*sin(z)]; potential(F)

% pretty
% Displays a symbolic expression in mathematical style
syms a b c x; solve(a*x^2 + b*x + c == 0, x), pretty(ans)

% quiver
% Plots vector field based on grid coordinates supplied by meshgrid
[X, Y] = meshgrid(-1:0.1:1, -1:0.1:1);
quiver(X, Y, X.^2 +Y.^2, X.^2 - Y.^2)

% rand
% Generates a matrix of uniformly distributed random numbers
rand(2)

% real
% Follows syms to insure variables are real
syms x real; solve(x^3 - 1 == 0, x)

% series
% Computes a series expansion
 =series(cos(x),x)
 
% simplify
% Used to simplify a symbolic expression
expand((x+2)^12), simlify(ans)


% sin
% The sine function
syms x; diff(sin(x),x)
fplot(sin(x), [-2*pi 2*pi])

% sinh
% The hyperbolic sine function (e^x - e^(-x))/2
syms x; int(sinh(x), 0, 5); fplot(sinh(x), [0 5])

% size
% Gets the dimensions of a matrix
A= rand(7); size(A)

% solve
% Symbolic equation solver
syms x; solve(x^2 - x - 5 == 0, x)

% sqrt
% Square root function
fplot(sqrt(x), [0 10])

% struct
% Creates a structure array
sa = struct; sa.integers = [1 2 3 4 5]; sa.squares = [1 4 9 16 25];
sa.description = 'perfect squares';
['These ' num2str(sa.squares) ' are the squares of these '...
    num2str(sa.integers)])

% subs
% Substitute for a variable in an expression
syms r s t; s = (r - 1)^2; subs(s, r, t)

% syms
% Set up one or more symbolic variables
% See previous example.

% symsum
% Finds the (symbolic) sum of a series
syms n; symsum(1/n^2, 1, inf)

% tan
% The tangent function
fplot(tan(x), [-pi pi])

% taylor
% Computes the Taylor polynomial; use the optional argument Order to 
% reset the order. Compare series, which is similar but allows expansions 
% involving negative and fractional exponents.
syms t; taylor(tan(t))

% title
% Puts a title on a figure
syms u; fplot(log(u), [0 10]), title 'log'

% trace
% Sum of the diagonal elements of a matrix
A= [1 2; 3 4]; trace(A)

% transpose
% Computes the transpose of a symbolic matrix
transpose(A)

% vectorPotential
% Computes a vector potential for a divergence-free vector field
syms x y z real; F = [-sin(y)-cos(x)*cos(z)*sin(y), cos(y)*cos(z), ...
    sin(y)*sin(z) - sin(x)*sin(y)*sin(z)];
simplify(divergence(F, [x, y, z]))
A=transpose(vectorPotential(F, [x,y,z]))

% view
% Specifies a point from which to view a 3D graph
syms t; fplot3(t, t^2, t^3), view([1 1 1])

% vpasolve
% Finds numerical solutions to symbolic equations
syms z; vpasolve(z^3 - z + 5 ==0, z)

% zeros
% Creates a matrix of all zeros
zeros(5)

%
%% Options to MATLAB Commands
% LevelList Specifies the contour levels for fcontour

f = @(x, y) (x^2 + y^2)^2 - x*2 + y^2
levels = [ 0.1 0.2 1 4 10]
fcontour(f(x, y), [-2 2 -2 2], 'LevelList', levels)

% LineColor
% Specifies color of curves in a graph
fcontour(f(x, y), [-2 2 -2 2], 'LineColor', 'r')

% LineWidth
% Controls thickness of contours in a graph
fcontour(f(x, y), [-2 2 -2 2], 'LineWidth', 3)

% MarkerSize
% Controls size of points in a graph
plot(0,0, '.k', 'MarkerSize', 20)

% MaxDegree
% An option to solve, giving the maximum degree of polynomials for which 
% MATLAB will try to find explicit solution formulas
syms x; f = x^3 - 4*x^2 + 1;
solve(f) % Ugh!
sols = solve(f, 'MaxDegree', 3), pretty(sols)

% MaxFunEvals
% Sets maximum number of function evaluations allowed in a computation 
% or process
f =@(x) x^80 + x^31 - x^2 + x + 1
options = optimset('MaxFunEvals', 4000);
fminbnd(f, -1,1, options)

% odeset
% Sets options for ode45
opts = odeset('RelTol',1e-7,'AbsTol',1e-8);
[tsol, wsol] = ode45(@(t, w) w^(5/2) - w^2 + w + exp(t), [0 1], 1, opts);
plot(tsol, wsol)
axis([0 .64 0 50]) 


% Order
% An option to taylor and series, specifying the number of terms to take 
% in the series
syms v; f=@(v) exp(v^2)*sin(v); taylor(f, v, 'Order', 10)

% Steps
% An option to the command simplify, specifying the number of 
% simplification steps to try
syms t; a = expand((sin(t) + sin(2*t))^10)
simplify(a)
simplify(a, 'Steps', 50)