简体   繁体   English

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

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

I am porting code created in octave into pylab. 我将以八度为单位创建的代码移植到pylab中。 One of the ported equations gives dramatically different results in python than it does in octave. 其中一个移植方程在python中产生的结果与在八度音程中产生的结果截然不同。

The best way to explain is to show plots generated by octave and pylab from the same equation. 解释的最佳方式是显示由八度和pylab从同一方程生成的图。

Here is a simplified snippet of the original equation in octave. 这是八度音阶中原始方程的简化片段。 In this small test script, the result of function with phi held at zero is plotted from ~ (-pi,pi): 在这个小测试脚本中,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)

The resulting octave plot looks like this: 由此产生的八度音阶图如下所示:

八度结果

The same equation was copied and pasted from octave into python with '^' replaced with '**', 'acos' replaced with 'arccos', and 'asin' replaced with 'arcsin'. 将相同的等式从八度音阶复制并粘贴到python中,其中'^'替换为'**','acos'替换为'arccos','asin'替换为'arcsin'。 The same range of theta was plotted with phi held at zero: 绘制相同范围的θ,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's result looks like this: Python的结果如下所示: Python结果

I cant determine what is causing the differences, Any ideas? 我无法确定导致差异的原因,任何想法?

尝试from __future__ import division来消除楼层划分产生的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM