更新由Octave';调用的函数中的参数;斯夫索尔夫酒店

更新由Octave';调用的函数中的参数;斯夫索尔夫酒店,octave,solver,Octave,Solver,我正致力于在倍频程中模拟单驱动腿的运动。腿有3个点:一个固定的臀部(点a),一个沿着已知路径移动的脚(点B),和一个膝盖(点C),我正试图求解它的位置和角度 使用下面的代码,我可以成功地求解膝盖的XYZ位置和单个参数值s0和Theta_H的相关角度 现在,我希望能够循环通过多个s0和Theta_H值并运行解算器。我的问题是,我不知道如何将这些变量的新值传递到等式函数中 这很棘手的原因是,使用Octave的fsolve所需的函数格式会阻止在函数中输入未知量以外的输入。我曾尝试将全局变量更新为索引器

我正致力于在倍频程中模拟单驱动腿的运动。腿有3个点:一个固定的臀部(点a),一个沿着已知路径移动的脚(点B),和一个膝盖(点C),我正试图求解它的位置和角度

使用下面的代码,我可以成功地求解膝盖的XYZ位置和单个参数值
s0
Theta_H
的相关角度

现在,我希望能够循环通过多个
s0
Theta_H
值并运行解算器。我的问题是,我不知道如何将这些变量的新值传递到等式函数中

这很棘手的原因是,使用Octave的
fsolve
所需的函数格式会阻止在函数中输入未知量以外的输入。我曾尝试将全局变量更新为索引器,但要做到这一点,我需要清除所有导致其他问题的工作区变量

如果您想知道如何更新此函数中的参数,同时仍能将其输入到
fsolve
中,我们将不胜感激

下面的代码调用解算器:

global AC = 150; % length of the thigh limb
global CB = 150; % length of the shin limb
global lspan = 75; % width span of the foot touch down wrt the hip
global bob = 10; % height of the hip joint off the ground during a step

inits = [ .75; 2.35; 37; 0; 125]; % initial guesses at horizontal step position

% x(1): hip joint - guessing a 45 deg (.75 rad) angle for hip joint
% x(2): knee joint - guessing a 135 deg (2.35 rad) angle (wrt to vert) 
% x(3): X position of the knee joint - guessing middle of the leg span in mm
% x(4): Y position of the knee joint - know it is 0 mm at the horizontal step position
% x(5): Z position of the knee joint - guessing the height to be ~80% of the height of a limb 

[x, fval, info] = fsolve(@Rug_Bug_Leg, inits); % when running fsolve for the first time often have to remove the output suppress
下面的代码显示了包含要通过Octave的
fsolve
函数求解的方程组的函数:

function y = Rug_Bug_Leg(x)

global AC; 
global CB; 
global lspan; 
global bob; 

s0 = 0; % fore/aft (Y) position of the foot during the step. Trying to iterate this
Theta_H = 0; % hip angle during the step. Trying to iterate this

y = zeros(6,1); % zeros for left side of each equation

% First set of equations, Joint C wrt to Joint A
y(1) = -1*x(3)+AC*sin(x(1))*cos(Theta_H);
y(2) = -1*x(4)+AC*sin(x(1))*sin(Theta_H);
y(3) = -1*bob - x(5)+AC*cos(x(1));

% Second set of equations, Joint B wrt to Joint C
y(4) = x(3)-lspan +CB*sin(x(2))*cos(Theta_H);
y(5) = x(4) - s0 +sin(x(2))*sin(Theta_H);
y(6) = x(5) + bob + CB*cos(x(2));
end function
@(x) Rug_Bug_Leg(x,0,0)

你绝对可以做到! 您只需创建一个返回函数的函数

首先让您的
Rug\u Bug\u Leg
功能将
s0
Theta\u H
作为输入:

function y = Rug_Bug_Leg(x, s0, Theta_H)

% ...

endfunction
然后,您可以围绕
Rug\u Bug\u Leg
编写一个“包装器”函数,如下所示:

rbl = @(s0, Theta_H) @(x) Rug_Bug_Leg(x, s0, Theta_H)
现在,如果您使用一些值调用
rbl
(s0,θH),它将返回一个函数,该函数将
x
作为输入,并返回
Rug\u Bug\u Leg(x,s0,θH)
。 例如,
rbl(0,0)
返回函数:

function y = Rug_Bug_Leg(x)

global AC; 
global CB; 
global lspan; 
global bob; 

s0 = 0; % fore/aft (Y) position of the foot during the step. Trying to iterate this
Theta_H = 0; % hip angle during the step. Trying to iterate this

y = zeros(6,1); % zeros for left side of each equation

% First set of equations, Joint C wrt to Joint A
y(1) = -1*x(3)+AC*sin(x(1))*cos(Theta_H);
y(2) = -1*x(4)+AC*sin(x(1))*sin(Theta_H);
y(3) = -1*bob - x(5)+AC*cos(x(1));

% Second set of equations, Joint B wrt to Joint C
y(4) = x(3)-lspan +CB*sin(x(2))*cos(Theta_H);
y(5) = x(4) - s0 +sin(x(2))*sin(Theta_H);
y(6) = x(5) + bob + CB*cos(x(2));
end function
@(x) Rug_Bug_Leg(x,0,0)
下面是一个示例用法:

for s0=1:10
    for Theta_H=1:10
        [x, fval, info] = fsolve( rbl(s0,Theta_H), inits );
    endfor
endfor

谢谢@jadhachem包装器效果很好!对于任何不熟悉八度包装函数的人,此链接提供了更多背景信息: