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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get 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