I had problem while using the fmincon in for loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
x0(1,1)=-4;
x0(1,2)=4;
{for i=1:3 x0(i,1)=x0(1,1)+k*(1/18); x0(1,2)=x0(1,2)+k*(1/36);
energy=@(x)(K0+K1*((sin(x(1))*cos(x(2)))^2*(sin(x(1))*sin(x(2)))^2+(sin(x(1))*sin(x(2)))^2*(cos(x(1)))^2+(cos(x(1)))^2*(sin(x(1))*cos(x(2)))^2)) ;
A=[];b=[];Aeq=[];beq=[];
[x,fval]=fmincon(energy,x0,A,b,Aeq,beq,lb,ub)
m1=sin(x(1))*cos(x(2)) m2=sin(x(1))*sin(x(2)) m3=cos(x(1)) k=k+1; end}
But i want to run for different values of starting point so that i can get the different values of x at each time,but it shows the error
Length of lower bounds is < length(x); filling in missing lower bounds with -Inf. so how to rectify this error
0 comentarios
Respuestas (1)
Ayush
el 7 de Ag. de 2024
Editada: Ayush
el 7 de Ag. de 2024
Hi Manik,
The issue seems to be in the implementation. The error Length of lower bounds is < length(x) is because the size of "x0" and vectors "lb" and "ub" are different during the subsequent iterations.
I've solved it using two methods as decribed below:
Method-1: Instead of increasing the size of vector "x0" during different iterations, it can be made constant by using "x0" as 2x2 matrix.
x0(1) =-4;
x0(2) =4;
and in the loop, the corresponding change can be made:
x0(1) = -4 + k * (1/18);
x0(2) = 4 + k * (1/36);
This works because now the size of vector "x0" remains constant and is same as size of vectors "lb" and "ub".
Method-2: Increasing the size of vectors "lb" and "ub" during different iterations. The aim is to make size of vectors "lb", "ub" same as "x0".
% Define bounds
lb(1,1) = -Inf;
lb(1,2) = -Inf;
ub(1,1) = -Inf;
ub(1,2) = Inf;
and in the loop, the corresponding change can be made:
lb(i,1) = -Inf;
lb(i,2) = -Inf;
ub(i,1) = Inf;
ub(i,2) = Inf;
This works because now the size of vectors "lb" and "ub" remains constant and is same as size of vector "x0".
For futher information, you can refer to documentation of fmincon function: https://in.mathworks.com/help/optim/ug/fmincon.html
Hope it helps!!
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!