Archive

Archive for the ‘Computer/Programming’ Category

fmincon with normalized variables

August 20, 2012 Leave a comment

This post extends the work shown in my previous post:
fmincon with linear constraints

In this example I have normalized the variable vector w.r.t. the upper bound. Apart from that it’s pretty much the same as my previous post.

mscr.m (the objective function)
function product = mscr(X,UB)
X=X.*UB;
product = -X(1)*X(2)^2*X(3)^3*X(4)^4;
disp(X);
disp(product);
return

main.m (script calling the objective function)
close all; clear all; clc
lb=[0;0;0;0];
ub=[10;20;30;40];
x0=[2.5;2.5;2.5;2.5];
x0=x0./ub; %normalization (check the objective function)

A=[ub(1) ub(2) -ub(3) 0; 0 ub(2) ub(3) -ub(4)];
b=[0;0];
% A=[];
% b=[];
Aeq=[5*ub(1) 0 0 -ub(4)];
beq=[0];
options=optimset('Algorithm','active-set');

[x,fval]=fmincon(@(X)mscr(X,ub),x0,A,b,Aeq,beq,lb./ub,ub./ub,{},options);


There are two inequality constraints: the sum of first two variables should be less than or equal to the third, and the sum of the second and the third variables should be less than or equal to the fourth. The only equality constraint requires the first variable to be a fifth of the fourth variable. This is a working example.

P.S.: When the ‘Algorithm’ was set to ‘interior-point’ the minimization yielded an incorrect (read sub-optimal) answer. I am yet to investigate this anomaly.

fmincon example with linear constraints

August 12, 2012 1 comment

The following is a working example for ‘fmincon’. It tries to minimize the product (maximize the product if you account for the negative sign). There’re two linear constraints: the sum of the arguments should be equal to 10, and the sum of the first two arguments should be equal to the sum of the last two arguments.

mscr.m (the objective function)
function product = mscr(X)
product = -X(1)*X(2)^2*X(3)^3*X(4)^4;
disp(X);
disp(product);
return

main.m (script calling the objective function)
close all; clear all; clc
x0=[2.5;2.5;2.5;2.5];
Aeq=[1 1 1 1; 1 1 -1 -1];
beq=[10;0];
lb=[0;0;0;0];
ub=[10;10;10;10];
options=optimset('Algorithm','active-set');
[x,fval]=fmincon(@mscr,x0,[],[],Aeq,beq,lb,ub,{},options);

In conclusion here are a few tips:
– The function name, when passed to fmincon, can’t just be ‘mscr’ but it has to be ‘@mscr’ (I wonder why MATLAB doesn’t bother to specify that in bold in their help).
– If you want to specify ‘options’ without using non-linear constraint, use ‘{}’ as the empty space-holder. I had to find this out the hard way (i.e. by trial and error).
Remember that ‘fmincon’ is a minimizer. So if you want to maximize something, don’t forget the negative sign.

Graph paper in TikZ

June 2, 2012 1 comment

Building up on the answer by Jake on a question on TeX.SX, I have written the following program illustrating the use of Tikz in labeling diagrams.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\node[anchor=south west,inner sep=0] (image) at (0,0)
{\includegraphics[width=10cm]{fabrication_steps.pdf}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\draw [help lines,lime,xstep=.01,ystep=.01] (0,0) grid (1,1);
\draw [help lines,orange,xstep=.05,ystep=.05] (0,0) grid (1,1);
\draw [help lines,red,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {\scalebox{.2}{0.\x}}; }
\foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {\scalebox{.2}{0.\y}}; }
\node at (0.2,0.26) {\huge Label 1};
\node at (0.7,0.66) {\huge Label 2};
\end{scope}
\end{tikzpicture}

\end{document}

The result is at the link: https://docs.google.com/open?id=0B2-zRvD2mGgcalZKaXVhOXQ3S0U

Categories: Computer/Programming, Tex

Updating the PATH variable

May 26, 2012 3 comments

The problem turned out to be more difficult than I had originally thought. All I wanted to do was to update the PATH variable. To do so I executed the following command in the Ubuntu terminal (I wanted to use Tex Live with Ubuntu):
PATH=/usr/local/texlive/2011/bin/i386-linux:$PATH
But after rebooting Ubuntu, the above change was forgotten by the system. I continued my search for a solution to make the above change permanent. After reading numerous questions and answers on askubuntu.com, I reached the page https://help.ubuntu.com/community/EnvironmentVariables which explained in detail (but no so clearly) as to how to change the environment variables (and by extension the PATH variable). The following are a few popular solutions:
1. Edit the file /etc/environment. Edits to this file result in system wide changes, i.e. the changes affect all the users.
2. Edit the file ~/.profile. Edits to this file yield changes relevant only to the local user. Currently I am under the impression that ~/.profile is summoned when the user logs into the system.
3. Edit the numerous files which have names like bashrc, bash.bashrc, bash_profile and bash_login, to name a few. I haven't been able to justify to myself the existence (and in some cases they simply don't exist - regardless of what some community members may tell you) of these files.

The point is: there's no one single way in which you can edit the environment variables. That pretty much goes against the pythonic way of doing things and can be a source of significant mental trauma. Luckily, the edit to the ~/.profile file seems to be working fine and following is the way in which I have accomplished it:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
export LANGUAGE="en_US:en"
export LC_MESSAGES="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
export LC_COLLATE="en_US.UTF-8"

# 2012 May 26 0351hrs. I am editing this file to add the following
# location to the PATH variable.
PATH="/usr/local/texlive/2011/bin/i386-linux:$PATH"

A few comments on the above file edits:
1. : simply serves to append 2 strings. So "new_path:$PATH" appends new_path to the original PATH variable.
2. In case you are wondering about the $ symbol: the concept is similar to that of pointers. $ simply reads as "value of". Therefore, $PATH simply means current value of the PATH variable. It can be further understood by trying the following commands:
echo PATH will make the terminal display "PATH",
echo $PATH will print the current value of the PATH variable on the terminal.

A final point to note: bash is the default shell environment of Ubuntu. And what is a shell environment? I haven't really understood. More on this someday later.

I hate Windows

May 24, 2012 Leave a comment

Warning: This is a rant. You have been warned. Not all statements below this line in this post are true.

In a discussion with my friend in which we were discussing the operating systems we were currently using, I happened to praise the Windows 7 operating system. I think I did it too soon. Not even a week had passed that the Windows was able to provide me with a problem that had me completely stumped. The following are the gory details.

I wanted to make bubble charts for a future presentation. Therefore I downloaded a software named amcharts. Uncompressing the downloaded file created a folder. Soon after that I wanted to remove the uncompressed folder (so that I could move it to a more suitable location) from my desktop. But Windows 7 simply wouldn’t allow me to do that.

I tried many tricks. For example, I tried the following command.

net user administrator /active:yes

I referred a thread on superuser.com but to no avail. Later I invoked the administrator account of Windows 7. Even when logged in as the administrator, Windows was able to come up with brilliant messages like “Administrator privileges are required for this action”.

After trying numerous solutions on these lines (which involved opening the command prompt no less than 10 times, opening the control panel no less than 5 times and other unmentionable atrocities) I was still not able to get rid of the satanic folder from my hard drive. After revisiting the previously mentioned superuser.com thread, I happened to read another solution and that is what did the trick.

I immediately pulled out my Ubuntu 10.04 disc and performed a live boot. Mounting the partition where my Windows 7 is installed, I silently crawled to the God forsaken location. And then, with all my might (imaginary might that is – I am too stingy to actually do what I am about to write) I hit the Shift + Delete button. Warily, I shut down the Ubuntu OS and rebooted my Windows 7 OS. And when I reached the desktop, seeing the satanic folder exist no more made me happier than I was when India lifted the cricket 2011 world cup (well that’s a bit of an exaggeration – even by the standards of this rant).

P.S.: While suffering through the above ordeal I also downloaded Unlocker – a software that proved to be of great use when I was using my Windows XP operating system. Unfortunately, on Windows 7, I have not been able to use it successfully. It doesn’t open up any GUI nor does it integrate itself with Windows shell (i.e. right clicking on a folder never yielded the Unlocker option).

What’s the equivalent of “add or remove programs” in Ubuntu?

May 17, 2012 1 comment

Having asked a question too long on the askubuntu.com, I was asked to split the question into different parts. Understandably, splitting the question will help in getting the answers. But I still want to preserve the context in which I am asking the questions and therefore, I have recorded my original question below:

I have been using Ubuntu for quite some time now. But I haven’t yet got the hang of it. One of the reasons is that due to the excellent support that the Ubuntu community offers, I find specific answers for the specific questions that I have and my Ubuntu system still runs pretty alright.

There are sections of my file system that I completely avoid (to avoid messing up the system files of my Ubuntu) and then there’re are user folders like “Documents”, “Downloads”, etc. which I use the way I would have used them if I were using my Windows OS.

Coming to my specific question: I wanted to install the plugin TeXlipse ([installation instructions][1]) and for that I am supposed to remove any previous version of it. Maybe the eclipse GUI is sufficient to achieve this end, but it compels me to ask a few more general questions:

1. What’s the equivalent of “add or remove programs” in Ubuntu?
2. Is there a good tutorial on user management (with good schematics for better understanding)? Currently since I am a single user, I first try a command. If it doesn’t work I try it with “sudo” and then it usually works. But if you see the problem, I haven’t really understood when I should use “sudo”. Since I am the only admin and the only user, my approach almost always works, but I want a deeper insight.
3. I have understood how to change the “PATH” variable on Ubuntu (using terminal), but is there a GUI (like the “system properties” in Windows) where I can do that?
4. In Windows there’re perhaps only a couple of important folders (by important I mean important in my logical picture of the Windows file system) in the installation drive (in my case C:\). Namely “Program Files” and “Windows”. I simply stay away from “Windows” folder and the “add remove program files” is good enough to handle the “program files” folder of Windows. Of course there’s a folder named “Users” where the users (who are not admins) can access only their folders.

Thus there’s a clear picture at some level in my mind of the Windows file system. In Ubuntu, when I reach the location “/”, there’s a huge list of folders, most of which I have no clue as to what they contain. The “/bin” folder seems to be the equivalent of the “Windows” folder in windows. The “/usr” folder seems like it’s the equivalent of the “Users” folder in Windows. But even the “/home” folder looks like it can fit the bill.

Please understand that I do understand, that Ubuntu (Linux) has a different character than that of Windows, i.e., there need not be exact equivalent of Windows functions, in Ubuntu. All I am looking for is a bit more clearer picture of the Ubuntu file system.

[1]: http://texlipse.sourceforge.net/manual/installation.html

Python on Windows

May 14, 2012 Leave a comment

I pretty much repeated the steps I took for installing Python + PyDev + Eclipse on my Ubuntu 11.10 operating system. This time the operating system was Windows 7 SP1 (and unlike the Ubuntu OS, this one is not running on VirtualBox).

To be able to use Python through command line, I referred to a link from stackoverflow.com. Somehow, even after restarting the machine, I was stilling getting the “not an internal command or filename” error for python, in the command prompt.

After viewing a youtube video, which basically tells the viewer to append “C:\Python27” to the system environment PATH variable, I was able to make the command prompt recognize the python command. Of course, I restarted the system, but I am not sure if that’s necessary. I also haven’t verified whether the action mentioned in the previous paragraph helped at all.

Then I was able to use the trick given at a link, which enables the user to embed python code in a windows batch (.bat) file. The convenience this trick affords is that you can double click the .bat file and it functions like a .exe file. An example is as follows:
Sample python code embedded in a Windows 7 .bat file.

Ubuntu 11.10 – My notes

May 2, 2012 2 comments

The format of this post has been changed on 2012/05/26. The older version of the post is below the line “The Line of Format Change (2012/05/26)”. The newer format simply lists the useful programs in one section and useful commands in the other.

Useful Commands
chown
chmod
gnome-session –version
Ctrl+H (shows the hidden files in a folder in nautilus)

Useful Programs/Softwares ([optional]installation date)
– Kile (2012/05/26)
– Tex Live (2012/05/15)
– VLC Media Player (2012/05/09)
– Geany
– Unity (read more about it below “the line of format change”)
Ubuntu + Eclipse + PyDev (Python IDE)
– Skype
– Google Chrome
– Inkscape Editor
– Okular
VirtualBox, Ubuntu, Guest Additions, Seamless mode, Shared folders

————————- The Line of Format Change (2012/05/26) ————————-

It seems like I will have to reinstall Ubuntu once in a while. This post is for recording the useful changes I have made to the installation. I intend to record the changes in the reverse chronological order, but since I don’t remember all the changes made (at the time I initiated this post), the order may be wrong (esp. for the oldest recorded changes).

Add future changes just below this

2012 May 15

– The TexLive installation I have mentioned below failed (or perhaps I was not able to use tlmgr&lt with it). Therefore I removed the installation (using the method given in the link mentioned in Step2) and I reinstalled TexLive using the following steps:

Step0: Understood why I need “vanilla” TexLive

Step1: Acquired the net-install software
Step2: Followed the quick install procedure
– Installed Synaptic package manager (sudo apt-get install synaptic). I wonder why Ubuntu decided not to include this software by default.
– Installed TexLive. Used the following guide.
http://mehulphy.wordpress.com/2011/11/05/installing-tex-live-2011-on-ubuntu-11-10/
– Steps involved:
Step1: http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
Step2: Extract the folder.
Step3: Open terminal and change the directory to the extracted folder. After that, run the following command:
sudo perl install-tl
(there should be a file named install-tl or similar in the extracted folder).
Step4: Update the path variable by executing the command PATH=/usr/local/texlive/2011/bin/i386-linux:$PATH in the terminal. This change is temporary. To make it persistent edit the ~/.profile file. Refer my post for more info.

2012 May 09
– VLC Media player

Changes made on or before 2012 May 01
– The primary motivation for writing this post is my disdain towards the Ubuntu 11.10 interface “Unity”. The following changes are copied from the a device guru web-page.

- Advanced Settings (aka gnome-tweak-tool)
- Configuration Editor (aka gconf-editor)
- dconf Editor ("dconf-editor" doesn't work, just go with "dconf Editor")
- “gnome-colors”

Ubuntu + Eclipse + PyDev (Python IDE)
– Skype
– Google Chrome
– Inkscape Editor
– Okular
VirtualBox, Ubuntu, Guest Additions, Seamless mode, Shared folders

Categories: Computer/Programming, Linux, Tex Tags:

Ubuntu + Eclipse + PyDev

May 1, 2012 3 comments

OS: Ubuntu 11.10 (Running on VirtualBox Seamless Mode)
IDE: Eclipse 3.7.0
Plug-in: PyDev (2.5.0 - unsure about the version as I couldn't find a way to make the terminal display the current version number)
Language: Python 2.7

After struggling with gedit to write my Python code, I soon realized that writing (actions like commenting, uncommenting, auto-complete, etc.) and debugging was going to be a royal PITA (Pizza In The Armory). Therefore, I tried to install PyDev. I will update any kinks/issues I will have because of it on this post. So far it seems good.

I use the following link as my main installation guide:
Starting with Python On Eclipse in Ubuntu.
The steps given in the guide are pretty straightforward. Folder permissions on my Ubuntu is what concerned me the most, but somehow everything seems to have worked out pretty well.

Matlab Plotting Tips

April 28, 2012 Leave a comment

(note to self: add new tips on the top)

2012 April 28
Tip for plotyy
Source: An answer on mathworks.com forum
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
set(AX,{'ycolor'},{'r';'b'}) % Left color red, right color blue...

In the above example observe the way the handles have been used for the axes (AX) and for the plots (H1, H2).

Categories: Computer/Programming, Matlab Tags: ,