Useful Gibberish

Archive for the ‘Programming’ Category

Matlab: How to plot boob?

Yes, serious.

Reading emails about professors dying because of  radio-active rays or students suiciding because of stresses makes me want to stop for while and forget what I’m doing.

But I can’t leave my computer, so I have to do something with it.

Google is useful for this purpose, now all I need is a silly question. I use Matlab to plot a lot of my data, but never I’d plotted boob, which is no doubt a lot more attractive than those stress-strain curve of the simulation. Fortunately, there’s a Wolfram’s widget provides the function, so I put it in Matlab and tried, it’s pretty funny.

Here is the code:

fplot(@(x) -(3*x*log(x)-0.1*exp(-(30*x-12)^6)),[0 1])

Here is the result:


Draw particles contact with tikz

I wrote this code long time ago and I want to share it since it’s pretty cool and come in handy a lot of times.

\draw[step=.5cm,lightgray,very thin] (0.1,0.6) grid (7.8,6.8);
\draw[fill=yellow,fill opacity=0.5] (1,3.5) -- (2,5) -- (4,5.5) -- (6,3.6) -- (4,2) -- (2,2) -- (1,3.5);
\draw[fill=cyan,fill opacity=0.5] (1,3.5) circle (0.78cm);
\draw[fill=cyan,fill opacity=0.5] (2,5) circle (1cm);
\draw[fill=cyan,fill opacity=0.5] (4,5.5) circle (1.05cm);
\draw[fill=cyan,fill opacity=0.5] (6,3.72) circle (1.64cm);
\draw[fill=cyan,fill opacity=0.5] (4,2) circle (1cm);
\draw[fill=cyan,fill opacity=0.5] (2,2) circle (1cm);
(1,3.5) node [above] {$P_1$}
(2,5) node [above] {$P_2$}
(4,5.5) node [above] {$P_3$}
(6,3.6) node [above] {$P_4$}
(4,2) node [above] {$P_5$}
(2,2) node [above] {$P_6$};
\draw[thick,->] (9,3.5) -- (10,3.5);
% draw 2 particles use for pair of contacts
\draw[fill=cyan,fill opacity=0.5] (13,5.5) circle (1.05cm);
\draw[fill=cyan,fill opacity=0.5] (15,3.72) circle (1.64cm);
(13,5.5) node [above] {$P_3$}
(15,3.72) node [above] {$P_4$};
\draw[blue,->,thick] (13,5.5) -- (15,3.72);
(14,4.5) node [below] {$\vec{l}_{\,3,4}$};
\draw[red,->,thick] (13.784,4.802) -- (15.784,5);
(15.9,5) node [above] {$\vec{f}_{\,3,4}$};
% equation of the stress tensor
(14,2) node [below] {$\sigma_{ij}=\frac{1}{V}\sum \limits_{c} f_{ij}^c\times l_{ij}^c$};
\caption{Calculation of meso-stress in a cycle of grains}

Make python wait for you by sleep function

import time

will make python wait for you 1 second before processing next line. To make python wait for you with another value of time, change 1 to whatever you need, remember the unit here is second.
Very useful when you have to check your code, see the temporary result and so on.

Happy coding!

Hello world by fortran

This is just a very basic script to run a Hello world! program by the language Fortran in Ubuntu.

program hello
print *, "Hello world!"
end program hello

Save the text file as fortran extention, here I save it as hello.f90

To compile in, open terminal and navigate to that folder, run this:

gfortran -o hello hello.f90

Explain: This code define the compiler gcc-fortran, output file has name “hello” and the script file is “hello.f90”.
When the compilation is finished, in that folder we will see the hello program, in terminal, to run it, just input


And you will see the line “Hello world!” in the terminal window.
Have fun programming!

Feel more powerful in Ubuntu: kill all

This is some useful trick to kill task in Ubuntu (draft)

  • ps and kill

ps -A
sudo kill -9 pid

With the pid is the number we get from the first line.

  • Feel like windows’ task manager:
    sudo gnome-system-monitor
    This will open the system monitor tool, there we can view CPU+ Ram information, and a list of running program, kill what you need there by right click and choose End Process or Kill process

Those two methods can come to handy when we’re stuck, for example, in Ubuntu Software Center, that a package is not dpkg completely and the dpkg folder is locked and we can’t install another software due to this (installing apps in Ubuntu is one by one)

Useful MatLab code for plotting

Increase all text size:
figureHandle = gcf;

Labelling the plot:
xlabel('$\alpha_\varepsilon$ - direction of strain probe $(\circ)$','interpreter','latex');
ylabel('Second order work $W_2$ and $W_2^{\mu}$ (J)','interpreter','latex');

Hatch region:
harea = area([140 140 150 150 140],[-0.05 0.3 0.3 -0.05 -0.05],3 );
set( harea, 'FaceColor', 'm')

Increase text size for plotyy function

[ax h1 h2]=plotyy(e22,ev,e22,q)


%subplot of (w2 normalized), (compare), (new,lostcontact), (total contact)
title('Normalized $W_2$ at $\eta=0.92$','interpreter','latex')

xlim([0 350]);
xlabel('Angle of stress probe - $\alpha_\sigma$','interpreter','latex');
ylabel('Total contacts - $N_c$','interpreter','latex');
grid on;
title('Total contacts numbers $N_c$ at $\alpha_\sigma$','interpreter','latex')

xlim([0 350]);
xlabel('Angle of stress probe - $\alpha_\sigma$','interpreter','latex');
ylabel('New ($N_{new}$) and lost ($N_{lost}$) contacts','interpreter','latex');
grid on;
title('Lost contacts and new contacts during the directional research')

legend('W_2 macro','W_2 micro');
xlabel('Angle of stress probe - $\alpha_\sigma$','interpreter','latex');
ylabel('Second order work (J)','interpreter','latex');
xlim([0 350]);
grid on;
title('Comparison $W_{2}macro$ and $W_{2}micro$','interpreter','latex')

Adding point to Matlab plot

Here is the code to plot a data point to a 2D diagram.

plot(x,y) %plot the line
% a is a real number of the
% x-axis value of the data point
hold on

For example:

% Now add a data point with x=0.25
hold on;

Tag Cloud