【问题标题】:Python | The Plot I expected don't show up蟒蛇 |我期待的情节没有出现
【发布时间】:2020-08-24 17:01:43
【问题描述】:

我以为是这张。

但是..我每次尝试都只得到这个空的。

我不知道为什么缺少这条线..以及我缺少什么..

import math
from matplotlib import pyplot as plt
import numpy as np

dx=42; dy=-17; ab=10.5;  bc=42;  cd=24; be=65; ef=5;
ml = np.arange(0, 2*math.pi, math.pi/180)

for theta in ml:
    alpha = -90*math.pi/180
    P=2*cd*(dx-ab+math.cos(theta))
    Q=2*cd*(dy-ab+math.sin(theta))
    R=(dx**2)+(dy**2)+(ab**2)+(cd**2)-(bc**2)-(2*ab+(math.cos(theta))+dy+(math.sin(theta)))

    math_cos_phi=((-P*R)-(Q*math.sqrt((P**2)+(Q**2)-(R**2))))/((P**2)+(Q**2));
    math_sin_phi=((-Q*R)+(P*math.sqrt((P**2)+(Q**2)-(R**2))))/((P**2)+(Q**2));
    a = math_cos_phi;  b = math_sin_phi
    phi=math.atan2(a,b);

    math.cos_psi=((dx+cd+(math_cos_phi))-(ab+math.cos(theta)))/bc;
    math.sin_psi=((dy+cd+(math_sin_phi))-(ab+math.sin(theta)))/bc;
    c = math.cos_psi;  d = math.sin_psi;

    psi=math.atan2(c,d); 
    Fx=ab*math.cos(theta)+be*math.cos(psi)+ef*math.cos(psi+alpha);
    Fy=ab*math.sin(theta)+be*math.sin(psi)+ef*math.sin(psi+alpha);
    plt.plot(Fx,Fy, c = 'g', linewidth=2);
plt.show()

Data=[theta*180/math.pi,Fx,Fy];

【问题讨论】:

  • 为什么matlab被标记在这里?
  • FxFy 是标量,这是您的意图吗? Fx 范围从 12.56... 到 -8.45... - 这与您想要的情节不符。
  • 我用 Matlab 尝试了这个问题,它给了我第一张图片..

标签: python matlab numpy matplotlib math


【解决方案1】:

你是这个意思吗? (每个点都有循环内的情节。您需要将其移至循环外并累积所有 Fx 和 Fy):

import math
from matplotlib import pyplot as plt
import numpy as np

dx=42; dy=-17; ab=10.5;  bc=42;  cd=24; be=65; ef=5;
ml = np.arange(0, 2*math.pi, math.pi/180)
Fx, Fy = [], []

for theta in ml:
    alpha = -90*math.pi/180
    P=2*cd*(dx-ab+math.cos(theta))
    Q=2*cd*(dy-ab+math.sin(theta))
    R=(dx**2)+(dy**2)+(ab**2)+(cd**2)-(bc**2)-(2*ab+(math.cos(theta))+dy+(math.sin(theta)))

    math_cos_phi=((-P*R)-(Q*math.sqrt((P**2)+(Q**2)-(R**2))))/((P**2)+(Q**2));
    math_sin_phi=((-Q*R)+(P*math.sqrt((P**2)+(Q**2)-(R**2))))/((P**2)+(Q**2));
    a = math_cos_phi;  b = math_sin_phi
    phi=math.atan2(a,b);

    math.cos_psi=((dx+cd+(math_cos_phi))-(ab+math.cos(theta)))/bc;
    math.sin_psi=((dy+cd+(math_sin_phi))-(ab+math.sin(theta)))/bc;
    c = math.cos_psi;  d = math.sin_psi;

    psi=math.atan2(c,d); 
    Fx.append(ab*math.cos(theta)+be*math.cos(psi)+ef*math.cos(psi+alpha))
    Fy.append(ab*math.sin(theta)+be*math.sin(psi)+ef*math.sin(psi+alpha))
plt.plot(Fx,Fy, c = 'g', linewidth=2);
plt.show()

Data=[theta*180/math.pi,Fx,Fy];

输出:

【讨论】:

  • you need to move it to outside of the loop): - 并累积所有 Fx 和 Fy ...
  • @wwii 感谢您的额外说明。也将您的台词添加到帖子中。
猜你喜欢
  • 1970-01-01
  • 2019-01-08
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 2010-11-03
  • 2018-03-14
  • 2017-12-30
  • 1970-01-01
相关资源
最近更新 更多