Clear Filters
Clear Filters

Is there a way to use an optimvar to choose a mathematical function?

5 views (last 30 days)
I have an optimvar, opening, that determines three other parameters through basic arithmetic.
opening = optimvar('opening','LowerBound', -30, 'UpperBound', 30);
opening_zero_geo_def = -(e+temp_thickness)/2;
geo_def_des_load = -opening + opening_zero_geo_def;
geo_def_rebound = geo_def_des_load - rebound;
geo_def_jounce = geo_def_des_load + m_2_m_clear;
po_k = (design_load/spring_rate)+geo_def_des_load;
po_kl = po_k/spring_length;
Two parameters, geo_def_jounce and geo_def_rebound, are used to form a domain.
h = .1
travel = geo_def_rebound:h:geo_def_jounce;
The goal is to use po_kl and one other parameter to choose a function that the vales will be manipulated through.
For example
if po_kl is close to .7
rate = .5*travel^3
if po_kl is close to .3
rate = .1*travel^3 + .4*travel^2
(pseudo code)
A polynomial fit is then applied to rate and the root mean squared error of the fit is calculated, and the RMSE is the minimization objective.
My question is how to do this. I've tried using an integer optimvar as an index as suggested by Alan Weiss (https://www.mathworks.com/matlabcentral/answers/374059-how-can-i-set-an-optimization-variable-to-be-an-element-of-a-set-categorical) but this doesn't work as MATLAB gives an error that optimvars cannot be used as indices.
I've also tried just passing everything with no consideration that it is an optimvar, and that does not work either because an optimvar cannot be used as a conditional statement.
I think it might be non-linear and then i'll have to a solver based approach but i'm not sure.

Answers (1)

Ayush Modi
Ayush Modi on 14 Jan 2024
Hi Kendall,
I understand that you're trying to use an optimization variable as an index in MATLAB, which has led to some confusion.
Taking the example provided by @Alan Weiss in the community, answer you referred, optimization variables, such as "optimvar", cannot be used as indices in MATLAB. Indices must be concrete numerical values such as 1 and 2.
y = x*v(1) + (1-x)*v(2)
Here, x is the optimization variable and should not be used as an index. Instead, 1 and 2 are the indices used to access elements of the vector v.
Regarding your pseudo code for determining the value of the rate variable, you can use the following code snippet:
if abs(po_kl - 0.7) < abs(po_kl - 0.3)
% If po_kl is closer to 0.7
rate = 0.5 * travel.^3;
elseif abs(po_kl - 0.3) < abs(po_kl - 0.7)
% If po_kl is closer to 0.3
rate = 0.1 * travel.^3 + 0.4 * travel.^2;
else
% If po_kl is equidistant from 0.3 and 0.7, choose one (here I choose the first)
rate = 0.5 * travel.^3;
end
I hope the above solution resolves the issue!

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!