Thursday, November 05, 2009

Eigenvector Calculation with MATLAB vs Mathematica

We have realized that eigenvector calculation for a square matrix whose eigenvalues are all zero must be done by symbolic toolbox in MATLAB. Otherwise MATLAB gives the wrong answer. Or, you can use Mathematica instead. Since Mathematica is able to calculate eigenvectors numerically for this case.
Here is the example:
Consider such a 6x6 square matrix:
In MATLAB, try to calculate eigenvectors numerically:
%% Eigencvector calculation:: Directly numerical
A1=[0 0 0 0 1 0;0 0 1 1 0 -1;0 0 0 0 0 0;0 0 0 0 0 0;0 0 1 0 0 -1;0 0 0 0 0 0];
[v1,l1]=eig(A1)

Matlab gives eigenvectors as:
which is wrong!.

However by symbolic toolbox:

%% Eigencvector calculation: Using symbolic toolbox
A2=sym([0 0 0 0 1 0;0 0 1 1 0 -1;0 0 0 0 0 0;0 0 0 0 0 0;0 0 1 0 0 -1;0 0 0 0 0 0]);
[v2,l2]=eig(A2)
That's now ok!

%% Cross checking with "null space" concept
%Altough we define matrix numerically, null space of A1 however gives numerically true eigenvector familiy:
null(A1)
And finally here is the Mathematica screenshot:
Mathematica calculates successfully:

Wednesday, November 04, 2009

How to create mplayer playlist in Ubuntu

1 - Open a terminal within your directory that includes the media files ("mp3" for this case) to be added to the playlist.

2 - Use find command with -name option to list  files (lists recursively by default) and send this list into your "playlistName.m3u" ("Totem Player" in Ubuntu considers .m3u files)

Here is the script: (GNOME Terminal 2.28.1)

$find -name '*.mp3' >playlistName.m3u

 


Thursday, October 15, 2009

How to read Binary Data File in C (Sequentially)

Exactly two lines have to be changed from ASCII file mode given here.
Differences from ASCII mode:
1 - Additional "b" for fopen()
2 - Usage of fread() instate of fscanf
*/
...
fp = fopen(i_filename, "rb"); // read a binary file ("r" and "b")
...
/* loop until all data keep into array */

while( (!feof(fp))){
//fscanf(fp, "%f", &value); you can not use this any more for binary files. Now you have have to use fread()
fread(&value, sizeof(FP32), 1, fp);
...
}

Wednesday, August 19, 2009

Türkçe Karakterlerin Unicode karşılıklarını bulmak ve kullanmak

Start->Run->charmap ile Character Map açılır.
Buradaki harflerin unicode karşılıkları Character Map penceresinin en sonunda "U+0130" şeklinde gösterilir.
Bu unicode karakteri yazarken ortadaki "+" işareti kullanılmaz be başa bir "\" işareti koyulur. Örnek: I harfinin unicode'u U+0049 dur, \u0049 şeklinde kullanılır.

Tuesday, August 18, 2009

Kendinden enerjili eski mayın arama / patlatma aracı

"Self energized" robotlara baktım biraz ama cok birşey bulamadım. Birtane "spring loaded" kendi kendine zıplayan bir robot var. Neyse, mekanizması nasıl olur bilemiyorum ama birileri herhalde biliyordur.

Merak ettiğim konu şu:

Küre şeklinde olan, toprakta ve taşlık arazide hızlıca ilerleyebilecek minik keskin uçlu birrçok ayağı bulunan bir araç yapılabilse. Bu aracın boyu da normal bir insan eli kadar olsa. Ayrıca, üzerindeki sıkıştırılmış yay mekanizmalarından güç sağlasa (1km kadar ilerleseler yeter mi?) ve ucuzca üretilebilse. Bunlardan birçoğu (mesela 1000 tanesi) mayınlı olduğu düşünülen yada bilinen araziye salınsa. Acaba mayın temizlemede veya mayından korunmada bir yöntem olabilir mi?

Neyse bu arada araştırma yaparken birazcık da self energized robotlar ve uzun süredir merak ettiğim yay mekanizmalarına da bakarız. Bilen anlatsın. Mekanizmalar kitabında var mıdır ki?

Monday, August 17, 2009

How to add an image to a JPanel

In case of using Netbeans IDE Palettes for GUI building, in order to insert an image into JPanel, you have to change the layout of panel to "Border Layout". Then you have to use a JLabel to pass the image into JPanel.

I have tried many ways but this worked fine for me:

ImageIcon picturetoInsert = newImageIcon("C:/images/anyimage.gif");
JLabel label = new JLabel("",picturetoInsert,JLabel.CENTER);
pPanel.add(label,BorderLayout.CENTER);




Tuesday, July 28, 2009

How to read (ASCII) data file in C

/*Function that reads the data file into an array and returns a pointer that points to this array of FP32*/

FP32* readData(const char *i_filename){

FILE *fp;
fp = fopen(i_filename, "r"); //open file , read only
INT32U numElements = 0;
FP32 value; //keep value at pointer position
FP32 *array=NULL; //set to NULL because of realloc
/* loop until all data keep into array */

while( (!feof(fp))){
fscanf(fp, "%f", &value);
array = (FP32 *) realloc (array,(numElements+1) * sizeof(FP32 *));
array[numElements++] = value;
}
/* shift array and add "size of array" to its first element
since there is no way to get size of an array from its FP32 pointer*/

INT32U i;
array = (FP32 *) realloc (array,(numElements+1) * sizeof(FP32 *));
for(i=numElements;i>0;i--){
array[i]=array[i-1];
}
array[0]=numElements;
/* close file */
fclose(fp);
return array;
}