Python-集成函数并绘制结果 [英] Python - Integrating a function and plotting results

查看:82
本文介绍了Python-集成函数并绘制结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试数值求解伯努利的梁方程并绘制结果.该方程的一阶导数是斜率,二阶导数是挠度.我逐步解决了这个问题,首先绘制函数,然后对其进行积分,然后将积分结果绘制在同一张图上.

I'm trying to solve Bernoulli's beam equation numerically and plotting the results. First derivative of the equation is the slope and the second derivative is the deflection. I approached the problem step-by-step, first plot the function and then integrate it and plot the integration results on the same diagram.

到目前为止,我的代码如下.我怀疑问题在于,integrate.quad返回单个值,而我正试图从中获取多个值.有人知道如何解决吗?

My code so far is bellow. I suspect the problem lies in the fact that the integrate.quad returns a single value and I'm trying to get multiple values from it. Does anyone know how to approach it?

from scipy import integrate
import numpy as np

from pylab import *

# Beam parameters
L = 100
w = 10
h = 10
I = (w*h**3)/12
E = 200000
F = 100

def d2y_dx2(x):
    return (-F*x)/(E*I)

a = 0.0
b = L

res, err = integrate.quad(d2y_dx2, a, b)

t = np.linspace(a,b,100)

ax = subplot(111)
ax.plot(t, d2y_dx2(t))
ax.plot(t, res(t))

show()

波纹管是用willcrack的答案修改的代码.该代码现在可以使用,但是结果不正确.在底部,我添加了使用正确的梁方程的解析解绘制结果图的代码.

Bellow is modified code with willcrack's answer. This code now works, but the results are not correct. On the bottom I added the code for plotting the results using analytical solutions of the beam equation which are correct.

from scipy import integrate
import numpy as np
import matplotlib.pyplot as plt

# Beam parameters
L = 100
w = 10
h = 10
I = (w*h**3)/12
E = 200000
F = 100

# Integration parameters
a = 0.0
b = L

# Define the beam equation
def d2y_dx2(x,y=None):
    return (-F*x)/(E*I)


def something(x):
    return integrate.quad(d2y_dx2)[0]
    
# Define the integration1 - slope
def slope(t):
    slope_res = []
    for x in t:
        res1, err = integrate.quad(d2y_dx2, a, b)
        slope_res.append(res1)
    return slope_res

# Define the integration1 - deflection
def defl(t1):
    defl_res = []
    for t in t1:
        res2, err = integrate.dblquad(d2y_dx2,a,b, lambda x: a, lambda x: b)
        defl_res.append(res2)
    return defl_res

# Plot
fig, (ax1, ax2, ax3) = plt.subplots(3)
t = np.linspace(a,b,100)
t1 = np.linspace(a,b,100)
ax1.plot(t, d2y_dx2(t))
ax2.plot(t, slope(t))
ax3.plot(t1, defl(t1))
plt.show()

以下是分析解决方案,代码和结果.偏转光束的形状被扭转,光束的末端在x = 0.

Analytical solution, code and results bellow. The shape of deflected beam is turned around, the end of the beam is at x = 0.

from __future__ import division  #to enable normal floating division
import numpy as np
import matplotlib.pyplot as plt

# Beam parameters
w = 10  #beam cross sec width (mm)
h = 10  #beam cross sec height (mm)
I = (w*h**3)/12   #cross sec moment of inertia (mm^4)
I1 = (w*h**3)/12
E = 200000   #steel elast modul (N/mm^2)
L = 100  #beam length(mm)
F = 100   #force (N)

# Define equations
def d2y_dx2(x):
    return (-F*x)/(E*I)

def dy_dx(x):
    return (1/(E*I))*(-0.5*F*x**2 + 0.5*F*L**2)

def y(x):
    return (1/(E*I))*(-(1/6)*F*(x**3) + (1/2)*F*(L**2)*x - (1/3)*F*(L**3))

# Plot
fig, (ax1, ax2, ax3) = plt.subplots(3)

a = 0
b = L
x = np.linspace(a,b,100)

ax1.plot(x, d2y_dx2(x))
ax2.plot(x, dy_dx(x))
ax3.plot(x, y(x))
plt.show()

推荐答案

也许您可以尝试这样的事情

Maybe you can try something like this

from scipy import integrate
import numpy as np
import matplotlib.pyplot as plt

# Beam parameters
L = 100
w = 10
h = 10
I = (w*h**3)/12
E = 200000
F = 100

# Integration parameters
a = 0.0
b = L

# Define the beam equation
def d2y_dx2(x,y=None):
    return (-F*x)/(E*I)


def something(x):
    return integrate.quad(d2y_dx2)[0]
    
# Define the integration1 - slope
def slope(t):
    slope_res = []
    for x in t:
        res1, err = integrate.quad(d2y_dx2, a, b)
        slope_res.append(res1)
    return slope_res

# Define the integration1 - deflection
def defl(t1):
    defl_res = []
    for t in t1:
        res2, err = integrate.dblquad(d2y_dx2,a,b, lambda x: a, lambda x: b)
        defl_res.append(res2)
    return defl_res

# Plot
fig, (ax1, ax2, ax3) = plt.subplots(3)
t = np.linspace(a,b,100)
t1 = np.linspace(a,b,100)
ax1.plot(t, d2y_dx2(t))
ax2.plot(t, slope(t))
ax3.plot(t1, defl(t1))
plt.show()

结果:

这篇关于Python-集成函数并绘制结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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