Basic Operations

In [1]:
5+7
ans =

    12
In [2]:
6/4
ans =

    1.5000
In [3]:
5-6
ans =

    -1
In [4]:
2^4
ans =

    16
In [5]:
5*5
ans =

    25
In [6]:
sin(5)
ans =

   -0.9589
In [7]:
cos(5)
ans =

    0.2837
In [8]:
tan(pi)
ans =

  -1.2246e-16
In [9]:
pi
ans =

    3.1416
In [10]:
format long
In [11]:
pi
ans =

   3.141592653589793
In [12]:
format short
In [13]:
exp(1)
ans =

    2.7183
In [14]:
log(exp(1))
ans =

     1
In [15]:
log10(10)
ans =

     1
In [16]:
sinh(4)
ans =

   27.2899
In [17]:
tanh(3)
ans =

    0.9951
In [18]:
x1 = sqrt(100^2 -60^2)
y1 = x1*15*(60^2 + x1^2)^(-1/2)
x1 =

    80


y1 =

    12

vector and matrix

In [19]:
% row vector
x=[1 2]
x =

     1     2
In [20]:
% column vector 
y=[3;4]
y =

     3
     4
In [21]:
% transpose
y'
ans =

     3     4
In [22]:
y=[3 4]
y =

     3     4
In [23]:
A=[1 2; 3 4]
A =

     1     2
     3     4

$A*A$ and A^2 are same but A.^2 is different! $A*A$ or A^2 are the multiplication of A with A; however, A.^2 is the square of each element of A.

In [24]:
A*A
A^2
A.^2
ans =

     7    10
    15    22


ans =

     7    10
    15    22


ans =

     1     4
     9    16

Element-wise operations

$.*$
$./$
$.^$
are element-wise operations.

In [25]:
P = [2, 4]; Q = [3, 2];
P.*Q
P./Q
P.^Q
ans =

     6     8


ans =

    0.6667    2.0000


ans =

     8    16
In [26]:
B = [5 6; 7 8]
B =

     5     6
     7     8
In [27]:
C=B^(-1)
C =

   -4.0000    3.0000
    3.5000   -2.5000
In [28]:
A*C
ans =

    3.0000   -2.0000
    2.0000   -1.0000
In [29]:
B*C
ans =

    1.0000    0.0000
         0    1.0000
In [30]:
% component wise multiplication 
B.*C
ans =

  -20.0000   18.0000
   24.5000  -20.0000
In [31]:
inv(B)
ans =

   -4.0000    3.0000
    3.5000   -2.5000
In [32]:
% determinant
det(B)
ans =

   -2.0000
In [33]:
help eig
EIG    Eigenvalues and eigenvectors.
    E = EIG(A) produces a column vector E containing the eigenvalues of 
    a square matrix A.
 
    [V,D] = EIG(A) produces a diagonal matrix D of eigenvalues and 
    a full matrix V whose columns are the corresponding eigenvectors  
    so that A*V = V*D.
  
    [V,D,W] = EIG(A) also produces a full matrix W whose columns are the
    corresponding left eigenvectors so that W'*A = D*W'.
 
    [...] = EIG(A,'nobalance') performs the computation with balancing
    disabled, which sometimes gives more accurate results for certain
    problems with unusual scaling. If A is symmetric, EIG(A,'nobalance')
    is ignored since A is already balanced.
 
    [...] = EIG(A,'balance') is the same as EIG(A).
 
    E = EIG(A,B) produces a column vector E containing the generalized 
    eigenvalues of square matrices A and B.
 
    [V,D] = EIG(A,B) produces a diagonal matrix D of generalized
    eigenvalues and a full matrix V whose columns are the corresponding
    eigenvectors so that A*V = B*V*D.
 
    [V,D,W] = EIG(A,B) also produces a full matrix W whose columns are the
    corresponding left eigenvectors so that W'*A = D*W'*B.
 
    [...] = EIG(A,B,'chol') is the same as EIG(A,B) for symmetric A and
    symmetric positive definite B.  It computes the generalized eigenvalues
    of A and B using the Cholesky factorization of B.
 
    [...] = EIG(A,B,'qz') ignores the symmetry of A and B and uses the QZ
    algorithm. In general, the two algorithms return the same result,
    however using the QZ algorithm may be more stable for certain problems.
    The flag is ignored when A or B are not symmetric.
 
    [...] = EIG(...,'vector') returns eigenvalues in a column vector 
    instead of a diagonal matrix.
 
    [...] = EIG(...,'matrix') returns eigenvalues in a diagonal matrix
    instead of a column vector.
 
    See also CONDEIG, EIGS, ORDEIG.

    Reference page in Doc Center
       doc eig

    Other functions named eig

       sym/eig
In [34]:
A=[1 2 3; 4 5 6; 7 8 98]
A =

     1     2     3
     4     5     6
     7     8    98
In [35]:
[eigvec, eigval] = eig(A)
eigvec =

   -0.0319   -0.8127   -0.3296
   -0.0652    0.5826   -0.9381
   -0.9974    0.0104    0.1063


eigval =

   98.7472         0         0
         0   -0.4723         0
         0         0    5.7251

$\left[ \begin{array}{ccc} a_{1,1} & a_{1,2} & a_{1,3}\\ a_{2,1} & a_{2,2} & a_{2,3}\\ a_{3,1} & a_{3,2} & a_{3,3} \end{array} \right]$

In [36]:
A(1:3,3)
ans =

     3
     6
    98
In [37]:
size(A)
ans =

     3     3
In [38]:
A=zeros(3)
A =

     0     0     0
     0     0     0
     0     0     0
In [39]:
%identity matrix
A = eye(4)
A =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1
In [40]:
A=rand(4)
A =

    0.8147    0.6324    0.9575    0.9572
    0.9058    0.0975    0.9649    0.4854
    0.1270    0.2785    0.1576    0.8003
    0.9134    0.5469    0.9706    0.1419

Solve the linear system $Ax =b$.

For example, solve the following system of linear equations:
x+2y+3z = 1
3x+3y+4z = 1
2x+3y+3z = 2

Check the determinant whether the system has a unique solution or not. If $det(A)\ne 0$ then the system has unique solution.

In [42]:
A=[1 2 3; 3 3 4; 2 3 3];
det(A)
ans =

     4
In [43]:
b=[1; 1; 2];
x=A\b
x =

   -0.5000
    1.5000
   -0.5000

more vectors

In [44]:
x = 1:5
x =

     1     2     3     4     5
In [45]:
x = 1:0.5:5
x =

  Columns 1 through 7

    1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000

  Columns 8 through 9

    4.5000    5.0000
In [46]:
length(x)
ans =

     9
In [47]:
x = linspace(0,5,11)
x =

  Columns 1 through 7

         0    0.5000    1.0000    1.5000    2.0000    2.5000    3.0000

  Columns 8 through 11

    3.5000    4.0000    4.5000    5.0000
In [48]:
index = find(x>2)
index =

     6     7     8     9    10    11
In [49]:
x(index)
ans =

    2.5000    3.0000    3.5000    4.0000    4.5000    5.0000

run a script file

For example plottest.m is an script file. plottest.m contains the following three lines:
x = 0:0.1:10;
y = sin(x);
plot(x,y)
We can run plottest.m by simply write plottest

In [50]:
plottest