I am having issues while using fmincon to solve optimization problem

I am having issue regarding the error: Error using fmincon
FMINCON requires all values returned by functions to be of data type double.
Here is my MATLAB code
% Vehicle has to travel 300m and in 20 seconds
% and the maximum speed limit on the road is 100kmph(~27.8 m/s)
% and the maximum acceleration of vehicle is 4m/s^2.
% The range of velocity is (0, 27.7) in m/s
% and the range of acceleration is (-4, 4) in m/s^2
% The vehicle stops at end (300m) and the final velocity is 0 m/s
% and the final acceleration is also 0 m/s^2.
% Find the velocity and the acceleration trajectory throughout the journey
% so that energy consumption is minimized (∫a*ds or v*dv)
% Define the parameters
d=300; % distance to be covered in metres
t_f=20; % final time in seconds
v_0=20; % intial velocity in m/s
v_f=0; % final velocity in m/s
a_0=3; % intial acceleration in m/s^2
a_f=0; % final acceleration in m/s^2
a_max=4; % maximum acceleration in m/s^2
a_min=-4; % minumum acceleration in m/s^2
v_max=27.7; % maximum velocity in m/s
v_min=0; % minimum velocity in m/s
dt=0.1; % time step
% Define arrays for the velocity, acceleration, and distance
t= 0:dt:t_f;
s=zeros(size(t));
v=zeros(size(t));
a=zeros(size(t));
ds=zeros(size(t));
% Define the intial conditions
s(1)=0;
v(1)=v_0;
a(1)=a_0;
ds(1)=0;
% Forming optimization problem
lb = [-4,-0.01];
ub = [4,50];
% Options
myopt = optimoptions('fmincon');
myopt.Display = 'iter';
% Function handle
cost = @(x) costFun(x, a, ds, t_f, dt);
constraint = @(x) constraintFun(s, v, a, v_f, a_f, d);
% Solve
x0=[4,0]; %Intial acceleration and velocity
x = fmincon(cost,x0,[],[],[],[],lb,ub,constraint,myopt);
function E = costFun(x, a, ds, t_f, dt)
% Define arrays for the velocity, and distance
v= zeros(size(a));
s=zeros(size(a));
v(1)=20; %Intialize the intial velocity
s(1)=0; %Intialize %the intial displacement
for i=2:((t_f/dt)+1)
%compute the velocity, acceleration and distance using 1-D equation of motion
a(i-1)=3;
v(i)=v(i-1)+a(i-1)*dt;
s(i)=s(i-1)+ v(i-1)*dt + (0.5*a(i-1)*dt*dt);
ds(i)=s(i)-s(i-1);
% Apply the constraints on the velocity and acceleration
if v(i) > 27.7
v(i) = 27.7;
end
if v(i) < 0
v(i) = 0;
end
if a(i) > 4
a(i) = 4;
end
if a(i) < -4
a(i) = -4;
end
end
% Create the cost function for energy consumption
E = double(sum(a.*ds));
end
% Create constraint function
function [cInEq,cEq] = constraintFun(s, v, a, v_f, a_f, d)
% A local function for the constraints
% Nonlinear inequality constraint: None
cInEq = [];
% Equality constraint
cEq = [s(end)==d; v(end)==v_f; a(end)==a_f];
end

1 Comment

I find it strange that neither your cost function nor your constraint function depend on the solution variables. Something must be wrong with your code. I fear: not only something.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Asked:

on 1 Mar 2023

Commented:

on 1 Mar 2023

Community Treasure Hunt

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

Start Hunting!