I had problem while using the fmincon in for loop
조회 수: 11 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Ayush
2024년 8월 7일
편집: Ayush
2024년 8월 7일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!