# Python, NumPy, SciPy, MatPlotLib

These are options besides Sage, which will work on Windows and require only little more effort. You are going to miss the Maxima support among other things, and it is not a packaged program like Euler. But it certainly worth to take a look at. My feeling is that this is a better way to start doing research in math than the proprietary Matlab. But make up your own mind!

The glue of the system is Python, a good choice for interaction with other systems. That’s why Sage uses it too. It is also a nice language to learn. I am presently unsure, if it would make a good starter language, but I doubt it. Java is much more structured and opens the road to older languages like C, C++ and even other not so C-like object oriented languages. Coming from Python, this looks more difficult to me.

Anyway, install Python 2.7 and the other packages from the title of this blog. They will install as libraries into the Python directory and can be used immediately. Make sure to select the versions for Python 2.7 in Windows. I am not sure if all packages have versions for Python 3.3 already. You can try.

The next choice is to write Python programs with any text editor, or to run commands in a shell. I did both. So open any text editor (the je that comes with Euler will do, or install Notepad++) and paste the following into it.

import numpy as np
import matplotlib.pyplot as plt

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)

t = plt.xlabel('my data', fontsize=14, color='red')

plt.title(r'$\sigma_i=15$')

plt.show()

As you see, we import things from the NumPy package, and other things from the matplotlib package. Python lets you import packages into different name spaces, here „np“ and „plt“. The next command is like a multiple assignment in Euler. It might better be written in two lines. But I copied the commands from a tutorial. The rest is plotting with the wonderful matplotlib.

Then save the file under a name like „plottest.py“, and double click this file. Or open a command shell and enter the name of the file. A plot window will open, and you can see and save the graphics. Here is the result.

The package does one thing that Euler cannot do currently. It adds Latex to the plot. The fonts do not quite fit, but that is acceptable.

For another try with SciPy, I did an optimization from a tutorial file.

from pylab import *
from numpy import *
from scipy.optimize import fmin

## Parametric function: 'v' is the parameter vector, 'x' the independent variable
fp = lambda v, x: v[0]/(x**v[1])*sin(v[2]*x)

## Noisy function (used to generate data to fit)
v_real = [1.5, 0.1, 2.]
fn = lambda x: fp(v_real, x)

## Error function
e = lambda v, x, y: ((fp(v,x)-y)**2).sum()

## Generating noisy data to fit
n = 30
xmin = 0.1
xmax = 5
x = linspace(xmin,xmax,n)
y = fn(x) + rand(len(x))*0.2*(fn(x).max()-fn(x).min())

## Initial parameter value
v0 = [1, 1, 1]

## Fitting
v = fmin(e, v0, args=(x,y),maxiter=10000, maxfun=10000)

## Plot
def plot_fit():
print 'Estimater parameters: ', v
print 'Real parameters: ', v_real
X = linspace(xmin,xmax,n*5)
plot(x,y,'ro', X, fp(v,X))

plot_fit()
show()

This file shows a longer definition of a function. The body of the function is indented, and the number of blanks matters. So if you copy and paste, take care of this!

For a comparison, I did the same in Euler. I explain the function and the optimization on this page.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.