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

8 visualizaciones (últimos 30 días)
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)?

Respuesta aceptada

Matt J
Matt J el 7 de Oct. de 2021
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 comentario
Cheng Zhang
Cheng Zhang el 7 de Oct. de 2021
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Problem-Based Optimization Setup en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by