簡體   English   中英

相同的等式,來自Pylab和Octave的不同答案

[英]Same equation, different answers from Pylab and Octave

我將以八度為單位創建的代碼移植到pylab中。 其中一個移植方程在python中產生的結果與在八度音程中產生的結果截然不同。

解釋的最佳方式是顯示由八度和pylab從同一方程生成的圖。

這是八度音階中原始方程的簡化片段。 在這個小測試腳本中,phi保持為零的函數結果從〜( - pi,pi)繪制:

clear
clc
close all

L1 = 4.25; % left servo arm length
L2 = 5.75; % left linkage length
L3 = 5.75; % right linkage length
L4 = 4.25; % right servo arm length
L5 = 11/2; % distance from origin to left servo
L6 = 11/2; % distance from origin to right servo

theta_array = [-pi+0.1:0.01:pi-0.1];
phi = 0/180*pi;

for i = 1 : length(theta_array)

theta = theta_array(i);

A(i) = -L3*(-((2*cos(theta)*L1*(sin(phi)*L4-sin(theta)*L1)-2*sin(theta)*L1*(L6+L5-cos(phi)*L4-cos(theta)*L1))/(2*L3*sqrt((L6+L5-cos(phi)*L4-cos(theta)*L1)^2+(sin(phi)*L4-sin(theta)*L1)^2))-((2*sin(theta)*L1*(L6+L5-cos(phi)*L4-cos(theta)*L1)-2*cos(theta)*L1*(sin(phi)*L4-sin(theta)*L1))*(-(L6+L5-cos(phi)*L4-cos(theta)*L1)^2-(sin(phi)*L4-sin(theta)*L1)^2-L3^2+L2^2))/(4*L3*((L6+L5-cos(phi)*L4-cos(theta)*L1)^2+(sin(phi)*L4-sin(theta)*L1)^2)^(3/2)))/sqrt(1-(-(L6+L5-cos(phi)*L4-cos(theta)*L1)^2-(sin(phi)*L4-sin(theta)*L1)^2-L3^2+L2^2)^2/(4*L3^2*((L6+L5-cos(phi)*L4-cos(theta)*L1)^2+(sin(phi)*L4-sin(theta)*L1)^2)))-((cos(theta)*L1)/sqrt((L6+L5-cos(phi)*L4-cos(theta)*L1)^2+(sin(phi)*L4-sin(theta)*L1)^2)-((sin(theta)*L1-sin(phi)*L4)*(2*sin(theta)*L1*(L6+L5-cos(phi)*L4-cos(theta)*L1)-2*cos(theta)*L1*(sin(phi)*L4-sin(theta)*L1)))/(2*((L6+L5-cos(phi)*L4-cos(theta)*L1)^2+(sin(phi)*L4-sin(theta)*L1)^2)^(3/2)))/sqrt(1-(sin(theta)*L1-sin(phi)*L4)^2/((L6+L5-cos(phi)*L4-cos(theta)*L1)^2+(sin(phi)*L4-sin(theta)*L1)^2)))*sin(acos((-(L6+L5-cos(phi)*L4-cos(theta)*L1)^2-(sin(phi)*L4-sin(theta)*L1)^2-L3^2+L2^2)/(2*L3*sqrt((L6+L5-cos(phi)*L4-cos(theta)*L1)^2+(sin(phi)*L4-sin(theta)*L1)^2)))-asin((sin(theta)*L1-sin(phi)*L4)/sqrt((L6+L5-cos(phi)*L4-cos(theta)*L1)^2+(sin(phi)*L4-sin(theta)*L1)^2)));

end

plot(theta_array,A)

由此產生的八度音階圖如下所示:

八度結果

將相同的等式從八度音階復制並粘貼到python中,其中'^'替換為'**','acos'替換為'arccos','asin'替換為'arcsin'。 繪制相同范圍的θ,phi保持為零:

from pylab import *

# physical setup
L1 = 4.25; # left servo arm length
L2 = 5.75; # left linkage length
L3 = 5.75; # right linkage length
L4 = 4.25; # right servo arm length
L5 = 11.0/2.0; # distance from origin to left servo
L6 = 11.0/2.0; # distance from origin to right servo

theta = arange(-pi+0.1,pi-0.1,0.01);
phi = 0/180.0*pi

def func(theta,phi):

A = -L3*(-((2*cos(theta)*L1*(sin(phi)*L4-sin(theta)*L1)-2*sin(theta)*L1*(L6+L5-cos(phi)*L4-cos(theta)*L1))/(2*L3*sqrt((L6+L5-cos(phi)*L4-cos(theta)*L1)**2+(sin(phi)*L4-sin(theta)*L1)**2))-((2*sin(theta)*L1*(L6+L5-cos(phi)*L4-cos(theta)*L1)-2*cos(theta)*L1*(sin(phi)*L4-sin(theta)*L1))*(-(L6+L5-cos(phi)*L4-cos(theta)*L1)**2-(sin(phi)*L4-sin(theta)*L1)**2-L3**2+L2**2))/(4*L3*((L6+L5-cos(phi)*L4-cos(theta)*L1)**2+(sin(phi)*L4-sin(theta)*L1)**2)**(3/2)))/sqrt(1-(-(L6+L5-cos(phi)*L4-cos(theta)*L1)**2-(sin(phi)*L4-sin(theta)*L1)**2-L3**2+L2**2)**2/(4*L3**2*((L6+L5-cos(phi)*L4-cos(theta)*L1)**2+(sin(phi)*L4-sin(theta)*L1)**2)))-((cos(theta)*L1)/sqrt((L6+L5-cos(phi)*L4-cos(theta)*L1)**2+(sin((phi)*L4-sin(theta)*L1)**2)-((sin(theta)*L1-sin(phi)*L4)*(2*sin(theta)*L1*(L6+L5-cos(phi)*L4-cos(theta)*L1)-2*cos(theta)*L1*(sin(phi)*L4-sin(theta)*L1)))/(2*((L6+L5-cos(phi)*L4-cos(theta)*L1)**2+(sin(phi)*L4-sin(theta)*L1)**2)**(3/2)))/sqrt(1-(sin(theta)*L1-sin(phi)*L4)**2/((L6+L5-cos(phi)*L4-cos(theta)*L1)**2+(sin(phi)*L4-sin(theta)*L1)**2)))*sin(arccos((-(L6+L5-cos(phi)*L4-cos(theta)*L1)**2-(sin(phi)*L4-sin(theta)*L1)**2-L3**2+L2**2)/(2*L3*sqrt((L6+L5-cos(phi)*L4-cos(theta)*L1)**2+(sin(phi)*L4-sin(theta)*L1)**2)))-arcsin((sin(theta)*L1-sin(phi)*L4)/sqrt((L6+L5-cos(phi)*L4-cos(theta)*L1)**2+(sin(phi)*L4-sin(theta)*L1)**2)))

return A

f = figure();
a = f.add_subplot(111);

a.plot(theta,func(theta,phi))

ginput(1, timeout=-1); # wait for user to click so we dont lose the plot

Python的結果如下所示: Python結果

我無法確定導致差異的原因,任何想法?

嘗試from __future__ import division來消除樓層划分產生的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM