How to install both Python 2.x and 3.x on you machine?

First install Python 2.x using the windows installer from here.
Make sure, you tick the box that says add python.exe to your path.

Let’s say you install it at: c:\python27

The installer will add python to the system’s Path and will edit your environment variables to include C:\Python27\;C:\Python27\Scripts\;

Now you should install python 3.x using the windows installer.
This time make sure the add python.exe to your path remains unchecked.

Let’s say you install it at c:\Python35
Now go into this folder and rename the following files:

  • python.exe –> python3.exe
  • pythonw.exe –> pythonw3.exe

The next step is adding the following string to your path manually.
C:\Python35\;C:\Python35\Scripts\;

Now, in a command line, you can type python and the python 2.x interpreter will start,

or enter python3 and python 3.x interpreter will start. (you might need to restart before the path is reloaded to the memory)

Posted in Linux, programming, Python, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , | Leave a comment

SO(3) vs. so(3)

SO(3) – Definition

The set of all 3\times3 rotation matrices, \mathbb{R}^{3 \times 3}, which satisfy the following two properties is denoted SO(3) ,

  1. det(R)=+1
  2. RR^T = R^TR = I

The notation SO refers to Special Orthogonal.

More generally, we can define the space of rotation matrices in \mathbb{R}^{n\times n} by

SO(n)=\{ R \in \mathbb{R}^{n \times n} : RR^T=I, det(R)=+1\}

so(3) – Definition

The vector space of all 3 \times 3 skew matrices is denoted so(3) and more generally the space of n \times n skew-symmetric matrices is

so(n)=\{ S \in \mathbb{R}^{n \times n} : S^T=-S\}

Relationship between SO(3)  and so(3)

Every rotation matrix can be represented as the matrix exponential of some skew-symmetric matrix; that is the map exp: so(3) \rightarrow SO(3) is surjective.

In other words, given R \in SO(3), there exists \omega \in \mathbb{R}^{n \times n}, \| \omega \| = 1, and \theta \in \mathbb{R} such that R=\exp(\hat{\omega}\theta).

Example

\exp(\hat{x}\theta)  = \left[ \begin{array}{ccc} 1 & 0 & 0 \\ 0 & \cos(\theta) & -\sin(\theta) \\ 0 & \sin(\theta) & \cos(\theta) \end{array} \right]

Posted in Machine Learning, Robotics | Tagged , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Installing necessary modules on python – (whl version)

There are several very nice python packages that I use everyday. They include: numpy, scipy, matplotlib, …I used to install these packages on windows using pre-built binary installers.

Recently, I have noticed that those installers are being disappeared and in order to keep them updated you need either to install very large packages such as Anaconda or build the packages on your machine from source.

Then I found Unofficial Windows Binaries for Python Extension Packages. Here you can find almost all existing packages for python as whl files.

This page provides 32- and 64-bit Windows binaries of many scientific open-source extension packages for the official CPython distribution of the Python programming language. The files are unofficial (meaning: informal, unrecognized, personal, unsupported, no warranty, no liability, provided “as is”) and made available for testing and evaluation purposes. Most binaries are built from source code found on PyPI or in the projects public revision control systems. Source code changes, if any, have been submitted to the project maintainers or are included in the packages. You should use pip version 8 or newer to install the downloaded .whl files.

Since many binaries depend on numpy+mkl, install it before other packages that depend on it. As you can notice, the numpy has changed to numpy+mkl.
Numpy+MKL
is linked to the Intel® Math Kernel Library and includes required DLLs in the numpy.core directory.

Step 1. Download the whl files you need (for instance on python2.7 32bit):

numpy-1.11.3+mkl-cp27-cp27m-win32.whl
scipy-0.19.0-cp27-cp27m-win32.whl
matplotlib-1.5.3-cp27-cp27m-win32.whl

Step 2. Run a command window as administrator in windows

Window+R
Type: cmd
R-click
select Run as Administrator

Step 3. Install the whl files one by one:

pip install numpy-1.11.3+mkl-cp27-cp27m-win32.whl
pip install scipy-0.19.0-cp27-cp27m-win32.whl
pip install matplotlib-1.5.3-cp27-cp27m-win32.whl

Step 4. Test in a command-line to see everything is OK:

c:\python
>>> import numpy as np
>>> import scipy as sp
>>> import matplotlib.pyplot as plt

 

 

Posted in programming, Python, Software | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Argument handling in MATLAB functions – using default values

When writing functions in MATLAB sometimes the function requires a number of arguments some of which can have default values. So when the arguments are given, they overwrite the default arguments. Consider the following example:

function c = myfcn(a,b)
if nargin < 2
b = 2;
end
c = a+b;
end

First of all, we can determine the number of arguments given when the function was executed. based on the number of arguments, we can then decide whether use the default value for b or use the given value. In this case: myfcn(2,3) results in 5 but myfcn(2) results in 4.

This is good, but the problem begins when the number of arguments increases. Consider the function has 8 arguments. In order to decide whether using the default or the given values, you should check which arguments are given and which are not. In other words, you might need to write an if for different conditions, for instance:

function k = myfcn(a,b,c,d,e,f,g,h)
if nargin < 8; h=0.2; end
if nargin < 7; g=0.3; end
if nargin < 6; f=1; end
if nargin < 5; e=0; end
...
end

You can see this is ugly and not professional. It also prevents you from providing the function with a specific set of parameters, say you want new values for d and f and want to use the default values for the rest of the arguments.

The best way, I know and have used for a long time is using the arguments as a cell list. Check the following example:

function y = myfcn(n,varargin)
defaultValues = {2 20 150 0.85};    % default values {x1 x2 x3 x4};
idx = ~cellfun(@isempty,varargin);  % find which parameters have changed
defaultValues(idx) = varargin(idx); % replace the changed ones
[x1,x2,x3,x4] = defaultValues{:};   % convert from cell to var
y = (x1+x2*x3+x4)^(1/n);
end

Test:

 

>> myfcn(1)

ans =

3.0028e+03

>> myfcn(1,2)

ans =

3.0028e+03

>> myfcn(1,[],2)

ans =

302.8500

>> myfcn(1,[],[],2)

ans =

42.8500

>> myfcn(1,[],[],2)

ans =

42.8500

>> myfcn(1,[],[],2,2)

ans =

44
Posted in Linux, MATLAB, programming, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Iterative Learning Control – Part 2

In the previous post I explained the ILC controllers and compared them to the conventional feedback controllers. In this post I show a simple example that employs an ILC.

Check the following simple, linear plant:

y(t+1)= -0.7*y(t) - 0.12*y(t-1) + u(t)
y(0)=2
y(1)=2

In this system, u(t) is the input at time t, and y(t) is the output of the system at time t. The initial condition of the system also is represented with two simple equations.

We want to force the system to follow a square wave such as:

y_d(t)=\begin{cases} 2 & 0<t<10 \\ 4 & 11<t<20 \\ 2 & 21<t<30 \end{cases}

The ILC algorithm works as follows:

  1. consider y_d and the initial input (first iteration, k=0)
  2. run the system with this input and keep the result (y_0)
  3. compute the error as $latex e_0(t)=y_d(t)-y_0(t)
  4. compute the next input using the previous result as: u_1(t)=u_0(t)+K_p*e_0(t+1)
  5. use u_1(t) and jump to 2.

By implementing this simple algorithm we can get the following result. For this result I have chosen K_p=0.5.

ilc

Posted in control, Machine Learning, MATLAB, Optimization, programming, Robotics, Software | Tagged , , , , , , , , , , , , , , , , , , | Leave a comment

Iterative Learning Control – Part 1

Iterative Learning Control (ILC) is used for improving the transient response in systems that perform repetitively. ILC tries to improve the response by adjusting the input to the plant based on the error observed in the previous iteration.

A conventional controller C can be written as follows:

u_{k+1}(t) = C[e_{k+1}(t-1)]

where k shows the number of iterations, t denotes time, u is the input to the controller, and e is the error between the desired and the actual goal. It can be seen that the error from time t-1 has been used to make an output from the controller, and the output is being used at time t. The controller can be simply a constant gain such as:

u_{k+1}(t) = K_p*e_{k+1}(t-1)

The ILC controller, on the other hand, uses the error in time t+1 to make inputs at time t. This can be possible only through iteration. So that the error at time t+1 was observed from the previous iteration, k and will affect the next iteration, k+1.

u_{k+1}(t) = u_{k}(t) + C[e_{k+1}(t+1)]

again with a simple controller we can have:

u_{k+1}(t) = u_{k}(t) + K_p * e_{k+1}(t+1)

See an example in the next post.

 

Posted in control, Machine Learning, MATLAB, Optimization, programming, Robotics, Software | Tagged , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Probability – Basic Lesson 4

In the previous lesson, we saw the definition of Expectation. Here the definition of Variance (and Standard Deviation) is given.

Variance means that how much variability is in f(x) around its mean value (expected value) E[f(x)], and it can be written as follows:

var[f] = E[(f(x) - E[f(x)])^2]

and it can be simplified as,

var[f] = E[f(x)^2] - E[f(x)]^2


Example:

Let’s consider the previous example. You are playing a dice game, you roll the dice and you earn $2 for a 1, $1 for a 6, and you lose $1 otherwise. We learned that the Expectation was $-1/6 . Now we can calculate the variance as follows:

var[x] = E[x^2] - E[x]^2
= \sum_x x^2 Pr(x) - E[x]^2
= (2^2).(1/6) + (1^2).(1/6) + (-1^2).(1/6) - (-1/6)
= 53/36

You should also know that the Standard Deviation is defined as

std = \sqrt{var}

So in this example,

std = \sqrt{53}/6

 

 

Posted in Machine Learning, Statistics, Thoughts | Tagged , , , , , , , , , | Leave a comment