def comparison_plot(f, u, Omega, plotfile='tmp'): """Compare f(x,y) and u(x,y) for x,y in Omega in a plot.""" x, y = sm.symbols('x y') f = sm.lambdify([x,y], f, modules="numpy") u = sm.lambdify([x,y], u, modules="numpy") # When doing symbolics, Omega can easily contain symbolic expressions, # assume .evalf() will work in that case to obtain numerical # expressions, which then must be converted to float before calling # linspace below for r in range(2): for s in range(2): if not isinstance(Omega[r][s], (int,float)): Omega[r][s] = float(Omega[r][s].evalf()) resolution = 41 # no of points in plot xcoor = linspace(Omega[0][0], Omega[0][1], resolution) ycoor = linspace(Omega[1][0], Omega[1][1], resolution) xv, yv = ndgrid(xcoor, ycoor) # Vectorized functions expressions does not work with # lambdify'ed functions without the modules="numpy" exact = f(xv, yv) approx = u(xv, yv) figure() surfc(xv, yv, exact, title='f(x,y)', colorbar=True, colormap=hot(), shading='flat') if plotfile: savefig('%s_f.pdf' % plotfile, color=True) savefig('%s_f.png' % plotfile) figure() surfc(xv, yv, approx, title='f(x,y)', colorbar=True, colormap=hot(), shading='flat') if plotfile: savefig('%s_u.pdf' % plotfile, color=True) savefig('%s_u.png' % plotfile)
def non_physical_behavior(I, a, T, dt, theta): """ Given lists/arrays a and dt, and numbers I, dt, and theta, make a two-dimensional contour line B=0.5, where B=1>0.5 means oscillatory (unstable) solution, and B=0<0.5 means monotone solution of u'=-au. """ a = np.asarray(a); dt = np.asarray(dt) # must be arrays B = np.zeros((len(a), len(dt))) # results for i in range(len(a)): for j in range(len(dt)): u, t = solver(I, a[i], T, dt[j], theta) # Does u have the right monotone decay properties? correct_qualitative_behavior = True for n in range(1, len(u)): if u[n] > u[n-1]: # Not decaying? correct_qualitative_behavior = False break # Jump out of loop B[i,j] = float(correct_qualitative_behavior) a_, dt_ = st.ndgrid(a, dt) # make mesh of a and dt values st.contour(a_, dt_, B, 1) st.grid('on') st.title('theta=%g' % theta) st.xlabel('a'); st.ylabel('dt') st.savefig('osc_region_theta_%s.png' % theta) st.savefig('osc_region_theta_%s.pdf' % theta)
def test_easyviz(): from scitools.std import linspace, ndgrid, plot, contour, peaks, \ quiver, surfc, backend, get_backend n = 21 x = linspace(-3, 3, n) xx, yy = ndgrid(x, x, sparse=False) # a basic plot plot(x, x**2, 'bx:') wait() if backend in ['gnuplot', 'vtk', 'matlab', 'dx', 'visit', 'veusz']: # a contour plot contour(peaks(n), title="contour plot") wait() # a vector plot uu = yy vv = xx quiver(xx, yy, uu, vv) wait() # a surface plot with contours zz = peaks(xx, yy) surfc(xx, yy, zz, colorbar=True) wait() if backend == 'grace': g = get_backend() g('exit')
def non_physical_behavior(I, a, T, dt, theta): """ Given lists/arrays a and dt, and numbers I, dt, and theta, make a two-dimensional contour line B=0.5, where B=1>0.5 means oscillatory (unstable) solution, and B=0<0.5 means monotone solution of u'=-au. """ a = np.asarray(a) dt = np.asarray(dt) # must be arrays B = np.zeros((len(a), len(dt))) # results for i in range(len(a)): for j in range(len(dt)): u, t = solver(I, a[i], T, dt[j], theta) # Does u have the right monotone decay properties? correct_qualitative_behavior = True for n in range(1, len(u)): if u[n] > u[n - 1]: # Not decaying? correct_qualitative_behavior = False break # Jump out of loop B[i, j] = float(correct_qualitative_behavior) a_, dt_ = st.ndgrid(a, dt) # make mesh of a and dt values st.contour(a_, dt_, B, 1) st.grid('on') st.title('theta=%g' % theta) st.xlabel('a') st.ylabel('dt') st.savefig('osc_region_theta_%s.png' % theta) st.savefig('osc_region_theta_%s.eps' % theta)