How to apply if statement with nonlinear inequality constraint with fmincon Optimization Toolbox
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Emily
el 29 de Dic. de 2023
Respondida: Walter Roberson
el 29 de Dic. de 2023
I'm attempting to minimize the sum of squares between experimental data and a model fit with fmincon.
The nonlinear inequality constraint I need to apply is that x(2) cannot decrease if the load is decreasing
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1578851/image.png)
where F is load, p is a model parameter x(2), dp is displacement, and t is time
I've tried modifying x0 (initial point), but there is no change in the goodness of fit.
Viewing the iterative display suggests that the nonlinear constraint is being considered, but based on the goodness of fit, it's clear there's an error.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1578856/image.png)
Appreciate any insight on this!
model_disp = @(x) sqrt(load)./x(1) + sqrt(load)./x(2) + exp(-(x(3).*time)./x(4))+sqrt(load)./x(3) + sqrt(load.*time./x(5)); %anonymous function
fun = @(x)sum((model_disp(x)-disp).^2); %objective function to be minimized
x0 = [1 1 1 1 1]; %initial
lb = [0 0 0 0 0]; %lower bounds
ub = [Inf Inf Inf Inf Inf]; %upper bounds
problem=createOptimProblem('fmincon','objective',fun,'x0',x0,'lb',lb,'ub',ub,'nonlcon',@(x)nonlconstraint(x,load));
[model_disp_params,fval,exitflag,output]=run(GlobalSearch,problem);
figure; plot(disp); hold on; plot(feval(model_disp,model_disp_params)); legend('Raw Data','Model')
function [c,ceq] = nonlconstraint(x,load)
for i = 1:length(load)-1
if load(i+1)>load(i)
c = sqrt(load(i+1))/x(2);
elseif load(i+1) < load(i)
c = sqrt(max(load))/x(2);
end
end
ceq=[]; %nonlinear equality constraints
end
0 comentarios
Respuesta aceptada
Walter Roberson
el 29 de Dic. de 2023
You need to assign to c(i) instead of to c
You fail to assign anything in the case that load(i+1) == load(i) which is a problem.
0 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!