更新Octave的fsolve调用的函数中的参数

时间:2016-02-27 05:39:56

标签: octave solver

我正在研究Octave中单个驱动腿的运动建模。腿部有3个点:一个静止的臀部(A点),一个沿着已知路径移动的脚(B点),以及一个膝盖(C点),其位置和角度我试图解决。

使用下面的代码,我可以成功解决膝关节的XYZ位置以及参数s0Theta_H的单个值的相关角度。

现在,我希望能够遍历多个s0Theta_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

1 个答案:

答案 0 :(得分:1)

你绝对可以做到! 您需要做的就是创建一个返回函数的函数。

首先让您的Rug_Bug_Leg函数以s0Theta_H作为输入:

function y = Rug_Bug_Leg(x, s0, Theta_H)

% ...

endfunction

然后,您可以在Rug_Bug_Leg周围写一个“包装”函数,如下所示:

rbl = @(s0, Theta_H) @(x) Rug_Bug_Leg(x, s0, Theta_H)

现在,如果您使用某些值rbl致电(s0,Theta_H),则会返回函数,其中x为输入并返回Rug_Bug_Leg(x,s0,Theta_H) 。 例如,rbl(0,0)返回函数:

@(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