简体   繁体   English

如何修改图形以显示正确的轴信息?

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

I have written a program to solve the Heat Equation ( u_t = k * u_xx ) numerically by method of Finite Differences. 我编写了一个程序,通过有限差分法来数值求解热方程( u_t = k * u_xx )。

For my problem, u is function of x and t , where 0 < x < L and t > 0 . 对于我的问题, uxt函数,其中0 < x < Lt > 0 I have specified L = 1 (the length of the rod) and the terminal time T = 10 seconds for my problem, so I would like for the graph to be displayed on the domain (x,t) \\in {(0,1) x (0, 10)} . 我为我的问题指定了L = 1 (杆的长度),并且终止时间T = 10秒,所以我希望图形显示在域(x,t) \\in {(0,1) x (0, 10)} However, my axes just don't make sense. 但是,我的轴根本没有意义。 It is plotting the x-axis from values of 0 - 40 and the t-axis is showing -0.25 - 0.00 . 它从0 - 40值绘制x轴,而t轴显示-0.25 - 0.00

How can I edit my code so that when I plot u which depends on x, t the graph will display for values of x ranging from 0 - 1 and t ranging from 0 - 10 seconds?? 如何编辑代码,以便在绘制取决于x, t u ,图形将显示x值,范围为0 - 1t范围为0 - 10秒?

Thanks in advance for any and all help. 在此先感谢您提供的所有帮助。 it is very greatly appreciated. 非常感谢。 Here is the code I am working with: 这是我正在使用的代码:

    ## 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()

It appears that whatever displays for the x-axis reflects the number of step sizes in space that I take ( N = 40 ) for my code. 似乎x轴上的任何显示都反映了我为代码占用的空间步长数( N = 40 )。 I thought np.arange(x_init, x_end, dx) would return evenly spaced values within the interval (x_init, x_end) with step size dx ? 我以为np.arange(x_init, x_end, dx)将以步长dx返回间隔(x_init, x_end)内的均匀间隔值? So what am I doing wrong? 那我在做什么错? Thanks again. 再次感谢。

You have some issues with your code as your u turns out to be 40x1601 and not 40x1600. 您的代码遇到了一些问题,因为您的u变成40x1601而不是40x1600。 However, I think the plot you may be after (after correcting u) is 但是,我认为您(校正u之后)可能要绘制的图是

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何修改此代码,以免每次都重新绘制图形,matplotlib - How can I modify this code so that I am not replotting my graph everytime, matplotlib 如何修改我的 for 循环以提取正确的数据 - How can I modify my for loop so that it extracts the correct data 如何确保我的被子数据 package 在目录 UI 视图中默认显示相关信息? - How can I ensure that my Quilt data package displays relevant information by default in the catalog UI view? 我如何编程我的代码,以便它在LIFO结构的python中从上到下显示堆栈顺序 - how can I program my code so it displays a stack order from top to bottom in python for a LIFO structure 如何绘制带有分类轴和数字(基准)轴的折线图? - How can I plot line graph with categorical and numeric (datum) axes? 当我将Plotly Interactive Graph悬停在折线图中的特定点上时,如何更改显示的内容 - How can I change what my Plotly Interactive Graph displays when it hovers over a specific point in the line graph 如何将其转换为unicode,以便正确显示? - How can I convert this to unicode so it displays properly? 如何获取/定义我的图形的输入 output 名称或以正确的形式冻结它 - How can i obtain/define input output names of my graph or freeze it in proper form 如何修改我的代码,以便列表中的图像显示在行和图块中? - How can I modify my code so that images from a list appear in rows and tiles? 如何获取Web图形的javascript标记内的数据,以便创建自己的图形? - How can I get the data inside a javascript tag for a web graph, so I can make my own graph?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM