Пример #1
0
def logistic():
    problem = Logistic(0.2, 1, 0.1)
    T = 40
    dt = 0.1
    method = ForwardEuler(problem, dt)
    method.set_initial_condition(problem.u0, 0)
    u, t = method.solve(T)
    # note that import * is not legal inside functions so we
    # have to import each specific function:
    from scitools.std import plot, hardcopy, xlabel, ylabel, title
    plot(t, u)
    xlabel('t'); ylabel('u')
    title('Logistic growth: alpha=0.2, dt=%g, %d steps' \
          % (dt, len(u)-1))
    # compare with exponential growth:
    #from scitools.std import hold, linspace, exp
    #te = linspace(0, dt*N, N+1)
    #ue = 0.1*exp(0.2*te)
    #hold('on')
    #plot(te, ue)
    hardcopy('tmp.eps')
Пример #2
0
def logistic():
    problem = Logistic(alpha=0.2, R=1, U0=0.1)
    T = 40
    solver = ForwardEuler(problem)
    solver.set_initial_condition(problem.U0)
    t = np.linspace(0, T, 401)  # 400 intervals in [0,T]
    u, t = solver.solve(t)
    # Note that import * is not legal inside functions so we
    # have to import each specific function.
    from scitools.std import plot, hardcopy, xlabel, ylabel, title
    plot(t, u)
    xlabel('t'); ylabel('u')
    title('Logistic growth: alpha=%s, R=%g, dt=%g' \
          % (problem.alpha, problem.R, t[1]-t[0]))
    # Compare with exponential growth
    #from scitools.std import hold, linspace, exp
    #te = linspace(0, dt*N, N+1)
    #ue = 0.1*exp(0.2*te)
    #hold('on')
    #plot(te, ue)
    hardcopy('tmp.eps')
Пример #3
0
def logistic():
    problem = Logistic(alpha=0.2, R=1, U0=0.1)
    T = 40
    method = ForwardEuler(problem)
    method.set_initial_condition(problem.U0)
    t = np.linspace(0, T, 401)  # 400 intervals in [0,T]
    u, t = method.solve(t)
    # Note that import * is not legal inside functions so we
    # have to import each specific function.
    from scitools.std import plot, hardcopy, xlabel, ylabel, title
    plot(t, u)
    xlabel('t'); ylabel('u')
    title('Logistic growth: alpha=%s, R=%g, dt=%g' \
          % (problem.alpha, problem.R, t[1]-t[0]))
    # Compare with exponential growth
    #from scitools.std import hold, linspace, exp
    #te = linspace(0, dt*N, N+1)
    #ue = 0.1*exp(0.2*te)
    #hold('on')
    #plot(te, ue)
    hardcopy('tmp.eps')
Пример #4
0
import random
random.seed(42)
N = 500  # no of samples
x = range(N)
y = [random.uniform(-1, 1) for i in x]

import scitools.std as st
st.plot(x, y, '+', axis=[0, N - 1, -1.2, 1.2])
st.hardcopy('tmp.eps')

import matplotlib.pyplot as plt
plt.plot(x, y, '+')
plt.axis([0, N - 1, -1.2, 1.2])
plt.savefig('tmp.eps')
plt.show()
Пример #5
0
"""
As uniform_numbers1.py but the histogram is plotted both as a piecewise
constant curve and a piecewise linear curve. The number of bins is
read from the command line.
"""
import sys
N = int(sys.argv[1])
nbins = int(sys.argv[2])

import numpy as np
np.random.seed(12)
# Vectorized generation of random numbers
samples = np.random.random(size=N)

import scitools.std as st
x1, y1 = st.compute_histogram(samples, nbins, piecewise_constant=True)
x2, y2 = st.compute_histogram(samples, nbins, piecewise_constant=False)
st.plot(x1, y1, 'r', x2, y2, 'b')
st.title('%d samples of uniform numbers on (0,1)' % N)
st.hardcopy('tmp.eps')