optimization using problem based

Hello!
i want to optimize the varibles so that i get the maximum valus of my function
i have 4 varibles (for now):
delta = optimvar('delta',1);
eta = optimvar('eta',1);
alpha = optimvar('alpha',1);
t = optimvar('t',1,'LowerBound',0,'UpperBound',Time);
and this is my constrains:
T = alpha*t^2+eta*t;
T_dot = 2*alpha*t+eta;
omega = delta*t;
%constrains
cons1 = T <= T_max;
cons2 = T >= T_min;
cons3 = omega <= omega_max;
cons4 = omega >= omega_min;
cons5 = T_dot <= T_dot_max;
cons6 = T_dot >= T_dot_min;
my questions
  1. how do i make my temperature profile (T) to be lower that T_max and bigger than T_min for every value of the time (0<t<Time)?
  2. i know i cant do it now (cause this is not a full second order profile), but if T was:
T = alpha*t^2+eta*t+const;
how can i make the sulution start from T_min? something like initial condition so that T(0) = T_min
or: how can i make the solution go trough every value between T_min and T_max?
Thank u all!
this is the full code i'm using
%% General
clc;
clear;
close;
fontsize = {'Fontsize',14};
linewidth ={'Linewidth', 1.5};
legendfont = {'FontSize' , 12};
T_max = 60;
T_min = -60;
omega_max = 200;
omega_min = -200;
T_dot_max = 5;
T_dot_min = -5;
Time = 100;
%% Optimization problem
prob = optimproblem('ObjectiveSense','max');
%optimization varibles
delta = optimvar('delta',1);
eta = optimvar('eta',1);
alpha = optimvar('alpha',1);
t = optimvar('t',1,'LowerBound',0,'UpperBound',Time);
%setting initial point
x0.delta = 0;
x0.eta = 0;
x0.t = 0;
x0.alpha = 0;
%profiles
T = alpha*t^2+eta*t;
T_dot = 2*alpha*t+eta;
omega = delta*t;
%constrains
cons1 = T <= T_max;
cons2 = T >= T_min;
cons3 = omega <= omega_max;
cons4 = omega >= omega_min;
cons5 = T_dot <= T_dot_max;
cons6 = T_dot >= T_dot_min;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;
prob.Constraints.cons4 = cons4;
prob.Constraints.cons5 = cons5;
prob.Constraints.cons6 = cons6;
%cost func
alpha = 1; %Weight factor
prob.Objective = (alpha^(2)*t^5)/5 + (alpha*eta*t^4)/2 + (delta^(2)*t^3)/3 + (eta^(2)*t^3)/3 + t;
%show and solve
show(prob)
sol = solve(prob,x0);
%% setting the profiles i get
dt = 1;
time = 0:dt:sol.t;
T_opti = sol.alpha.*time.^2+sol.eta.*time;
omega_opti = sol.delta.*time;
%% ploting
%omega profile
figure(1);
plot(time,omega_opti,linewidth{1:2});
xlabel('t [sec]');
ylabel('\omega [rad/s]');
grid on;
%temperature profile
figure(2);
plot(time,T_opti,'r',linewidth{1:2});
xlabel('t [sec]');
ylabel('T [\circC]');
grid on;
%both
figure(3);
yyaxis left
plot(time,omega_opti,linewidth{1:2});
xlabel('t [sec]');
ylabel('\omega [rad/s]');
ylim([omega_min omega_max]);
yyaxis right
grid on;
plot(time,T_opti,'r',linewidth{1:2});
xlabel('t [sec]');
ylabel('T [\circC]');
ylim([T_min T_max]);

Answers (1)

Are you sure t should be an optimvar? It seems to me it should be a set of discrete known values
t = linspace(0,Time,N)
Once you make that change, and choose const=T_min, it seems to me everything is as you want it.

5 Comments

first of all, thank u for your answer.
i tried what u said before, in that case, my Objective function look like that (just added the dots)
prob.Objective = (alpha^(2).*t.^5)/5 + (alpha*eta.*t.^4)/2 + (delta^(2).*t.^3)/3 + (eta^(2).*t.^3)/3 + t;
and i get this error:
Error using optimization (line 49)
Objective must be a scalar OptimizationExpression or a struct containing a scalar
OptimizationExpression.
beacuse now prob.Objective is an 101X1 vector of optimization varibles (1 of every time step )
how can i handle this ?
I can't tell what your objective is supposed to be when you have multiple time steps. Is it some sort of least squares criterion?
well,it might be the one in the last time step, t=Time. ( in later problems it might no)
at the moment i have
6X length(t) Constraints, when i try to solve it for the last objective ( beacuse now i know the grater as t is bigger) i get this:
Solving problem using quadprog.
Your Hessian is not symmetric. Resetting H=(H+H')/2.
The interior-point-convex algorithm does not accept an initial point.
Ignoring X0.
The problem is non-convex.
and to sol is just [ 1 1 1] array.
which is clearly not correct, beacuse it's not satisfied all the Constraints
in the future i would like to max my objective , when i dont know which t is giving me the largest value (maybe i sould stop before t=Time? and "save time"), so it's look like i might have 101X1 objective function, and i would like to find the biggest one (this is why i used t as optimvar in the start)
is my answer helpd u?
thank u so much
well,it might be the one in the last time step, t=Time.
If so, then,
prob.Objective = (alpha^(2)*Time^5)/5 + (alpha*eta*Time^4)/2 +...
(delta^(2)*Time^3)/3 + (eta^(2)*Time^3)/3 + Time;
thank u for the answer!
i have another question about this problem.
if i wish to make my temperature profile (T) go through all th range ( so go bewteen -60 to 60). how can i add this?

Sign in to comment.

Categories

Asked:

on 10 Jan 2021

Commented:

on 12 Jan 2021

Community Treasure Hunt

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

Start Hunting!