How can I use the Optimization Toolbox with user-defined design variables instead of predefined (hard coded) design variables?

2 visualizaciones (últimos 30 días)
My aim is to wrap an optimizer code around an already existing design calculation; so as to create a more flexible and generic program that will allow the user to choose which variable(s) they want to vary (as the design variable) and record the corresponding response in the output. For example, consider the Banana function with variables 'a','b' : out = (100*(b - a^2)^2 + (1 - a)^2)
  1. ask user, how many design variables to be defined? (0,1 or 2)
  2. ask user, which variables to use as design variables? (a,b or neither)
  3. change or assign 'a','b' as design variables x(1), x(2)
  4. optimize the function to find minimum output using fmincon()
I am unable to achieve step 3 without hardcoding both variables as x(1), x(2). I would like the flexibility of choosing either or both variables (a,b) to be the design variables, as and when desired; because the design calculation I'm using has more than 6 variables and is highly non-linear, so hard coding each variables' combination is a mammoth task.
I have tried optimvar(), but it doesn't seem to work for non-linear functions.
  2 comentarios
Stephen23
Stephen23 el 30 de Oct. de 2018
Editada: Stephen23 el 30 de Oct. de 2018
All of MATLAB's optimization operators require the function to accept the "variables" being optimized within one array (usually as the first input argument to the function). So even disregarding your other requirements, your first step is to ensure that your function is written to do that. The easiest way is to use x(1), x(2), etc., or vectorized code. However if you have written your function with separate input arguments and don't want to change this then you can easily write a simple wrapper function. And once you have a wrapper function it is easy to use indexing to specify which function inputs (i.e. array elements) that you want to optimize, and which ones that you want to keep constant.
Start by reading these:
Archana Beena
Archana Beena el 1 de Nov. de 2018
YES! It worked! Thank you, Sir. I wasn't using a wrapper function. That was the issue. Thanks again! :)

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Problem-Based Optimization Setup en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by