2011-11-07 63 views
8

我將用octave創建的代碼移植到pylab中。其中一個移植的方程在python中的效果明顯不同於八度。同樣的等式,從Pylab和Octave得到的答案不同

要解釋的最好方法是顯示來自同一方程的八度和pylab生成的圖。

這裏是一個簡單的八度原始方程式的片段。在這個小測試腳本,與保持爲零披功能結果從〜(-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) 

產生的倍頻情節是這樣的:

Octave result

將'^'替換爲'**','acos'替換爲'arccos','asin'替換爲'arcsin',將同一個等式複製並從八度音符粘貼到python中。同樣的範圍theta的繪製與保持爲零披:

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 result

我不能確定是什麼原因造成的差異,任何想法?

+1

這些函數是原始函數的_simplified_版本?哇。你有可能一次只從兩件作品中剔除相同的塊,並試圖找到更小的東西? :) – sarnold

+0

鑑於函數的複雜性,它可能是不同的浮點精度和/或舍入誤差的問題嗎?您是否嘗試過繪製函數的較小部分來縮小原因? –

+0

它的簡化意味着所有無關代碼都被取出來簡化堆棧溢出大師的問題。 –

回答

12

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

+0

Huzzah!謝謝!似乎已經解決了它。有什麼其他的數學問題我應該留意? –

+0

@Inverse_Jacobian:如果這個答案解決了你的問題,你應該接受它(點擊它的複選標記)。 –

相關問題