How to use fmincon for minimizing multiple variables
Show older comments
Hello, I'm a student of the Master Degree in Control Systems Engineering, and I have to study a scientific paper and create the matlab code for solving it and obtain the right results.
This scientific paper speaks about a problem of Optimal Control.
I send you the paper, in which there are all the informations.
I also send you the Matlab codes that I’m trying to do for solving this problem, but I think that my code it’s not good at the moment.
Thank you
main.m
% PARAMETERS CONFIGURATION
beta_0 = 0.292; %friction coefficient
beta_1 = 1.005;
beta_2 = 2.652*(10^(-4)); %ohmic losses coefficient
t_0 = 0; % initial time
t_f = 1080; % final time
tau = 5; % step time (discrete)
N = 216; % size of discrete interval
v_t0 = 70; % Initial velocity
v_tf = 70; % Final velocity
s_t0 = 0; % Initial position
s_tf = 21; % Final position
m = 15950; % mass
g = 9.81; % gravity force
c_r = 0.1; % rolling force coefficient
sigma_d = 3.1246; % drag coefficient
v_min = 60; % minimal velocity
v_max = 80; % maximal velocity
% initial values
S0 = s_t0;
V0 = v_t0;
A0 = 0;
% initial conditions
init = [ S0; V0; A0 ]*ones(1,N);
% boundaries
lb = [ -inf; v_min; -inf ]*ones(1,N);
ub = [ inf; v_max; inf ]*ones(1,N);
options = optimoptions('fmincon','MaxFunEvals',5000000,'MaxIterations', 1000000);
[opt_sol, fval, exitflag, output, lambda, grad, hessian] = fmincon(@opt, init, [], [], [], [], lb, ub, @constr, options) ;
% for i=1:1:N
% S(i+1) = S(i) + tau*V(i);
% V(i+1) = V(i) + tau*A(i);
constr.m
function [c, ceq] = constr(opt_sol)
N = 216; % size of discrete interval
s_t0 = 0; % Initial position
v_t0 = 70; % Initial velocity
s_tf = 21; % Final position
v_tf = 70; % Final velocity
S = opt_sol(1,1:N);
V = opt_sol(2,1:N);
c = [];
%initial values
ceq(1) = S(1) - s_t0;
ceq(2) = V(1) - v_t0;
% INSERIRE VINCOLI
% for i=1:1:N
%
% S(i+1) = S(i) + tau*V(i);
% V(i+1) = V(i) + tau*A(i);
% end
% final values
ceq(3) = S(N) - s_tf;
ceq(4) = V(N) - v_tf;
5 Comments
Riccardo Draghi
on 5 Feb 2021
Riccardo Draghi
on 5 Feb 2021
Walter Roberson
on 5 Feb 2021
S = zeros(1,N+1);
V = zeros(1,N+1);
A = zeros(1,N+1);
S(1) = 0 ;
V(1) = 70 ;
A(1) = 0 ;
S = opt_sol(1,1:N);
V = opt_sol(2,1:N);
A = opt_sol(3,1:N);
Why do you create S and V and A as all zeros and initialize one entry, and then overwrite all of S, V, and A?
for i=1:1:N
S(i+1) = S(i) + tau*V(i);
That overwrite S(2) before you have used the stored S(2) value, so you effectively ignore all of the passed-in values for S other than the very first. That would not seem to be wise. Likewise you overwrite V as well.
Riccardo Draghi
on 5 Feb 2021
Matt J
on 5 Feb 2021
Riccardo Draghi commented
Here's the paper.
Answers (0)
Categories
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!