How to solve a nonlinear optimization problem using fmincon
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to use fmincon in order to solve a nonlinear optimization problem. my code is supposed to take 40 variables that are in "x" and find the minimal potential energy under the given constraints.
Objective function:
function E=Objective_Function(x)
global K
psi=X_psi(x);th=X_th(x);phi=X_phi(x);dz=X_dz(x); %These functions take the relevant data from x into a new variable.
Kx=K(1); Ky=K(2); Kz=K(3); Kd=K(4);
E = (1/2)*(Kx*sqsum(psi)+Ky*sqsum(th)+Kz*sqsum(phi)+Kd*sqsum(dz));
end
Constraints
function [c,ceq]=constraints(x)
global r_0 r_n X0 Xf l
dz=X_dz(x);
[A,W]=AW(x); %Generally these are multiplications of rotation matrices. so I have a lot of sine and cosine functions (and therefore, the constraints are non-linear).
n=size(W,3);
Sum=0;
for i=1:(n-1)
Sum=Sum+(W(:,:,i)'*[0;0;l+dz(i)]);
end
PA=W(:,:,n-1);
c=[];
temp=PA-((Aj(Xf(2),Xf(1),Xf(3)))*((Aj(X0(2),X0(1),X0(3)))'));
temp2=Sum-(r_n-r_0);
ceq=[temp,temp2];
end
Main Function:
clear all
close all
clc
%clearvars -except x0
global K r_0 r_n X0 Xf l
K=[10,10,10,1/2];
n=10;
g1=(30/180)*pi; a1=0;
l=100; r1=100 ;R1=50 ;h1=1100 ;
ph_0=0; th_0=0; ps_0=0;
ph_n=0; th_n=0; ps_n=g1-a1;
X0=[ps_0;th_0;ph_0]; Xf=[ps_n;th_n;ph_n];
r_0=[0;-r1;0]; r_n=[R1*sin(g1-a1);-R1*cos(g1-a1);h1];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
x0=rand(4*n,1);
fun=@Objective_Function;
nonlcon=@constraints;
options=optimoptions('fmincon','MaxFunEvals',5000);%,'Algorithm','sqp','disp','iter','TolFun',.001);%,'MaxIter',5000,'MaxFunEvals',5000);
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
I tried to run the code with the default options and with the SQP algorithm.
Default: I get a lot of singularities warnnings and the program stops at 3000 fun evals. The results are close to the ones that I have excpected although the accuracy is not enough for me. I have tried to increase the default value of MaxFunEval but the code ran for the whole day and didn't finish. SQP: I got no warnnings at all. the program converged to an infeasible point. the results were no good at all.
I think i'm not using FMINCON in the correct way and would love to get directions in order to correct my code.
Thanks a lot!
Liron.
0 commentaires
Réponses (1)
Alan Weiss
le 3 Avr 2018
The only thing that occurs to me is that you have no bounds on your variables. This is usually a mistake, especially for a problem that has a difficult set of nonlinear equalities to satisfy. I would imagine that the 'sqp' algorithm goes wandering off to an entirely unreasonable set of parameters.
The closer you can bound your solution, the faster and more reliably fmincon will operate.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
0 commentaires
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display 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!