Useful Gibberish

Archive for the ‘Programming’ Category

Butterfly curve

Nay ngồi xem lại phương trình tham số (parametric equation), vọc một tí với plot trong python.

Phương trình tham số là phương trình mà nghiệm được biểu diễn bởi một số (tham số) bên ngoài, ví dụ nghiệm x,y sẽ được biểu diễn bởi x = f(t) và y =g(t).

Phương trình tham số của đường con bướm được biểu diễn như sau:

x = sin (t) \left( e^{cos(t)} - 2 cos (4t) -sin^5 (\frac{t}{12}) \right)

y = cos (t) \left( e^{cos(t)} - 2 cos (4t) -sin^5 (\frac{t}{12}) \right)

t \in (0, 12 \pi)

Đồ thị nó sẽ ra như thế này:

ButterflyFunction

Code python để plot như sau:


# the butterfly curve
from __future__ import division
import numpy as np, math, os, scipy
import matplotlib.pyplot as plt

#t_range = np.arange(0,12*math.pi,0.001)
t_range = np.linspace(0,12*math.pi,1000)
for t in t_range:
x = math.sin(t)*(math.exp(math.cos(t))-2*math.cos(4*t)-(math.sin(t/12))**5)
y = math.cos(t)*(math.exp(math.cos(t))-2*math.cos(4*t)-(math.sin(t/12))**5)
plt.scatter(x,y,marker='.',s=0.2,c='k')
plt.show()

Advertisements

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!

Tag Cloud

%d bloggers like this: