Clear Filters
Clear Filters

How to apply if statement with nonlinear inequality constraint with fmincon Optimization Toolbox

1 view (last 30 days)
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
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.
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

Accepted Answer

Walter Roberson
Walter Roberson on 29 Dec 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.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!