from math import cos def exact_S_solution(t): return g * (1 - cos(t)) def check_S(S, t, step): error = exact_S_solution(t) - S[step] print 't=%.2f S[%d]=%+g error=%g' % (t, step, S[step], error) # Fixed values for a test from math import pi m = 1 b = 2 L = 10 k = 1 beta = 0 S0 = 0 dt = 2 * pi / 40 g = 9.81 N = 200 def w(t): return 0 S = solve(m, k, beta, S0, dt, g, w, N, user_action=check_S)
m = 1 b = 2 L = 10 k = 1 beta = 0 S0 = 0 dt = 2 * pi / 40 g = 9.81 N = 200 def w(t): return 0 S = solve(m, k, beta, S0, dt, g, w, N) # Plot S and the exact solution from scitools.std import * tcoor = linspace(0, N * dt, len(S)) exact = exact_S_solution(tcoor) plot(tcoor, S, 'r', tcoor, exact, 'b', xlabel='time', ylabel='S', legend=('computed S(t)', 'exact S(t)'), savefig='tmp_S.pdf')
dt = 2 * pi / 40 g = 9.81 w_formula = '0' N = 200 m, b, L, k, beta, S0, dt, g, w, N = \ init_prms(m, b, L, k, beta, S0, dt, g, w_formula, N) # Vectorize the StringFunction w w_formula = str(w) # keep this to see if w=0 later if ' else ' in w_formula: w = vectorize(w) # general vectorization else: w.vectorize(globals()) # more efficient (when no if) S = solve(m, k, beta, S0, dt, g, w, N, user_action=plot_S) # First make a hardcopy of the the last plot of Y savefig('tmp_Y.pdf') # Make plots of several additional interesting quantities tcoor = linspace(0, N * dt, N + 1) S = array(S) plots = 2 # number of rows of plots if beta != 0: plots += 1 if w_formula != '0': plots += 1 # Position Y(t)
from boxspring import init_prms, solve from math import cos def exact_S_solution(t): return g*(1 - cos(t)) # Fixed values for a test from math import pi m = 1; b = 2; L = 10; k = 1; beta = 0; S0 = 0; dt = 2*pi/40; g = 9.81; N = 200; def w(t): return 0 S = solve(m, k, beta, S0, dt, g, w, N) # Plot S and the exact solution from scitools.std import * tcoor = linspace(0, N*dt, len(S)) exact = exact_S_solution(tcoor) plot(tcoor, S, 'r', tcoor, exact, 'b', xlabel='time', ylabel='S', legend=('computed S(t)', 'exact S(t)'), savefig='tmp_S.pdf') # Plot the error figure() # new plot window S = array(S) # turn list into NumPy array for computations error = exact - S plot(tcoor, error, xlabel='time', ylabel='error', savefig='tmp_error.pdf')
from boxspring import init_prms, solve from scitools.std import * def plot_S(S, t, step): if step == 0: # nothing to plot yet return tstart = 0 tstop = N*dt tcoor = linspace(0, t, step) S = array(S[:len(tcoor)]) Y = w(tcoor) - L - S - b/2. # (w, L, b are global vars.) plot(tcoor, Y) # Default values m = 1; b = 2; L = 10; k = 1; beta = 0; S0 = 1; dt = 2*pi/40; g = 9.81; w_formula = '0'; N = 200; m, b, L, k, beta, S0, dt, g, w, N = \ init_prms(m, b, L, k, beta, S0, dt, g, w_formula, N) w.vectorize(globals()) # w must work with arrays in plot_S S = solve(m, k, beta, S0, dt, g, w, N, user_action=plot_S) # Make hardcopy of last plot of S savefig('tmp_S.pdf')
from boxspring import init_prms, solve import sys, time, os # make sure that we find the boxspring_figure module in ../oo: #sys.path.insert(0, '../oo') # better: sys.path.insert(0, os.path.join(os.pardir, 'oo')) from boxspring_figure import draw, set_figure_size def animate_figure(S, t, step): draw(L, w(t+dt), S[step], box_size) time.sleep(0.1) # Default values from math import pi m = 1; b = 2; L = 10; k = 1; beta = 0; S0 = 1; dt = 2*pi/40; g = 9.81; w_formula = '0'; N = 80; box_size = 4 w_max = 0 S_max = 15 set_figure_size(L, w_max, S_max, box_size) m, b, L, k, beta, S0, dt, g, w, N = \ init_prms(m, b, L, k, beta, S0, dt, g, w_formula, N) S = solve(m, k, beta, S0, dt, g, w, N, user_action=animate_figure)
from boxspring import init_prms, solve from math import cos def exact_S_solution(t): return g*(1 - cos(t)) def check_S(S, t, step): error = exact_S_solution(t) - S[step] print 't=%.2f S[%d]=%+g error=%g' % (t, step, S[step], error) # Fixed values for a test from math import pi m = 1; b = 2; L = 10; k = 1; beta = 0; S0 = 0 dt = 2*pi/40; g = 9.81; N = 200 def w(t): return 0 S = solve(m, k, beta, S0, dt, g, w, N, user_action=check_S)
sys.path.insert(0, os.path.join(os.pardir, 'oo')) from boxspring_figure import draw, set_figure_size def animate_figure(S, t, step): draw(L, w(t + dt), S[step], box_size) time.sleep(0.1) # Default values from math import pi m = 1 b = 2 L = 10 k = 1 beta = 0 S0 = 1 dt = 2 * pi / 40 g = 9.81 w_formula = '0' N = 80 box_size = 4 w_max = 0 S_max = 15 set_figure_size(L, w_max, S_max, box_size) m, b, L, k, beta, S0, dt, g, w, N = \ init_prms(m, b, L, k, beta, S0, dt, g, w_formula, N) S = solve(m, k, beta, S0, dt, g, w, N, user_action=animate_figure)