Matlab:将符号转换为函数句柄,返回的值不是double

时间:2014-07-30 15:25:48

标签: matlab symbolic-math

我有3个变量(g_eq)的一些模糊的长符号表达式,我希望通过一些约束来最小化。我试图将它转换为函数句柄并使用fmicon:

cfun = matlabFunction(g_eq,'vars',[kappa;theta;sigma]);
A = [-1 0 0; 0 -1 0; 0 0 -1];
b = [0; 0; 0];
[x,fval] = fmincon(@(kappa, theta, sigma) cfun, x0, A, b)

Matlab不喜欢:

  

FMINCON要求用户函数返回的所有值都是   数据类型为double。

我怀疑,问题在于cfun,因为它充满了具有符号精度的数字,我可以以某种方式转换它,以便它们加倍吗?或者更好(计算时间明智)在创建我的目标函数(cfun)(一些数据转换和参数模型的复杂过程)时,我可以使用符号或其他“代理变量”表达式的数字部分加倍?

谢谢, 学家

编辑 - MCVE: 我的目标是通过最小化一些加权区域上隐含的模型和数据隐含的拉普拉斯变换之间的差异来找到模型的参数。在这里,我提供了一个小区域的问题,而不使用区域上的权重和一些进一步的简化。在第0部分中,我提供了转换函数的代码,在第II部分中我进行了参数转换,而在III中则进行了数据转换并尝试在IV中将其最小化。

%% 0. Functions used

%% 0.1 L_V1 - transform of parametric
function lv = L_V1(u,sigma,kappa,theta)
lv = (1/(1+u.*sigma^2/(2*kappa))).^(2*kappa*theta/sigma^2);
end

%% 0.2 LV_2 - transform of data
function lv = L_hat1(u,D,n,T)
A_u = cos(sqrt(2 .*u) *sqrt(n) .*D);
Z_u = 1/n * sum(A_u);
lv = 1/T * sum(Z_u);
end


%% I. Pre-estimation

ulng1=100; %select number of points on the evaluated interval
u1 = linspace(.8, 1.6,ulng1); % create region of interest

%% II. Parametric part

par_mat1 = sym(zeros(ulng1,1)); % create interval for parametric

syms sigma kappa theta LV_par1;

for i = 1:ulng1
      par_mat1(i) = L_V1(u1(i),sigma,kappa,theta); %transformations of parametric

end

LV_par1 = sum(par_mat1); %sum of parametric over the region


%% III. Data part
n = 100; %choose number of days
T = 20;  %choose number of obs over a day
D = rand([n-1, T]); %vector of observations, here just random numbers

for i = 1:ulng1
    hat_mat1(i) = L_hat1(u1(i),D,n,T); %transformations of data
end

hat_1  = sum(hat_mat1); %sum of transforms over the region

%% IV. Minimize
W = 1; %weighting matrix, here just one region, hence 1
MC = hat_1 - LV_par1 ; %moment condition
g_eq = (MC) * (W) *(MC.'); %objective function (symbolic)
cfun = matlabFunction(g_eq,'vars',[kappa;theta;sigma]); %create a function handle from the symbolic

x0 = [.5; 1; .5];
A = [-1 0 0; 0 -1 0; 0 0 -1]; %constrains
b = [0; 0; 0]; %constrains
[x,fval] = fmincon(@(kappa, theta, sigma) cfun, x0, A, b) %minimize

1 个答案:

答案 0 :(得分:0)

优化参数始终作为向量传递。

[x,fval] = fmincon(@(x) cfun(x(1),x(2),x(3)), x0, A, b)