Sympy:将空图作为输出

时间:2019-06-04 14:02:38

标签: python-3.x plot sympy

我正在尝试用sympy绘制一些函数,第一个图是完美的,而第二个图是空的。 两种绘图命令都相似,但是我不明白为什么第一个给出正确的绘图,而第二个却没有。

# In[1]:


import sympy as sym              
import matplotlib.pyplot as plt  
import math
import numpy as np


# In[2]:


# general data variables:
max_housing_width, max_housing_height, transmission_ratio_1, transmission_ratio_2, motor_power, service_factor, impact_energy, impact_rate, rpm_motor = sym.symbols('max_housing_width, max_housing_height, transmission_ratio_1, transmission_ratio_2, motor_power, service_factor, impact_energy, impact_rate, rpm_motor', real = True)

# gear design & optimization variables
pressure_angle, module_1, module_2, d_p1, d_p2, d_g1, d_g2, di_p1, di_p2, di_g1, di_g2, z_p1, z_p2, z_g1, z_g2, b_p1, b_p2, b_g1, b_g2, mass_p1, mass_p2, mass_g1, mass_g2 = sym.symbols('pressure_angle, module_1, module_2, d_p1, d_p2, d_g1, d_g2, di_p1, di_p2, di_g1, di_g2, z_p1, z_p2, z_g1, z_g2, b_p1, b_p2, b_g1, b_g2, mass_p1, mass_p2, mass_g1, mass_g2', real = True)


# Steel properties
yield_strength,tensile_strength, fatigue_strength, young_modulus, shear_modulus, safe_strength, poisson_modulus,thermal_expansion_coeff  = sym.symbols('yield_strength,tensile_strength, fatigue_strength, young_modulus, shear_modulus, safe_strength, poisson_modulus,thermal_expansion_coeff', real = True)

# Bearings
# general data
b1_width, b2_width, reliability, service_life = sym.symbols('b1_width, b2_width, reliability, service_life', real = True)
# Catalogue data
d,D,C0,C,Pu = sym.symbols('d,D,C0,C,Pu', real = True)

# Shaft

# shaft geometry
d1,d2,d3,d4,d5 = sym.symbols('d1,d2,d3,d4,d5', real = True)
moment_inertia1, moment_inertia2, moment_inertia3, moment_inertia4, moment_inertia5 = sym.symbols('moment_inertia1, moment_inertia2, moment_inertia3, moment_inertia4, moment_inertia5', real = True) 
polar_moment1, polar_moment2, polar_moment3, polar_moment4, polar_moment5 = sym.symbols('polar_moment1, polar_moment2, polar_moment3, polar_moment4, polar_moment5', real = True) 
Lu, L1, L2, L3, L4, L5, axial_clearance = sym.symbols('Lu, L1, L2, L3, L4, L5, axial_clearance', real = True)
c1, c2, c3, c4, c5, c6 = sym.symbols('c1, c2, c3, c4, c5, c6', real = True)

#
K_u, K_m = sym.symbols('K_u, K_m', real = True)
J_p1, J_p2, J_g1, J_g2, J_r, J_u, J_m, J_p12, J_g12, J_gb, J_2, J_1, J_p = sym.symbols('J_p1, J_p2, J_g1, J_g2, J_r, J_u, J_m, J_p12, J_g12, J_gb, J_2, J_1, J_p', real = True)
tau_1, tau_2 = sym.symbols('tau_1, tau_2', real = True)
omega_nf = sym.Symbol('omega_nf')


# In[3]:


# Steel 16NiCrMo12, pg.:1154 manuale ingegnere
# shaft material: http://www.mtacciai.com/it/acciai/da-cementazione-en-10084/16nicrmo12

steel_prop = {
    yield_strength : 785, # [MPa] 30 [mm] bar
    tensile_strength : 1080, # [MPa] 30 [mm] bar
    safe_strength : 0.75*1080,  # [MPa] at 1×10^4 cycles (rotating bending)
    fatigue_strength : 0.45*1080,  # [MPa] at 2×10^6 cycles (rotating bending)
    young_modulus : 210000, # [MPa]
    poisson_modulus: 0.3,
    shear_modulus : 80000, # [MPa]
    thermal_expansion_coeff : 24*10**-6 # [1/°C] https://www.steel-grades.com/Steel-Grades/Carbon-Steel/AISI-E9314.html
}

general_data = {
    max_housing_width : 60,      # [mm]
    max_housing_height : 120,    # [mm]
    transmission_ratio_1 : 0.3,  # [--]
    transmission_ratio_2 : 0.64, # [--]
    motor_power : 800,           # [W]
    rpm_motor: 1400,             # [rev/min]
    service_factor : 1.6,        # [--]
    impact_energy : 1.6,         # [J]
    impact_rate : 5600           # [impact/min]
}


# In[4]:


bearings_data = {
    b1_width: 8, # [mm]
    b2_width: 8, # [mm]
    reliability: 99, # [%]
    service_life: 1000 # [h]
}


# In[5]:


gear_optimized_data = {
    pressure_angle : np.deg2rad(20),
    module_1 : 1, # [mm]
    module_2 : 1, # [mm]
    d_p1 : 23, # [mm]
    d_p2 : 39, # [mm]
    d_g1 : 77, # [mm]
    d_g2 : 61, # [mm]
    z_p1 : 23, # [--]
    z_p2 : 39, # [--]
    z_g1 : 77, # [--]
    z_g2 : 61, # [--]
    b_p1 : 11, # [mm]
    b_p2 : 9, # [mm]
    b_g1 : 11, # [mm]
    b_g2 : 9, # [mm]
    di_p1 : 12, # [mm]
    di_p2 : 12, # [mm]
    di_g1 : 14, # [mm]
    di_g2 : 14, # [mm]
    mass_p1 : 0.025, # [kg]
    mass_p2 : 0.075, # [kg]
    mass_g1 : 0.382, # [kg]
    mass_g2 : 0.192, # [kg]
    transmission_ratio_1 : 0.2987,
    transmission_ratio_2 : 0.6393
}


# In[6]:


sinusoidal_results = {
    axial_clearance : 6, # [mm]
}


# In[7]:


shaft_lenghts = {
    Lu: 50,
    L1: 0.5*bearings_data[b1_width] + sinusoidal_results[axial_clearance] + 0.5*gear_optimized_data[b_g1], # [mm]
    L2: general_data[max_housing_width] - (2*sinusoidal_results[axial_clearance] + 0.5*gear_optimized_data[b_g1] + 0.5*gear_optimized_data[b_g2]), # [mm]
    L3: 0.5*bearings_data[b2_width] + sinusoidal_results[axial_clearance] + 0.5*gear_optimized_data[b_g2] # [mm]   
}
motor_shaft_lenghts = {
    L4 : 0.5*(gear_optimized_data[b_p1] + 2*sinusoidal_results[axial_clearance]) + 0.5*(gear_optimized_data[b_p2] + 2*sinusoidal_results[axial_clearance]),
    L5 : 50
}

shaft_diameters = {
    d1:12, # [mm]
    d2:14, # [mm]
    d3:22, # [mm]
    d4:14,  # [mm]
    d5:12  # [mm]
}

m_inertia = {
    moment_inertia1: (np.pi*(shaft_diameters[d1]**4))/64, # [mm^4]
    moment_inertia2: (np.pi*(shaft_diameters[d2]**4))/64, # [mm^4]
    moment_inertia3: (np.pi*(shaft_diameters[d3]**4))/64, # [mm^4]
    moment_inertia4: (np.pi*(shaft_diameters[d4]**4))/64, # [mm^4]
    moment_inertia5: (np.pi*(shaft_diameters[d5]**4))/64, # [mm^4]
    polar_moment1: (np.pi*(shaft_diameters[d1])**4)/32, # [mm^4]
    polar_moment2: (np.pi*(shaft_diameters[d2])**4)/32, # [mm^4]
    polar_moment3: (np.pi*(shaft_diameters[d3])**4)/32, # [mm^4]
    polar_moment4: (np.pi*(shaft_diameters[d4])**4)/32, # [mm^4]
    polar_moment5: (np.pi*(shaft_diameters[d5])**4)/32, # [mm^4]
}

m_inertia_in_meters = {
    moment_inertia1: (np.pi*((shaft_diameters[d1]*10**(-3))**4))/64, # [mm^4]
    moment_inertia2: (np.pi*((shaft_diameters[d2]*10**(-3))**4))/64, # [mm^4]
    moment_inertia3: (np.pi*((shaft_diameters[d3]*10**(-3))**4))/64, # [mm^4]
    moment_inertia4: (np.pi*((shaft_diameters[d4]*10**(-3))**4))/64, # [mm^4]
    moment_inertia5: (np.pi*((shaft_diameters[d5]*10**(-3))**4))/64, # [mm^4]
    polar_moment1: (np.pi*(shaft_diameters[d1]*10**(-3))**4)/32, # [mm^4]
    polar_moment2: (np.pi*(shaft_diameters[d2]*10**(-3))**4)/32, # [mm^4]
    polar_moment3: (np.pi*(shaft_diameters[d3]*10**(-3))**4)/32, # [mm^4]
    polar_moment4: (np.pi*(shaft_diameters[d4]*10**(-3))**4)/32, # [mm^4]
    polar_moment5: (np.pi*(shaft_diameters[d5]*10**(-3))**4)/32, # [mm^4]
}


# ## Torsional Vibration

# It was decided to model the system as a three disk rotor system, whose equations of motion for free vibrations are obtained in matrix form as follows:

# In[8]:


J_mat = sym.Matrix([[J_1, 0, 0],[0, J_gb, 0],[0, 0, J_2]])
K_mat = sym.Matrix([[K_m, - K_m, 0],[-K_m,(K_m + K_u),-K_u],[0,-K_u,K_u]])
A_mat = -(omega_nf**2)*J_mat + K_mat
A_mat


# ### Characteristic (or Frequency) Equations

# In[9]:


detA = A_mat.det()
torsional_natural_frequencies  = sym.solve(detA,omega_nf, dict = True)


# ### Modelling the equivalent shaft: Fast configuration

# #### Torsional stiffnesses

# In[10]:


# User shaft
# K1
K1 = (((steel_prop[shear_modulus]*10**6)*m_inertia_in_meters[polar_moment1])/(shaft_lenghts[Lu]*10**-3))

# Motor shaft

# K2
L21 = (0.5*bearings_data[b1_width])*10**(-3)
L22 = (shaft_lenghts[L1] - 0.5*bearings_data[b1_width])*10**(-3)

K21 = (((steel_prop[shear_modulus]*10**6)*m_inertia_in_meters[polar_moment1])/(L21))
K22 = (((steel_prop[shear_modulus]*10**6)*m_inertia_in_meters[polar_moment2])/(L22))

K2 = pow(pow(K21,-1) + pow(K22,-1),-1)

# K3
L31 = 0.5*gear_optimized_data[b_g1]*10**(-3)
L32 = (shaft_lenghts[L2] - 0.5*gear_optimized_data[b_g1] - 0.5*gear_optimized_data[b_g2])*10**(-3)
L33 = 0.5*gear_optimized_data[b_g2]*10**(-3)
K31 = (((steel_prop[shear_modulus]*10**6)*m_inertia_in_meters[polar_moment2])/(L31))
K32 = (((steel_prop[shear_modulus]*10**6)*m_inertia_in_meters[polar_moment3])/(L32))
K33 = (((steel_prop[shear_modulus]*10**6)*m_inertia_in_meters[polar_moment4])/(L33))

K3 = pow(pow(K31,-1) + pow(K32,-1) + pow(K33,-1),-1)

## User shaft equivalent torsional stiffnesse
K_user_2 = (gear_optimized_data[transmission_ratio_2]**2)*pow(pow(K1, -1) + pow(K2, -1) + pow(K3, -1),-1)


# Motor shaft

K4 = ((steel_prop[shear_modulus]*10**6)*m_inertia_in_meters[polar_moment1])/(motor_shaft_lenghts[L4]*10**-3)
K5 = ((steel_prop[shear_modulus]*10**6)*m_inertia_in_meters[polar_moment1])/(motor_shaft_lenghts[L5]*10**-3)
K_motor_2 = pow(pow(K5,-1),-1)

equivalent_elastic_2  ={
    K_u : K_user_2,
    K_m : K_motor_2
}


# In[11]:


print(K3)


# #### Moment of inertia

# In[12]:


mass_inertia  ={
    J_p1 : 0.5*gear_optimized_data[mass_p1]*((0.5*gear_optimized_data[d_p1]*10**(-3))**2 - (0.5*gear_optimized_data[di_p1]*10**(-3))**2),
    J_g1 : 0.5*gear_optimized_data[mass_g1]*((0.5*gear_optimized_data[d_g1]*10**(-3))**2 - (0.5*gear_optimized_data[di_g1]*10**(-3))**2),
    J_p2 : 0.5*gear_optimized_data[mass_p2]*((0.5*gear_optimized_data[d_p2]*10**(-3))**2 - (0.5*gear_optimized_data[di_p2]*10**(-3))**2),
    J_g2 : 0.5*gear_optimized_data[mass_g2]*((0.5*gear_optimized_data[d_g2]*10**(-3))**2 - (0.5*gear_optimized_data[di_g2]*10**(-3))**2),
    J_m : 0.004
}

Jgb_eq_2 = sym.Eq(J_gb, (tau_2**2)*J_g2 + J_p1 + J_p2)
J2_eq_2 = sym.Eq(J_2,(tau_2**2)*(J_u + J_g1))
J1_eq_2 = sym.Eq(J_1,J_m)


# #### Characteristic (or Frequency) Equations: Fast configuration

# In[13]:


omega_nf_fast_1 = torsional_natural_frequencies[0][omega_nf].subs({J_gb : Jgb_eq_2.args[1], J_1 : J1_eq_2.args[1], J_2 : J2_eq_2.args[1]}).subs(mass_inertia).subs(equivalent_elastic_2).subs({tau_2 : gear_optimized_data[transmission_ratio_2]})
omega_nf_fast_2 = torsional_natural_frequencies[2][omega_nf].subs({J_gb : Jgb_eq_2.args[1], J_1 : J1_eq_2.args[1], J_2 : J2_eq_2.args[1]}).subs(mass_inertia).subs(equivalent_elastic_2).subs({tau_2 : gear_optimized_data[transmission_ratio_2]})
omega_nf_fast_3 = torsional_natural_frequencies[4][omega_nf].subs({J_gb : Jgb_eq_2.args[1], J_1 : J1_eq_2.args[1], J_2 : J2_eq_2.args[1]}).subs(mass_inertia).subs(equivalent_elastic_2).subs({tau_2 : gear_optimized_data[transmission_ratio_2]})


# In[14]:


# critical torsional speed
#NB : 1 Hz = 2π rad/s
f50 = 50*2*math.pi
f100 = 100*2*math.pi
f150 = 150*2*math.pi



# In[15]:


sym.plot((omega_nf_fast_2,(J_u,0,5)),
         (omega_nf_fast_3,(J_u,0,5)),
          xlabel = 'Ju',ylabel = '$\omega_{lt}$ [rad/sec]',title = 'Torsional vibration' )


# ### Modelling the equivalent shaft: Slow configuration

# #### Torsional stiffnesses

# In[16]:


K_user_1 = (gear_optimized_data[transmission_ratio_1]**2)*pow(pow(K1, -1) + pow(K2, -1),-1)
K_motor_1 = pow(pow(K4,-1) + pow(K5,-1),-1)

equivalent_elastic_1= {
    K_u : K_user_1,
    K_m : K_motor_1
}


# In[17]:


equivalent_elastic_1


# #### Moment of inertia

# In[18]:


Jgb_eq_1 = sym.Eq(J_gb, ((tau_1**2)*(J_g2 + J_g1)) + J_p1)
J2_eq_1 = sym.Eq(J_2,(tau_1**2)*(J_u))
J1_eq_1 = sym.Eq(J_1,J_m + J_p2)


# #### Characteristic (or Frequency) Equations: Slow configuration

# In[19]:


omega_nf_slow_1 = torsional_natural_frequencies[0][omega_nf].subs({J_gb : Jgb_eq_1.args[1], J_1 : J1_eq_1.args[1], J_2 : J2_eq_1.args[1]}).subs(mass_inertia).subs(equivalent_elastic_1).subs({tau_1 : gear_optimized_data[transmission_ratio_1]})
omega_nf_slow_2 = torsional_natural_frequencies[2][omega_nf].subs({J_gb : Jgb_eq_1.args[1], J_1 : J1_eq_1.args[1], J_2 : J2_eq_1.args[1]}).subs(mass_inertia).subs(equivalent_elastic_1).subs({tau_1 : gear_optimized_data[transmission_ratio_1]})
omega_nf_slow_3 = torsional_natural_frequencies[4][omega_nf].subs({J_gb : Jgb_eq_1.args[1], J_1 : J1_eq_1.args[1], J_2 : J2_eq_1.args[1]}).subs(mass_inertia).subs(equivalent_elastic_1).subs({tau_1 : gear_optimized_data[transmission_ratio_1]})


# In[20]:


sym.plot((omega_nf_slow_2,(J_u,0,5)),
         (omega_nf_slow_3,(J_u,0,5)),
          xlabel = 'Ju',ylabel = '$\omega_{lt}$ [rad/sec]',title = 'Torsional vibration' )

该情节应该看起来像第一个情节,但我没有任何输出。 我该做什么?可能是变量重叠而对sympy.plot函数造成了一些问题?

1 个答案:

答案 0 :(得分:0)

已解决:要绘制的函数未定义为零,因此我已经调整了代码部分:

sym.plot((omega_nf_slow_2,(J_u,0.001,5)),
         (omega_nf_slow_3,(J_u,0.001,5)),
          xlabel = 'Ju',ylabel = '$\omega_{lt}$ [rad/sec]',title = 'Torsional vibration' )