Examples

Basic Operations

a = eye(5);

b = zeros(5, 4);
c = ones(3, 5);

r = rand(3, 5);

l = linspace(1, 36, 12);

e = [1, 3; 2, -2];
a = eye(2);

b = [1, 3; 2, -2];

c = [1, 2; 3, 4];

d = b * c + a;

e = b ./ c;

f = det(d);
b = [1, 3, 4; 2, -2, 2; sin(pi / 3) cos(pi / 3) 1];

d = [ -1, 1, -2 ; 2, -2, 6 ; 7, 3, -3];

d[0, :] = d[2, :];

e = 2 * d - 4 * b - 1;
speedy = rand(100, 100);
ts = trans(speedy);
s = inv(ts);

Linear systems

A = [5,-6, 1;
     2, 4, 0;
     0, 5, 6];

b = [-1; 2; 3];

x = linsolve(A, b);

disp(A * x - b);

/// Another example
A = rand(100, 100);
b = rand(100, 1) + 3;

x = linsolve(A, b);

err = A * x - b;

disp(trans(err) * err);

Dct

a = eye(8) + 15;
a[0, 1] = 2;
a[1, 0] = 2; // non trivial 'a' matrix

b = zeros(8, 8);
loop(i, 0, 8)
{
  loop0(j, 8)
  {
    if(i==0)
    {
      s=sqrt(1.0 / 8.0);
    }
    else
    {
      s=sqrt(2.0 / 8.0);
    }

    if(j==0)
    {
      d=sqrt(1.0 / 8.0);
    }
    else
    {
      d=sqrt(2.0 / 8.0);
    }

    loop0(k, 8)
    {
      loop0(l, 8)
      {
        b[i][j] = b[i][j] + a[k][l] *
                  cos(((2 * k + 1) * i * pi) /(2 * 8)) * cos((( 2 * l + 1) * j * pi)/(2 * 8));
      }
    }

    b[i][j] = b[i][j] * s * d;
  }
}

Singular Value Decomposition

M  = [2.655,  0.3959, 2.044;
      0.9232, 3.959,  1.681;
      2.488,  2.897,  1.076];

[u s v] = svd(M);

disp(M - u * diag(s) * trans(v));

Eigenvalues/vectors

M  = [2.655,  0.3959, 2.044;
      0.9232, 3.959,  1.681;
      2.488,  2.897,  1.076];

[l v] = eig(M);

v1 = v[:,0];
disp(M * v1 - l[0] * v1);

v2 = v[:,1];
disp(M * v2 - l[1] * v2);

v3 = v[:,2];
disp(M * v3 - l[2] * v3);

Plot

x = zeros(37, 1);
y = zeros(37, 1);
loop0(i, 37)
{
   x[i][0] = 20 * sin(i * 10 * pi / 180);
   y[i][0] = 30 * cos(i * 10 * pi / 180);
}

plot(x, y);

Lorenz Attractor

// Lorenz Attractor
iter = 1000;

x = zeros(iter, 1);
y = zeros(iter, 1);
z = zeros(iter, 1);

alpha = 10;
beta = 8 / 3;
p = 28;
x[0] = 0.01;
dt = 0.01;

loop(i, 0 , iter - 1)
{
  xdot = alpha * (y[i] - x[i]);
  ydot = x[i] * (p - z[i]) - y[i];
  zdot = x[i] * y[i,0] - beta * z[i];

  // integrate
  x[i+1] = x[i] + dt * xdot;
  y[i+1] = y[i] + dt * ydot;
  z[i+1] = z[i] + dt * zdot;
}

plot(x, y);

Images

preload
{
  "http://upload.wikimedia.org/wikipedia/en/thumb/2/24/Lenna.png/220px-Lenna.png" as "lenna",
  "http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Adult_Long-tailed_fiscal.jpg/468px-Adult_Long-tailed_fiscal.jpg" as "bird"
}

[r g b] = imread("lenna");
[x y z] = imread("http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Adult_Long-tailed_fiscal.jpg/468px-Adult_Long-tailed_fiscal.jpg");
imshow(x, y, z);

Functions

///// simple function example
function addition(a, b)
{
  r = a + b;
  return r;
}

///// recursive function example
function f(a)
{
  disp(a);
  a = a - 1;
  if (a > 0)
  {
    f(a);
  }

  return 0;
}

///// main
f(20);

y = addition(55, 22);


Contact:
mathcortex@gmail.com
@mathcortex on Twitter.

Gorkem Gencay
Copyright (c) 2012-2014