Optimise multiple variables with "Division by an OptimizationVariable not supported"
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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)?
0 commentaires
Réponse acceptée
Matt J
le 7 Oct 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);
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!