python三维图如何标注曲面_在python中用matplotlib创建三维曲面图

我试图绘制一个三维曲面,但我遇到了一些麻烦,因为matplotlib的文档似乎不太全面,而且缺少示例。总之,我写的程序是用有限差分法数值求解热方程。这是我的代码:## 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

from matplotlib import cm

from mpl_toolkits.mplot3d import Axes3D

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

这是我写的试图绘制3D曲面图的内容:

^{pr2}$

当我运行代码来绘制图形时,我遇到了这个错误:“ValueError:shape mismatch:两个或多个数组在轴1上具有不兼容的维度。”

请,任何和所有的帮助是非常感谢。我认为出现错误是因为我将u定义为Nx(M+1)矩阵,但必须使原始程序运行。我不知道如何纠正这一点,使图表正确地绘制。谢谢!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值