programming_language
Plotting
2-D
Assume that Y is the function of X.
plot(X, Y, 'o')
To add text to the graph, specify "title", "legend", "xlabel", "ylabel".
"hold on" and "hold off" to control the same figure.
3-D
Assume that Z is the function of X and Y.
surf(X, Y, Z)
Dimensionality reduction to 2-D.
figure
[c,h] = contour(X,Y,Z);
clabel(c,h);
subgraph
subplot(n, m, i)
Piece the whole figure into n rows & m columns. i refers to the i-th sub-figure.
Loop
for k = 1:m
sth...
end
Condiction
if condiction_1
do_sth
elseif condiction_2
do_sth
else
do_sth
end
Get help
doc
+ command shows the document of the command.
help
+ command shows the instruction of the command.
Matrix Manipulation
a = [1 2 3; 4,5,6]
a'
transpose inv(a)
invertion .
before mean "element-wise".[a, a]
extends horizontally. [a; a]
extends vertically.indexing
Matlab index from ONE, not zero.
To access an element in a matrix, a tuple (row, col) is needed.
The usage of the colon start:end
or start:step:end
A single colon means "ALL".
workspace
whos
examines the info of all variables in the workspace.
save
+ file_name.mat saves the workspace.
load
+ file_name.mat loads it.
Minimizing Functions of One Variable
fminbnd(@fun_name, lower, upper, opt)
A chosen opt means setting like optimset('Display','iter')
.
Or options = optimset('PlotFcns',@optimplotfval)
to plot the iteration.
Standard return: [optimal_x, optimal_f, exit_flag, output_massage]
Minimizing Functions of Several Variables
fminsearch(@fun_name, init_values)
It it better to define the input variable of a function as a compressed one and decompose it inside the function for convenience of initialization.
Finding the zero point
fzero(@fun_name, [low high], opt)
Unconstraint minimization
fminunc(@f, init_value)
Min-Max Optimization
fminimax(@f, init_value, A, b, Aeq, beq, lb, ub, nonlcon, opt)
f
is a vector-value function. Min-Max optimization returns the maximum value among the function values with the minimum x. Constraint Optimization
fmincon(@f, init, A, b, Aeq, beq, lb, ub, nonlcon, opt)
Nonlinear Least Square Optimization
lsqnonlin(@f, init, lb, ub, opt)
return [x, f^2, f, flag, output]
Linear Programming
linprog(@f, A, b, Aeq, beq, lb, ub, init, opt)
Integer Programming
intlinprog(@f, intcon, A, b, Aeq, beq, lb, ub, opt)
intcon
is the indices of integers in matrix A
.
Quadratic Programming
quadprog(H, f, A, b, Aeq, beq, lb, ubm init, opt)
One other interesting characteristic is that the interpolating function passes through the data points. This is an important distinction between interpolation and curve/surface fitting. In fitting, the function does not necessarily pass through the sample data points.
Cubic Spline Interplotation.
Xq = (start:step:end)
Vq = interp1(X, Y, Xq, 'spline')
So the Vq=F(Xq) specifies a interplotation function.
2-D Cubic Spline Interplotation.
set up a 2-D mesh grid
[Xq, Yq] = meshgrid(start: step: end);
Vq = interp2(X, Y, Z, Xq, Yq, 'spline')
Interpolating Scattered Data
Zq = griddata(X, Y, Z, Xq, Yq);
Define symbolic variables
syms x y z
Symbolic variables can be used to define expressions, like x+y
and 4*z^2-1
.
Define a function of x using sym f(x)
.
f(x) = x^2 + x + 1
Matrices, of course.
Solve an equation.
solve(x^4 == 1)
Solve with assumptions. assume(x, x>0)
The second assumption. assumeAlso(x, 'real')
.
Clear all assumptions on x. assume(x, 'clear')
Substitution
subs(x^2, y)
substitute all x with y.
subs(x+y, [x y], [1 2])
Simplify an expression
simplify(x^2+x*2+1)
Similarily, combine(2*cos(x)*sin(x), 'sincos')
Factorization
factor(x^3-y^3)
Expand an expression.
expand((x - y)^3)
Functional composition
f(x) =
and g(x)=
.
compose(f, g, x)
differentiation
diff(f(x), x)
integration
int(f(x), x)
int(f(x), x, 0, 1)
x from 0 to 1
Taylor Expansion
taylor(f(x))
Limitation
limit(f(x), x, c, 'left')
the left limitation of f(x) at x=c
dsolve(diff(y, x) == -a*y)
with initial condition
dsolve(diff(y, x) == -a*y, y(0)==c)