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.95892
In [7]:
cos(5)
ans =  0.28366
In [8]:
tan(pi)
ans =   -1.2246e-16
In [9]:
pi
ans =  3.1416
In [10]:
format long
In [11]:
pi
ans =  3.14159265358979
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.290
In [17]:
tanh(3)
ans =  0.99505
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.66667   2.00000

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.00000  -2.00000
   2.00000  -1.00000

In [29]:
B*C
ans =

   1.00000   0.00000
   0.00000   1.00000

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

  -20.000   18.000
   24.500  -20.000

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' is a built-in function from the file libinterp/corefcn/eig.cc

 -- Built-in Function: LAMBDA = eig (A)
 -- Built-in Function: LAMBDA = eig (A, B)
 -- Built-in Function: [V, LAMBDA] = eig (A)
 -- Built-in Function: [V, LAMBDA] = eig (A, B)
     Compute the eigenvalues (and optionally the eigenvectors) of a
     matrix or a pair of matrices

     The algorithm used depends on whether there are one or two input
     matrices, if they are real or complex, and if they are symmetric
     (Hermitian if complex) or non-symmetric.

     The eigenvalues returned by 'eig' are not ordered.

     See also: eigs, svd.

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.
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.031944  -0.812694  -0.329567
  -0.065196   0.582597  -0.938125
  -0.997361   0.010440   0.106334

eigval =

Diagonal Matrix

   98.74715          0          0
          0   -0.47228          0
          0          0    5.72513

$\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 =

Diagonal Matrix

   1   0   0   0
   0   1   0   0
   0   0   1   0
   0   0   0   1

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

   0.565667   0.170590   0.753285   0.015079
   0.297896   0.576122   0.617942   0.030589
   0.369796   0.390534   0.742819   0.707328
   0.235775   0.107872   0.369441   0.085121

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 [41]:
A=[1 2 3; 3 3 4; 2 3 3];
det(A)
ans =  4.0000
In [42]:
b=[1; 1; 2];
x=A\b
x =

  -0.50000
   1.50000
  -0.50000

more vectors

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

   1   2   3   4   5

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

 Columns 1 through 8:

    1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000    4.5000

 Column 9:

    5.0000

In [45]:
length(x)
ans =  9
In [46]:
x = linspace(0,5,11)
x =

 Columns 1 through 8:

   0.00000   0.50000   1.00000   1.50000   2.00000   2.50000   3.00000   3.50000

 Columns 9 through 11:

   4.00000   4.50000   5.00000

In [47]:
index = find(x>2)
index =

    6    7    8    9   10   11

In [48]:
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]:
%plot gnuplot
plottest