2011-11-07 10 views
8

나는 옥타브에서 생성 된 코드를 pylab에 이식하고 있습니다. 포팅 된 방정식 중 하나는 파이썬에서 옥타브에서와 다른 결과를 보여줍니다.같은 방정식, Pylab과 Octave의 다른 답변들

설명 할 수있는 가장 좋은 방법은 같은 방정식에서 옥타브와 필라브로 생성 된 플롯을 보여주는 것입니다.

다음은 원래 방정식을 옥타브로 단순화 한 것입니다. 소정의 테스트 스크립트를 제로로 유지 파이로 함수의 결과는 ~ (-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'으로 바뀌 었습니다. 세타의 동일한 범위는 제로에서 개최 파이로 꾸몄다되었습니다

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 result

나는 차이의 원인을 결정하지 못할, 어떤 아이디어?

+1

이러한 함수는 원래 함수의 _ 단순화 된 버전입니까? 와우. 한 번에 두 조각의 동일한 덩어리를 털어 내고 여전히 더 작은 것을 찾으려고 할 가능성이 있습니까? :) – sarnold

+0

함수의 복잡성을 감안할 때 다른 부동 소수점 정밀도 및/또는 반올림 오류가있을 수 있습니까? 원인을 좁히기 위해 함수의 작은 부분을 그려 보았습니까? –

+0

스택 오버 플로우 Gurus의 문제를 단순화하기 위해 모든 외부 코드가 제거되었다는 점에서 단순화되었습니다. –

답변

12

바닥 분할로 인한 오류를 제거하려면 from __future__ import division을 시도하십시오.

+0

Huzzah! 고맙습니다! 그걸 수정 한 것 같습니다. 밖을 내다 봐야 할 다른 수학 문제가 있습니까? –

+0

@Inverse_Jacobian :이 대답으로 문제가 해결되면 수락해야합니다 (체크 표시를 클릭하십시오). –