Come foglie…

…e le foglie, che hanno aspettato lunghissimi mesi per poter tingersi a festa (e buttarsi poi di sotto).

…uno spirito libero

Caccioppoli era innanzitutto uno spirito libero, un eccezionale pianista, un filosofo e un poeta.

Luciano De Crescenzo, da Storia della filosofia greca.

NACA, Python e PyQt5…

I profili alari NACA sono particolari forme di profilo alare studiati dalla National Advisory Committee for Aeronautics (NACA) statunitense. La forma di un profilo alare NACA è descritta mediante la parola “NACA” seguita da un numero. I valori presenti in tale codice numerico possono essere inseriti nelle equazioni per calcolare le proprietà della sezione alare.

Le sezioni alari NACA a quattro cifre definiscono il profilo in questo modo:

  1. la prima cifra indica la curvatura massima come percentuale della corda;
  2. la seconda cifra fornisce la distanza del punto di massima curvatura dal bordo d’attacco espressa come percentuale della corda e in multipli di 10;
  3. le ultime due cifre descrivono il massimo spessore del profilo alare espresso come percentuale della corda.

La distribuzione degli spessori, lungo la linea media è:

dove:

  • c è la lunghezza della corda;
  • x è la posizione lungo la corda da 0 a c;
  • yt è metà dello spessore ad un dato valore di x;
  • t è lo spessore massimo espresso come frazione della corda, in modo che 100 t sia uguale alle ultime due cifre del codice NACA.

Si noti che in questa equazione, a (x/c) = 1 (il bordo d’uscita del profilo), lo spessore non è esattamente uguale a zero. Se per motivi computazionali è necessario uno spessore nullo al bordo d’uscita, si deve modificare uno dei coefficienti in modo che la loro somma dia zero. Modificando l’ultimo coefficiente a −0,1036, ad esempio, produrrà un piccolo cambiamento nella forma generale del profilo. Il bordo d’attacco è approssimabile ad un cilindro con raggio uguale a:

{\displaystyle r=1{,}1019\,t^{2}.\,}

L’equazione della curvatura del profilo è:

dove:

  • m è la massima curvatura espressa come percentuale della corda ed è la prima delle quattro cifre del codice;
  • p è la posizione della massima curvatura, espressa in multipli di 10 e come percentuale della corda, ed è la seconda cifra del codice.

Qui si propone un codice per generare i profili NACA della serie a quattro cifre sfruttando le potenzialità di Python e delle librerie PyQt5 e Matplotlib.

I sorgenti completi del progetto possono essere scaricati dal mio GitHub.

an essential tool for mathematics

I believe the blackboard is an essential tool for mathematics. Usually, to solve (or find) a problem, you have to make many incorrect guesses or other mistakes first. Writing on a blackboard is easily edited, which is useful in the process of shaping one’s own thoughts in the process of solving a problem. On paper, even pencil is more difficult to erase, so there is some underlying pressure not to waste space with ideas perceived to be bad — but these “bad” ideas might actually turn out to be very important, or at least inspire progress. Because writing on a blackboard is temporary, it is very easy to put an idea up and see what happens next.

from Do Not Erase: Mathematicians and Their Chalkboards by Wynne, Jessica.

How to Interpolate Data with Scipy

Interpolation may sound like a fancy mathematical exercise, but in many ways, it is much like what machine learning does.

  • Start with a limited set of data points relating multiple variables
  • Interpolate (basically, create a model)
  • Construct a new function that can be used to predict any future or new point from the interpolation

So, the idea is — ingest, interpolate, predict.

Concretely, suppose we have a limited number of data points for a pair of variables (x,y) that have an unknown (and nonlinear) relationship between them i.e. y = f(x). From this limited data, we want to construct a prediction function that can generate y values for any given x values (within the same range that was used for the interpolation).

There are a lot of mathematical theories and work on this subject. You can certainly write your own algorithm to implement those interpolation methods. But why not take advantage of the open-source (and optimized) options?

Scipy interpolate

We start with a quadratic function where we have only 11 data points. The code to interpolate is basically a one-liner:

f1 = interp1d(x, y, kind='linear')

Note that this interp1d class of Scipy has a __call__ method that returns back a function. This is the function f1 we get back as our prediction model.

Here the code we have used:

from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate

NUM_DATA = 11
NUM_INTERPOLATE = 41

x = np.linspace(0, 10, num=NUM_DATA, endpoint=True)
y = x**2+7*x-28

f1 = interp1d(x, y, kind='linear')
xnew = np.linspace(0, 10, num=NUM_INTERPOLATE, endpoint=True)


fig, ax  = plt.subplots(1,2,figsize=(6,3),dpi=120)
ax[0].scatter(x, y)
ax[0].set_title("Original data")
ax[1].scatter(x, y)
ax[1].plot(xnew, f1(xnew), color='orange',linestyle='--')
ax[1].set_title("Interpolation")
plt.show()

Let us go one degree higher to a cubic generating function. The interpolation result looks as smooth as ever.

x = np.linspace(0, 10, num=NUM_DATA, endpoint=True)
y = 0.15*x**3+0.23*x**2-7*x+18

f1 = interp1d(x, y, kind='linear')
xnew = np.linspace(0, 10, num=NUM_INTERPOLATE, endpoint=True)

fig, ax  = plt.subplots(1,2,figsize=(6,3),dpi=120)
ax[0].scatter(x, y)
ax[0].set_title("Original data")
ax[1].scatter(x, y)
ax[1].plot(xnew, f1(xnew), color='red',linestyle='--')
ax[1].set_title("Interpolation")
plt.show()

Note that the original data may come from a cubic function, but this is still a ‘linear’ interpolation which is set by the kind parameter as shown above. This means that the intermediate points between the original data points lie on a linear segment.

But Scipy offers quadratic or cubic splines too. Let’s see them in action.

Things can be a little tricky to handle with linear interpolation when the original data is not polynomial in nature or the data has inherent noise (natural for any scientific measurement).

Here is a demo example for a particularly tricky nonlinear example:

x = np.linspace(0, 10, num=NUM_DATA, endpoint=True)
y = 0.15*x**3+0.23*x**2-7*x+18

f1 = interp1d(x, y, kind='linear')
f3 = interp1d(x,y, kind='cubic')
xnew = np.linspace(0, 10, num=NUM_INTERPOLATE, endpoint=True)

fig, ax  = plt.subplots(1,3,figsize=(6,3),dpi=120)
ax[0].scatter(x, y)
ax[0].set_title("Original data")
ax[1].scatter(x, y)
ax[1].plot(xnew, f1(xnew), color='red',linestyle='--')
ax[1].set_title("Linear")
ax[2].scatter(x, y)
ax[2].plot(xnew, f3(xnew), color='orange',linestyle='--')
ax[2].set_title("Cubic and Linear splines")


plt.show()

Which Social is more popular in the last 3 months in Italy?

import pandas as pd

from pytrends.request import TrendReq

# build payload
pytrend = TrendReq(hl='en-US', tz=360)

# list of keywords to get data
#Which Social is more popular in the last 3 months in Italy?
keywords = ['Twitter', 'Facebook', 'Instagram']
pytrend.build_payload(
     kw_list = keywords,
     cat=0,
     timeframe = 'today 3-m',
     geo='IT',
     gprop='')

data = pytrend.interest_over_time()

data= data.drop(labels=['isPartial'],axis='columns')

image = data.plot(title = 'Social media more popular in last 3 months on Google Trends ')
fig = image.get_figure()
fig.savefig('figure.png')
data.to_csv('Py_VS_R.csv', encoding='utf_8_sig')

Teach it…

If you want to master something, teach it. The more you teach, the better you learn. Teaching is a powerful tool to learning.

– Richard Feynman

work in progress

Una semplice calcolatrice del BMI (Indice di Massa Corporea) in PyQt… Qui il codice sorgente.