使用 np.outer 生成圆柱曲面 [英] Generating a Cylindrical Surface with np.outer

查看:15
本文介绍了使用 np.outer 生成圆柱曲面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前能够使用 np.outer 生成和绘制球面:

I was able to previously generate and plot a spherical surface using np.outer:

u = np.linspace(0,  2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = R * np.outer(np.cos(u), np.sin(v))
y = R * np.outer(np.sin(u), np.sin(v))
z = R * np.outer(np.ones(np.size(u)), np.cos(v))

其中 x、y、z 为 100 x 100.结果见图,忽略粒子痕迹"

where x, y, z are of dim 100 x 100. See figure for result and ignore "particle traces"

我了解球体和圆柱体的数学和方向余弦,所以我认为混淆来自不了解 np.outer 到底在做什么.

I understand the math and direction cosines of a sphere as well as cylinder so I think the confusion comes from not understanding what exactly np.outer is doing.

我希望以下内容会生成一个圆柱体:

I would expect that the following would generate a cylinder:

u = np.linspace(0,  2*np.pi, 100)
h = 5
x = R * np.cos(u)                 
y = R * np.sin(u)                 
z = np.linspace(0, h, np.size(u))  

但是当我尝试绘图时,没有生成任何内容,我相信这是由于数组维度的差异:(100,).结果见图.

but when I try to plot, nothing is generated and I believe it is due to the discrepancy in array dimensions: (100,). See Figure for result.

我尝试的另一件事是:

u = np.linspace(0,  2*np.pi, 100)
h = 5
x = R * np.outer(np.ones(np.size(u)), np.cos(u))                                     
y = R * np.outer(np.ones(np.size(u)), np.sin(u))                                     
z = np.linspace(0, h, np.size(u)) * np.outer(np.ones(np.size(u)), np.ones(np.size(u)))

结果见图.

我不明白为什么我不能让它工作.其他注意事项:- 显然这仅适用于圆柱体的侧表面,因此如果有任何方法可以在相同的 x、y、z 中生成顶/底表面,那将是理想的- 此代码必须独立于 matplotlib 工作,因为点本身也是必不可少的- 如果这可以通过 np.outer 实现,这也将是理想的

I don't understand why I can't get this to work. Other notes: - Clearly this is only for the lateral surface of the cylinder so if there is any way to generate the top/bottom surfaces in the same x, y, z that would be ideal - This code must work independently of matplotlib as the points themselves are also essential - If this accomplishable with np.outer, this would also be ideal

我提前感谢任何帮助.

那么,我如何生成顶面和底面?不会吧?:

How, then, could I generate the top and bottom surfaces? Would it not be?:

x = R * np.outer(np.ones(np.size(u)), np.cos(u))
y = R * np.outer(np.ones(np.size(u)), np.sin(u))
z = h * np.outer(np.ones(np.size(u)), np.ones(np.size(u)))

这没有给我以前的结果.

This gives me no result as before.

推荐答案

试试这个:

u = np.linspace(0,  2*np.pi, 100)
h = 5
x = R * np.outer(np.ones(np.size(u)), np.cos(u))                                     
y = R * np.outer(np.ones(np.size(u)), np.sin(u))                                     
z = np.outer(np.linspace(0, h, np.size(u)), np.ones(np.size(u)))

此外,请查看 np 上的 文档.如果您不确定它的作用,则外部.它只是简单地取两个向量的外积并产生一个矩阵.

Also, check the docs on np.outer if you are unsure what it does. It simply take the outer product of two vectors and produces a matrix.

这篇关于使用 np.outer 生成圆柱曲面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆