Doing an example find the optimal state and optimal control based on minimizing the performance index, Fmincon error of scalar value
1 view (last 30 days)
Show older comments
Edited: Kunal Jain on 18 Feb 2022
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.
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
function F = cost_function(x)
% global P_alpha_1;
function F = system_of_equations(x)
global init;function F = system_of_equations(x)
x1=('C1*P_alpha_1*m.H') + init(1);
% 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)) ) ;
k=8; %no. of Haar wavelets
b=1; %Total number of days to plot
% u=(C3*m.H) ;
% cost function=(1/2)*u.^2;
A = ;
bb = ;
Aeq = ;
beq = ;
lb = ;
ub = ;
fun = @cost_function;
x = fmincon(fun,x0,A,bb,Aeq,beq,lb,ub,nonlcon)
Catalytic on 17 Feb 2022
Edited: Catalytic on 17 Feb 2022
Do as the error message says. Test your cost function on its own and make sure it returns a scalar value before applying fmincon.
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!