簡體   English   中英

如何修改圖形以顯示正確的軸信息?

[英]How can I modify my graph so it displays the proper information for the axes?

我編寫了一個程序,通過有限差分法來數值求解熱方程( u_t = k * u_xx )。

對於我的問題, uxt函數,其中0 < x < Lt > 0 我為我的問題指定了L = 1 (桿的長度),並且終止時間T = 10秒,所以我希望圖形顯示在域(x,t) \\in {(0,1) x (0, 10)} 但是,我的軸根本沒有意義。 它從0 - 40值繪制x軸,而t軸顯示-0.25 - 0.00

如何編輯代碼,以便在繪制取決於x, t u ,圖形將顯示x值,范圍為0 - 1t范圍為0 - 10秒?

在此先感謝您提供的所有幫助。 非常感謝。 這是我正在使用的代碼:

    ## This program is to implement a Finite Difference method approximation
## to solve the Heat Equation, u_t = k * u_xx,
## in 1D w/out sources & on a finite interval 0 < x < L. The PDE
## is subject to B.C: u(0,t) = u(L,t) = 0,
## and the I.C: u(x,0) = f(x).
import numpy as np
import matplotlib.pyplot as plt

# Parameters    
L = 1 # length of the rod
T = 10 # terminal time
N = 40 # spatial values
M = 1600 # time values/hops; (M ~ N^2)
s = 0.25 # s := k * ( (dt) / (dx)^2 )

# uniform mesh
x_init = 0
x_end = L
dx = float(x_end - x_init) / N

x = np.arange(x_init, x_end, dx)
x[0] = x_init

# time discretization
t_init = 0
t_end = T
dt = float(t_end - t_init) / M

t = np.arange(t_init, t_end, dt)
t[0] = t_init

# time-vector
for m in xrange(0, M):
    t[m] = m * dt

# spatial-vector
for j in xrange(0, N):
    x[j] = j * dx

# definition of the solution u(x,t) to u_t = k * u_xx
u = np.zeros((N, M+1)) # array to store values of the solution

# Finite Difference Scheme:

u[:,0] = x * (x - 1) #initial condition

for m in xrange(0, M):
    for j in xrange(1, N-1):
        if j == 1:
            u[j-1,m] = 0 # Boundary condition
        elif j == N-1:
            u[j+1,m] = 0 # Boundary Condition
        else:
            u[j,m+1] = u[j,m] + s * ( u[j+1,m] - 
            2 * u[j,m] + u[j-1,m] )

# for graph    
print u, x, t
plt.plot(u)
plt.title('Finite Difference Approx. to Heat Equation')
plt.xlabel('x-axis')
plt.ylabel('time (seconds)')
plt.axis()
plt.show()

似乎x軸上的任何顯示都反映了我為代碼占用的空間步長數( N = 40 )。 我以為np.arange(x_init, x_end, dx)將以步長dx返回間隔(x_init, x_end)內的均勻間隔值? 那我在做什么錯? 再次感謝。

您的代碼遇到了一些問題,因為您的u變成40x1601而不是40x1600。 但是,我認為您(校正u之后)可能要繪制的圖是

corrected_u = u[:,:-1:]
plt.pcolor(t, x, corrected_u)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM