2. About MATLAB¶
- Very popular high-level programming language
- Powerful numerical computing environment
- Highly popular in engineering and science
3. Elementary Tutorial¶
3.1. General Commands¶
In [3]:
clc %Clears the command window
In [4]:
clear %removes all variables from the workspace
In [5]:
clear all %removes all variables, globals, functions and MEX links
In [6]:
ls
. .. .ipynb_checkpoints MATLAB Introduction.ipynb
In [7]:
!explorer
(jmatlab)
In [8]:
pwd
ans =
'C:\Users\Karthik\Dropbox (Personal)\Programming\Jupyter\matlab'
In [9]:
whos
Name Size Bytes Class Attributes
ans 1x62 124 char
In [10]:
!ls
'ls' is not recognized as an internal or external command,
operable program or batch file.
(jmatlab)
In [11]:
!dir
Volume in drive C is Local Disk
Volume Serial Number is D649-B68C
Directory of C:\Users\Karthik\Dropbox (Personal)\Programming\Jupyter\matlab
06-Jan-18 13:41 <DIR> .
06-Jan-18 13:41 <DIR> ..
06-Jan-18 13:36 <DIR> .ipynb_checkpoints
06-Jan-18 13:41 3,239 MATLAB Introduction.ipynb
1 File(s) 3,239 bytes
3 Dir(s) 95,040,851,968 bytes free
(jmatlab)
In [13]:
lookfor linprog
intlinprog - Mixed integer linear programming.
linprog - Linear programming.
optimplotmilp - plot function for the intlinprog solver.
savemilpsolutions - save all the integer solution found by intlinprog.
In [14]:
help linprog
LINPROG Linear programming.
X = LINPROG(f,A,b) attempts to solve the linear programming problem:
min f'*x subject to: A*x <= b
x
X = LINPROG(f,A,b,Aeq,beq) solves the problem above while additionally
satisfying the equality constraints Aeq*x = beq. (Set A=[] and B=[] if
no inequalities exist.)
X = LINPROG(f,A,b,Aeq,beq,LB,UB) defines a set of lower and upper
bounds on the design variables, X, so that the solution is in
the range LB <= X <= UB. Use empty matrices for LB and UB
if no bounds exist. Set LB(i) = -Inf if X(i) is unbounded below;
set UB(i) = Inf if X(i) is unbounded above.
X = LINPROG(f,A,b,Aeq,beq,LB,UB,X0) sets the starting point to X0. This
option is only available with the active-set algorithm. The default
interior point algorithm will ignore any non-empty starting point.
X = LINPROG(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a
structure with the vector 'f' in PROBLEM.f, the linear inequality
constraints in PROBLEM.Aineq and PROBLEM.bineq, the linear equality
constraints in PROBLEM.Aeq and PROBLEM.beq, the lower bounds in
PROBLEM.lb, the upper bounds in PROBLEM.ub, the start point
in PROBLEM.x0, the options structure in PROBLEM.options, and solver
name 'linprog' in PROBLEM.solver. Use this syntax to solve at the
command line a problem exported from OPTIMTOOL.
[X,FVAL] = LINPROG(f,A,b) returns the value of the objective function
at X: FVAL = f'*X.
[X,FVAL,EXITFLAG] = LINPROG(f,A,b) returns an EXITFLAG that describes
the exit condition. Possible values of EXITFLAG and the corresponding
exit conditions are
1 LINPROG converged to a solution X.
0 Maximum number of iterations reached.
-2 No feasible point found.
-3 Problem is unbounded.
-4 NaN value encountered during execution of algorithm.
-5 Both primal and dual problems are infeasible.
-7 Magnitude of search direction became too small; no further
progress can be made. The problem is ill-posed or badly
conditioned.
[X,FVAL,EXITFLAG,OUTPUT] = LINPROG(f,A,b) returns a structure OUTPUT
with the number of iterations taken in OUTPUT.iterations, maximum of
constraint violations in OUTPUT.constrviolation, the type of
algorithm used in OUTPUT.algorithm, the number of conjugate gradient
iterations in OUTPUT.cgiterations (= 0, included for backward
compatibility), and the exit message in OUTPUT.message.
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA] = LINPROG(f,A,b) returns the set of
Lagrangian multipliers LAMBDA, at the solution: LAMBDA.ineqlin for the
linear inequalities A, LAMBDA.eqlin for the linear equalities Aeq,
LAMBDA.lower for LB, and LAMBDA.upper for UB.
NOTE: the interior-point (the default) algorithm of LINPROG uses a
primal-dual method. Both the primal problem and the dual problem
must be feasible for convergence. Infeasibility messages of
either the primal or dual, or both, are given as appropriate. The
primal problem in standard form is
min f'*x such that A*x = b, x >= 0.
The dual problem is
max b'*y such that A'*y + s = f, s >= 0.
See also QUADPROG.
Reference page in Doc Center
doc linprog
In [15]:
doc linprog
In [18]:
hist(rand(1000,1));
title('Histogram')
ylabel('Frequency')
In [23]:
A=randi(3,[3 2])
A =
2 1
1 1
3 2
In [26]:
size(A)
ans =
3 2
In [27]:
A'
ans =
2 1 3
1 1 2
In [28]:
size(A')
ans =
2 3
In [29]:
numel(A)
ans =
6
In [30]:
prod(size(A))
ans =
6
In [31]:
x = 1:7:100
x =
1 8 15 22 29 36 43 50 57 64 71 78 85 92 99
In [42]:
y = linspace(0,100,11)
y =
0 10 20 30 40 50 60 70 80 90 100
In [52]:
log10(logspace(1,3,7))
ans =
1.0000 1.3333 1.6667 2.0000 2.3333 2.6667 3.0000
In [53]:
linspace(1,3,7)
ans =
1.0000 1.3333 1.6667 2.0000 2.3333 2.6667 3.0000
In [54]:
10.^linspace(1,3,7)
ans =
1.0e+03 *
0.0100 0.0215 0.0464 0.1000 0.2154 0.4642 1.0000
In [55]:
logspace(1,3,7)
ans =
1.0e+03 *
0.0100 0.0215 0.0464 0.1000 0.2154 0.4642 1.0000
In [56]:
A = [1:5; 6:10; 11:15]
A =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
In [57]:
A(1:3,:)
ans =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
In [58]:
A(:,1:3)
ans =
1 2 3
6 7 8
11 12 13
In [59]:
A(:,:)
ans =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
In [60]:
A(1,3)
ans =
3
In [61]:
A(5)
ans =
7
In [63]:
A([1 3],[2 4])
ans =
2 4
12 14
In [64]:
A = zeros(10)
A =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
In [65]:
A = ones(10)
A =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
In [66]:
A = eye(10)
A =
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
In [67]:
find(A)
ans =
1
12
23
34
45
56
67
78
89
100
In [68]:
[i, j] = find(A)
i =
1
2
3
4
5
6
7
8
9
10
j =
1
2
3
4
5
6
7
8
9
10
In [69]:
A = rand(5)
A =
0.4759 0.2828 0.6101 0.4429 0.0782
0.3683 0.2052 0.2036 0.5480 0.4564
0.6556 0.4391 0.5199 0.5669 0.0478
0.9382 0.0273 0.0538 0.6804 0.7383
0.6204 0.8762 0.8622 0.3714 0.0380
In [70]:
A = randi(5,[5 5])
A =
5 2 1 1 4
4 4 3 3 5
5 5 3 4 2
3 2 4 1 3
2 4 2 3 5
In [71]:
A(A<=3) = 0
A =
5 0 0 0 4
4 4 0 0 5
5 5 0 4 0
0 0 4 0 0
0 4 0 0 5
In [72]:
gallery
File C:\Program Files\MATLAB\R2017a\toolbox\matlab\elmat\gallery.m, line 108, in gallery
Not enough input arguments.
In [73]:
gallery(5)
ans =
-9 11 -21 63 -252
70 -69 141 -421 1684
-575 575 -1149 3451 -13801
3891 -3891 7782 -23345 93365
1024 -1024 2048 -6144 24572
In [74]:
help gallery
GALLERY Higham test matrices.
[out1,out2,...] = GALLERY(matname, param1, param2, ...)
takes matname, a string that is the name of a matrix family, and
the family's input parameters. See the listing below for available
matrix families. Most of the functions take an input argument
that specifies the order of the matrix, and unless otherwise
stated, return a single output.
GALLERY(matname,param1, param2, ..., CLASSNAME) produces a matrix
of class CLASSNAME, which must be either 'single' or 'double' (unless
matname is 'integerdata', in which case 'int8', 'int16', 'int32',
'uint8', 'uint16', and 'uint32' are also allowed.)
If CLASSNAME is not specified then the class of the matrix is
determined from those arguments among param 1, param2, ..., that do
not specify dimensions or select an option:
if any of these arguments is of class single then the matrix is
single; otherwise the matrix is double.
For additional information, type "help private/matname", where matname
is the name of the matrix family.
binomial Binomial matrix -- multiple of involutory matrix.
cauchy Cauchy matrix.
chebspec Chebyshev spectral differentiation matrix.
chebvand Vandermonde-like matrix for the Chebyshev polynomials.
chow Chow matrix -- a singular Toeplitz lower Hessenberg matrix.
circul Circulant matrix.
clement Clement matrix -- tridiagonal with zero diagonal entries.
compar Comparison matrices.
condex Counter-examples to matrix condition number estimators.
cycol Matrix whose columns repeat cyclically.
dorr Dorr matrix -- diagonally dominant, ill-conditioned, tridiagonal.
(One or three output arguments, sparse)
dramadah Matrix of ones and zeroes whose inverse has large integer entries.
fiedler Fiedler matrix -- symmetric.
forsythe Forsythe matrix -- a perturbed Jordan block.
frank Frank matrix -- ill-conditioned eigenvalues.
gcdmat GCD matrix.
gearmat Gear matrix.
grcar Grcar matrix -- a Toeplitz matrix with sensitive eigenvalues.
hanowa Matrix whose eigenvalues lie on a vertical line in the complex
plane.
house Householder matrix. (Three output arguments)
integerdata Array of arbitrary data from uniform distribution on
specified range of integers
invhess Inverse of an upper Hessenberg matrix.
invol Involutory matrix.
ipjfact Hankel matrix with factorial elements. (Two output arguments)
jordbloc Jordan block matrix.
kahan Kahan matrix -- upper trapezoidal.
kms Kac-Murdock-Szego Toeplitz matrix.
krylov Krylov matrix.
lauchli Lauchli matrix -- rectangular.
lehmer Lehmer matrix -- symmetric positive definite.
leslie Leslie matrix.
lesp Tridiagonal matrix with real, sensitive eigenvalues.
lotkin Lotkin matrix.
minij Symmetric positive definite matrix MIN(i,j).
moler Moler matrix -- symmetric positive definite.
neumann Singular matrix from the discrete Neumann problem (sparse).
normaldata Array of arbitrary data from standard normal distribution
orthog Orthogonal and nearly orthogonal matrices.
parter Parter matrix -- a Toeplitz matrix with singular values near PI.
pei Pei matrix.
poisson Block tridiagonal matrix from Poisson's equation (sparse).
prolate Prolate matrix -- symmetric, ill-conditioned Toeplitz matrix.
qmult Pre-multiply matrix by random orthogonal matrix.
randcolu Random matrix with normalized cols and specified singular values.
randcorr Random correlation matrix with specified eigenvalues.
randhess Random, orthogonal upper Hessenberg matrix.
randjorth Random J-orthogonal (hyperbolic, pseudo-orthogonal) matrix.
rando Random matrix with elements -1, 0 or 1.
randsvd Random matrix with pre-assigned singular values and specified
bandwidth.
redheff Matrix of 0s and 1s of Redheffer.
riemann Matrix associated with the Riemann hypothesis.
ris Ris matrix -- a symmetric Hankel matrix.
sampling Nonsymmetric matrix with integer, ill conditioned eigenvalues.
smoke Smoke matrix -- complex, with a "smoke ring" pseudospectrum.
toeppd Symmetric positive definite Toeplitz matrix.
toeppen Pentadiagonal Toeplitz matrix (sparse).
tridiag Tridiagonal matrix (sparse).
triw Upper triangular matrix discussed by Wilkinson and others.
uniformdata Array of arbitrary data from standard uniform distribution
wathen Wathen matrix -- a finite element matrix (sparse, random entries).
wilk Various specific matrices devised/discussed by Wilkinson.
(Two output arguments)
GALLERY(3) is a badly conditioned 3-by-3 matrix.
GALLERY(5) is an interesting eigenvalue problem. Try to find
its EXACT eigenvalues and eigenvectors.
See also MAGIC, HILB, INVHILB, HADAMARD, PASCAL, ROSSER, VANDER, WILKINSON.
Reference page in Doc Center
doc gallery
In [75]:
hilb(5)
ans =
1.0000 0.5000 0.3333 0.2500 0.2000
0.5000 0.3333 0.2500 0.2000 0.1667
0.3333 0.2500 0.2000 0.1667 0.1429
0.2500 0.2000 0.1667 0.1429 0.1250
0.2000 0.1667 0.1429 0.1250 0.1111
In [95]:
x = hilb(5)\ones(5,1)
x =
1.0e+03 *
0.0050
-0.1200
0.6300
-1.1200
0.6300
In [97]:
hilb(5)*x
ans =
1.0000
1.0000
1.0000
1.0000
1.0000
The inline function can create simple functions inline, like
\(f(x, y) = \sin(x^2+y^2+xy)\)
In [2]:
f = inline('sin(x.^2+y.^2+x.*y)')
f =
Inline function:
f(x,y) = sin(x.^2+y.^2+x.*y)
In [6]:
f(sqrt(pi),sqrt(pi))
ans =
2.1438e-15
In [5]:
format
In [7]:
sin(3*pi)
ans =
3.6739e-16
In [1]:
x.*x
Undefined function or variable 'x'.
In [2]:
x = 1:10
x =
1 2 3 4 5 6 7 8 9 10
In [3]:
x*x
Inner matrix dimensions must agree.
In [4]:
x.*x
ans =
1 4 9 16 25 36 49 64 81 100
4. SPECIAL MATRICES¶
There are number of built-in special matrices in MATLAB, for producing matrices that are all zero, all ones, the identity matrix etc.
In [1]:
A = zeros(10)
A =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
In [2]:
A = ones(10)
A =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
In [3]:
B = eye(8,9)
B =
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
In [4]:
diag(B)
ans =
1
1
1
1
1
1
1
1
In [6]:
diag(diag(B))
ans =
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
5. INPUT/OUTPUT Related Stuff¶
In [7]:
x = 1
x =
1
In [8]:
x = 1; % Suppresses output
In [9]:
disp(x)
1
In [10]:
fprintf('%d', x)
1
In [11]:
a = sprintf('%d', x);
a
a =
'1'
In [12]:
load('test_matrix.txt')
In [13]:
whos
Name Size Bytes Class Attributes
A 10x10 800 double
B 8x9 576 double
a 1x1 2 char
ans 8x8 512 double
test_matrix 3x4 96 double
x 1x1 8 double
In [14]:
test_matrix
test_matrix =
1 2 3 4
5 6 7 8
9 10 11 12
Note that load will not load a file that does not have equal number
of entries in every row.
5.1. Other Useful Functions¶
importdata: Useful for loading text files and other files you would usually load using ‘File>Import…’xlsread: MATLAB can read XLS files directly