Getting error in fmincon with below code how to correct it

% Constants
m = 1; % mass of the rocket (kg)
x_target = 10; % target displacement (m)
t_final = 10; % final time (s)
% Objective function: integral of thrust squared
objective = @(F) integral(@(t) F(t).^2, 0, t_final);
% Initial guess for thrust profile (constant thrust)
%F0 = @(t) 0.0; % Initial guess for thrust profile is a zero vector
F0 = 0.0;
% Constraints
nonlcon = @(F) rocket_constraints(F, t_final, m, x_target);
% Solve optimization problem
options = optimoptions('fmincon', 'Display', 'iter');
F_opt = fmincon(objective, F0, [], [], [], [], [], [], nonlcon, options);
Array indices must be positive integers or logical values.

Error in solution>@(t)F(t).^2 (line 7)
objective = @(F) integral(@(t) F(t).^2, 0, t_final);

Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);

Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);

Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);

Error in integral (line 87)
Q = integralCalc(fun,a,b,opstruct);

Error in solution>@(F)integral(@(t)F(t).^2,0,t_final) (line 7)
objective = @(F) integral(@(t) F(t).^2, 0, t_final);

Error in fmincon (line 563)
initVals.f = feval(funfcn{3},X,varargin{:});

Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.

回答 (2 件)

Matt J
Matt J 2024 年 2 月 19 日
編集済み: Matt J 2024 年 2 月 19 日

0 投票

You seem to be trying to minimize over a space of functionals, F. You cannot do that with fmincon. The objective must be a function of an unknown parameter vector in and the initial point x0 must be a vector in

4 件のコメント

Charuka
Charuka 2024 年 2 月 19 日
Objective function defined as below and changing F0 for variable gives error as well
objective = @(F) integral(@(t) F(t).^2, 0, t_final);
F0 = @(t) 0.0;
Matt J
Matt J 2024 年 2 月 19 日
As well it should. Again, you cannot minimize over function spaces.
Walter Roberson
Walter Roberson 2024 年 2 月 19 日
fmincon() passes a vector the length of F0 to the objective function.
Your objective function takes that vector and tries to run an integration, with the integration trying to index the vector.
Charuka
Charuka 2024 年 2 月 20 日
Thanks I will relook in to it

サインインしてコメントする。

Torsten
Torsten 2024 年 2 月 19 日
移動済み: Torsten 2024 年 2 月 19 日

0 投票

If you define F0 = 0, "fmincon" will use one scalar value as input F to "objective" and "nonlcon". Thus the use of F as a function handle or function doesn't make sense.

3 件のコメント

Charuka
Charuka 2024 年 2 月 19 日
Changing F0 doesnt solve the problem as well, Tried doing this
% Constants
m = 1; % mass of the rocket (kg)
x_target = 10; % target displacement (m)
t_final = 10; % final time (s)
% Objective function: integral of thrust squared
objective = @(F) integral(@(t) F(t).^2, 0, t_final);
% Initial guess for thrust profile (constant thrust)
F0 = @(t) 0.0; % Initial guess for thrust profile is a zero vector
% Constraints
nonlcon = @(F) rocket_constraints(F, t_final, m, x_target);
% Solve optimization problem
options = optimoptions('fmincon', 'Display', 'iter');
F_opt = fmincon(objective, F0, [], [], [], [], [], [], nonlcon, options);
Torsten
Torsten 2024 年 2 月 19 日
編集済み: Torsten 2024 年 2 月 19 日
The inputs to "objective" and "nonlcon" must be vectors of parameters to be optimized, not functions or function handles.
So you will have to parametrize your function to be integrated and optimize these parameters.
If you don't know how to do that, I suggest you license TOMS software which focusses on solving optimal control problems with MATLAB:
Charuka
Charuka 2024 年 2 月 20 日
Thank you, I will try it

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeOptimization についてさらに検索

製品

リリース

R2023b

タグ

質問済み:

2024 年 2 月 19 日

コメント済み:

2024 年 2 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by