Doing an example find the optimal state and optimal control based on minimizing the performance index, Fmincon error of scalar value

์กฐํšŒ ์ˆ˜: 3 (์ตœ๊ทผ 30์ผ)
Kunal Jain
Kunal Jain 2022๋…„ 2์›” 17์ผ
ํŽธ์ง‘: Kunal Jain 2022๋…„ 2์›” 18์ผ
Example to find the optimal state and optimal control based on minimizing the performance index. I have written the code but the error shown in fmincon is "Supplied objective function must return a scalar value." I have checked the matrices but still the error is there. I have attached te codes, kindly tell how to resolve the problem.
EXAMPLE:
Find the optimal state and optimal control based on minimizing the performance index ๐ฝ=โˆซ (๐‘ฅ(๐‘ก) โˆ’ 1/2 ๐‘ข(๐‘ก)^2 ) ๐‘‘๐‘ก , 0 โ‰ค ๐‘ก โ‰ค 1 subject to ๐‘ข(๐‘ก) = ๐‘ฅฬ‡(๐‘ก) + ๐‘ฅ(๐‘ก) with the condition ๐‘ฅ(0) = 0, ๐‘ฅ(1) = 1 2 (1 โˆ’ 1/๐‘’ )^ 2 where ๐ฝ๐‘’๐‘ฅ๐‘Ž๐‘๐‘ก = 0.08404562020 In this example the initial approximation is ๐‘ฅ1 (๐‘ก) = 1 2 (1 โˆ’ 1/๐‘’ ) ^2
CODES:
File1;
function F = cost_function(x)
global def;
global m;
% global P_alpha_1;
s=def.k ;
C3=x(1,(s+1):2*s);
;
u=(C3*m.H) ;
F=(x-(1/2)*(u*u'));
File 2:
function F = system_of_equations(x)
global def;
global m;
global init;function F = system_of_equations(x)
global def;
global m;
global init;
global P_alpha_1;
s=def.k ;
C1=x(1,1:1);
C3=x(1,(s+1):s);
x1=('C1*P_alpha_1*m.H') + init(1);
u=('C3*m.H') ;
D_alpha1_x1= 'C1*m.H';
M=Haar_matrix(s);
HC=M(:,s);
%%control law1
% F = horzcat( D_alpha1_x1 - u , ...
F = horzcat( D_alpha1_x1 - u , ...
('C1*P_alpha_1*HC') + init(1) - (1/2*((1-exp(-1))^2)) ) ;
end
File 3:
alpha_1=1;
k=8; %no. of Haar wavelets
b=1; %Total number of days to plot
initialize(alpha_1,k,b )
global m;
global init;
global def;
global P_alpha_1;
P_alpha_1=fractional_operation_matrix(k,alpha_1,b,m.H);
s=def.k;
% C3=x(1,((2*s)+1):3*s);
% u=(C3*m.H) ;
% cost function=(1/2)*u.^2;
x0=zeros(2,2*s);
% system_of_equations(x)
A = [];
bb = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
fun = @cost_function;
nonlcon=@system_of_equations;
x = fmincon(fun,x0,A,bb,Aeq,beq,lb,ub,nonlcon)
  ๋Œ“๊ธ€ ์ˆ˜: 2
Matt J
Matt J 2022๋…„ 2์›” 17์ผ
It would be easiest if you run the code here in the forum, rather than on your local computer. That way, we can all see what it's doing in the same environment.
Kunal Jain
Kunal Jain 2022๋…„ 2์›” 17์ผ
ํŽธ์ง‘: Kunal Jain 2022๋…„ 2์›” 18์ผ
@Matt J Thanks for answering my question.
I am not able to run the code here on community. It shows the no code when I click on Run button..
Please tell what should I do?

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

๋‹ต๋ณ€ (1๊ฐœ)

Catalytic
Catalytic 2022๋…„ 2์›” 17์ผ
ํŽธ์ง‘: Catalytic 2022๋…„ 2์›” 17์ผ
Do as the error message says. Test your cost function on its own and make sure it returns a scalar value before applying fmincon.
  ๋Œ“๊ธ€ ์ˆ˜: 1
Kunal Jain
Kunal Jain 2022๋…„ 2์›” 17์ผ
@Catalytic Thanks for responding my query.
I tried running the cost function alone and I am getting a value as :
F = cost_function(zeros(1,16))
F =
1.4018e+04
Can you tell then why I am still getting fmincon error?

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Introduction to Installation and Licensing์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

Community Treasure Hunt

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

Start Hunting!

Translated by