Optimise multiple variables with "Division by an OptimizationVariable not supported"

1 回表示 (過去 30 日間)
I try to fit several unknown constants to match the experimental data.
% experimental data
c = [0.05; 0.1; 0.2; 0.4; 1];
obs = [3.947; 4.314; 4.171; 4.149; 4.109];
% Create a 3-D optimization variable named 'k'.
k = optimvar('k',1,2,3);
% To obtain the objective function
c_plus = (-k(1) + ((k(1).^2)-8*(-c*k(1))).^0.5)/4;
D = (c - c_plus)/2;
est = k(2)*c_plus/(1+D/k(3)); % this line causes the problem
% Create the objective function
obj = sum((obs - est).^2);
% Create an optimization problem named prob having obj as the objective function.
prob = optimproblem('Objective',obj);
% Solve Problem
k0.k = [1.48 5E-4 4.01E-08];
[sol,fval,exitflag,output] = solve(prob,k0);
But this line causes problem:
est = k(2)*c_plus/(1+D/k(3));
The error is this, but there is no other way to re-write my formula.
Error using /
Division of an OptimizationVariable by nonscalar not supported.
It seems that I should use fcn2optimexpr to convert my problem? But I could not follow the example here.
Or, should I use another way to find the fitted k(1), k(2) and k(3)?

採用された回答

Matt J
Matt J 2021 年 10 月 7 日
c = [0.05; 0.1; 0.2; 0.4; 1];
obs = [3.947; 4.314; 4.171; 4.149; 4.109];
% Create a 3-D optimization variable named 'k'.
k = optimvar('k',3);
% Create an optimization problem named prob having obj as the objective function.
fun=@(k) func(k,c,obs);
prob = optimproblem('Objective',fcn2optimexpr(fun,k));
% Solve Problem
k0.k = [1.48 5E-4 4.01E-08];
[sol,fval,exitflag,output] = solve(prob,k0);
Solving problem using fminunc. Solver stopped prematurely. fminunc stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 3.000000e+02.
function obj=func(k,c,obs)
% To obtain the objective function
c_plus = (-k(1) + ((k(1).^2)-8*(-c*k(1))).^0.5)/4;
D = (c(:) - c_plus(:))/2;
est = k(2)*c_plus./(1+D./k(3)); % this line causes the problem
% Create the objective function
obj = sum((obs - est).^2);
end
  1 件のコメント
Cheng Zhang
Cheng Zhang 2021 年 10 月 7 日
Thank you very much. The code can work. It seems that the code cannot run without all the code, which is a bit not intuitive.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by