gpt4 book ai didi

python - fsolve on python(将matlab代码转换为python代码)

转载 作者:太空狗 更新时间:2023-10-30 01:21:41 24 4
gpt4 key购买 nike

我在 matlab 上有一个程序,但我想在 python 中转换,我的 matlab 代码是:

function [f]=equation1(x)
%options = optimoptions('fsolve','TolFun',1.1e-20,'TolX',1.1e-20,'maxIter',Inf,'MaxFunEvals',Inf);
c = 330 ;%speed of sound meters per second
% unknown variable :
% r1 => x(1)
% theta => x(2)

time2 = 0.455; % Microphone 1 to Microphone 2 time delay
time3 = 0.606; % Microphone 1 to Microphone 3 time delay
% gives :
r2 = 150 %time2*c;
r3 = 200 %time3*c;

r4 = 499.1; % Distance from Microphone 2 to Microphone 3
r5 = 1267.9;% Distance from Microphone 1 to Microphone 3

phi = 16.177; % Angle between Microphone 1 and Microphone 2 measured from Microphone 3
%
f(1)= (x(1) + r2)^2- (x(1)+r3)^2 -r4^2 +2*(x(1)+r3)*r4*cosd(x(2));
f(2)= x(1)^2 - (x(1)+r3)^2 - r5^2 + 2*(x(1)+r3)*r5*cosd(x(2)-phi);

我用 [x,feval]=fsolve(@equation1,[100 10]) 播放这段代码;他返回物有所值 ( 1620.7 076.4)但是在 python 上我已经制作了这个程序

from scipy.optimize import fsolve
import math

def cosd(x):
return math.cos(x * math.pi / 180);


def equations(p):
time2 = 0.455 # Microphone 1 to Microphone 2 time delay
time3 = 0.606 # Microphone 1 to Microphone 3 time delay
# gives :
r2 = 150. #time2*c;
r3 = 200. #time3*c;

r4 = 499.1 # Distance from Microphone 2 to Microphone 3
r5 = 1267.9 # Distance from Microphone 1 to Microphone 3

phi = 16.177 # Angle between Microphone 1 and Microphone 2 measured from Microphone 3
r1, theta = p
f1 = (r1 + r2)**2. -(r1+r3)**2 -r4**2 +2*(r1+r3)*r4*(cosd(theta))
f2 = r1**2 - (r1+r3)**2 - r5**2 + 2*(theta+r3)*r5*(cosd(theta-phi))
return (f1,f2)

x, y = fsolve(equations, (1000.0, 20.0), xtol=1e-06, maxfev=10000000)

print x,y, equations((x, y))

但是当我运行时,我有一个错误的错误值:

迭代没有取得很好的进展,通过 从最近五次雅可比评估中得到改进。 warnings.warn(消息,RuntimeWarning)(-167792.68841940007, -1096453.7938717711)

有人说为什么它在 python 上不起作用吗?

编辑 => 我更改了学位问题的 python 代码。但是我没有好的值(value),(python返回-4263.77780373 -272.257364385,它与matlab的值(value)相去甚远)。

最佳答案

我认为问题在于这个减法:

(theta-phi)

在 matlab 中,您要减去度数,但在 python 中,theta 的单位是 dregrees,而 phi 的单位是弧度。

关于python - fsolve on python(将matlab代码转换为python代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29767402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com