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:

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

Here is the result:

2017-04-07_20-01-46

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.


\begin{figure}[H]
\begin{tikzpicture}[scale=0.5]
\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);
\fill[black,font=\footnotesize]
(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);
\fill[black,font=\small]
(13,5.5) node [above] {$P_3$}
(15,3.72) node [above] {$P_4$};
\draw[blue,->,thick] (13,5.5) -- (15,3.72);
\fill[blue,font=\small]
(14,4.5) node [below] {$\vec{l}_{\,3,4}$};
\draw[red,->,thick] (13.784,4.802) -- (15.784,5);
\fill[red,font=\small]
(15.9,5) node [above] {$\vec{f}_{\,3,4}$};
% equation of the stress tensor
\fill[black,font=\small]
(14,2) node [below] {$\sigma_{ij}=\frac{1}{V}\sum \limits_{c} f_{ij}^c\times l_{ij}^c$};
\end{tikzpicture}
\caption{Calculation of meso-stress in a cycle of grains}
\end{figure}

Make python wait for you by sleep function

import time
time.sleep(1)

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

./hello

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:
set(gca,'FontSize',14)
figureHandle = gcf;
set(findall(figureHandle,'type','text'),'fontSize',14)

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 );
child=get(harea,'Children')
set( harea, 'FaceColor', 'm')
alpha('texture')

Increase text size for plotyy function

[ax h1 h2]=plotyy(e22,ev,e22,q)
set(ax,'FontSize',14)

Subplot

%subplot of (w2 normalized), (compare), (new,lostcontact), (total contact)
subplot(2,2,1);
polar(degtorad(alpha),w2normal,'r--*');
title('Normalized $W_2$ at $\eta=0.92$','interpreter','latex')

subplot(2,2,2);
plot(alpha,ntotal,'b--*');
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')

subplot(2,2,3);
plot(alpha,nlost,'r--o',alpha,nnew,'b--*');
xlim([0 350]);
xlabel('Angle of stress probe - $\alpha_\sigma$','interpreter','latex');
ylabel('New ($N_{new}$) and lost ($N_{lost}$) contacts','interpreter','latex');
legend('N_{lost}','N_{New}');
grid on;
title('Lost contacts and new contacts during the directional research')

subplot(2,2,4);
plot(alpha,w2macro,'b--o',alpha,w2micro,'r--*');
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
yi=interp1(x,y,a)
% a is a real number of the
% x-axis value of the data point
hold on
plot(a,yi,'*')

For example:

x=[0:0.01:1];
y=[0:10:1000];
plot(x,y);
% Now add a data point with x=0.25
yi=interp1(x,y,0.25);
hold on;
plot(0.25,yi,'*')

Tag Cloud