예제 #1
0
파일: tools.py 프로젝트: SpuqTeam/spuq
 def plotConvergence(cls, scale, error4scale, title, legend, regression = False):
     p.figure()
     p.loglog(scale, error4scale, "k-d", xlabel = "samples", ylabel = "error",)  # semilogy
     p.title(title)
     p.legend(legend)
     if regression and len(error4scale) > 1:
         p.hold('on')
         lineSpace = np.array((scale[0], scale[-1]))
         slope, intercept, r_value, p_value, std_err = stats.linregress(scale, np.log10(error4scale))
         line = np.power(10, slope * lineSpace + intercept)
         p.plot(lineSpace, line, "k:", legend = "{slope:.2}x + c" \
                .format(slope = slope))
     p.hardcopy(cls.folder + title + "  " + t.strftime('%Y-%m-%d_%H-%M') + ".pdf")
예제 #2
0
파일: tools.py 프로젝트: SpuqTeam/spuq
    def plotConvergenceMean(cls, approx, exact, regression = False):
        nApprox = approx.shape[0]
        errorMean = np.empty(nApprox)
        for curSampleN in range(0, nApprox):
            errorMean[curSampleN] = cls.computeError(np.mean(approx[:curSampleN + 1], 0), exact)

        p.figure()
        # lSpace = p.linspace(0, errorMean.size, errorMean.size)
        lSpace = list(i * (len(exact) ** 2) for i in range(nApprox))
        p.loglog(lSpace, errorMean, "k-", xlabel = "samples", ylabel = "error",
                   legend = "E[(Q_M - E[Q])^2]^{1/2}")
        print("CompCost: {compCost:5.2e}\t Error: {error:5.4e}"
              .format(compCost = lSpace[-1], error = errorMean[-1]))
        if regression:
            p.hold('on')
            lineSpace = np.array((lSpace[0], lSpace[-1]))
            slope, intercept, r_value, p_value, std_err = stats.linregress(lSpace, np.log10(errorMean))
            line = np.power(10, slope * lineSpace + intercept)
            p.plot(lineSpace, line, "k:", legend = "{slope:.2}x + c" \
                   .format(slope = slope))
예제 #3
0
파일: cooling.py 프로젝트: imranal/inf5620
    
    n = 0
    while n<N and T[n]>=temp_after_death-0.0001 :
        T[n+1] = (T[n] - k*dt*((1-theta)*T[n] - T_s))/(1.0 + theta*dt*k)
        n = n + 1

    t = linspace(0,t_end,N+1)
    return T,t
    
    
def exact_sol(T0,k,T_s,t_end,dt):
    N = int(round(t_end/float(dt)))
    t = linspace(0,t_end,N+1)
    exact = (T0-T_s)*exp(-t*k) + T_s
    return exact,t
    

if __name__ == "__main__":
    T,t = cooling(37.5, 0.1, 14.0, 60.0, 5, 0.5)
    plot(t,T,"r--o")
    T_exact,t = exact_sol(37.5, 0.1, 14.0, 60.0, 5)
    #figure()
    hold("on")
    plot(t,T_exact)
    legend(["discrete sol","exact sol"])

    print "maximum error :" ,max(abs(T-T_exact))
    raw_input("press enter")


예제 #4
0
h0 = 2277.  # Height of the top of the mountain (m)
R = 4.  # The radius of the mountain (km)

x = y = np.linspace(-10., 10., 41)
xv, yv = plt.ndgrid(x, y)  # Grid for x, y values (km)
hv = h0 / (1 + (xv**2 + yv**2) / (R**2))  # Compute height (m)

x = y = np.linspace(-10., 10., 11)
x2v, y2v = plt.ndgrid(x, y)  # Define a coarser grid for the vector field
h2v = h0 / (1 + (x2v**2 + y2v**2) / (R**2))  # Compute height for new grid
dhdx, dhdy = np.gradient(h2v)  # Compute the gradient vector (dh/dx,dh/dy)

# Draw contours and gradient field of h
plt.figure(9)
plt.quiver(x2v, y2v, dhdx, dhdy, 0, 'r')
plt.hold('on')
plt.contour(xv, yv, hv)
plt.axis('equal')
# end draw contours and gradient field of h

x = y = np.linspace(-5, 5, 11)
xv, yv = plt.ndgrid(x, y)
u = xv**2 + 2 * yv - .5 * xv * yv
v = -3 * yv

# Draw 2D-field
plt.figure(10)
plt.quiver(xv, yv, u, v, 200, 'b')
plt.axis('equal')
# end draw 2D-field
xv, yv = plt.ndgrid(x, y)
hv = h0/(1+(xv**2+yv**2)/(R**2))

s = np.linspace(0, 2*np.pi, 100)
curve_x = 10*(1 - s/(2*np.pi))*np.cos(s)
curve_y = 10*(1 - s/(2*np.pi))*np.sin(s)
curve_z = h0/(1 + 100*(1 - s/(2*np.pi))**2/(R**2))

# Simple plot of mountain
plt.figure(1)
plt.mesh(xv, yv, hv)

# Simple plot of mountain and parametric curve
plt.figure(2)
plt.surf(xv, yv, hv)
plt.hold('on')

# add the parametric curve. Last parameter controls color of the curve
plt.plot3(curve_x, curve_y, curve_z, 'b-')
# endsimpleplots

# Default two-dimensional contour plot
plt.figure(3)
plt.contour(xv, yv, hv)

# Default three-dimensional contour plot
plt.figure(4)
plt.contour3(xv, yv, hv, 10)

# 10 contour lines (equally spaced contour levels)
plt.figure(5)