Parameter and constraints definition

Hi all, I am currently working on a code that requires calling 4 functions for a fixed number of iterations. My question is how to define the parameters and constraints in one main function that are called by different functions. A rough format :
Global (??? how to define parameters and constraints)
For i=Niterations
Function A (calls function B with a @function B)
Function B(having constraints)
Function C(taking the values from the constraints and call function D and E)
Function D runs to give the geometry
Function E calculates the cost function
end
Best Regards,
Vivek

4 Commenti

Adam
Adam il 3 Nov 2015
Modificato: Adam il 3 Nov 2015
I always use classes for this type of programming (well, all types of programming) to wrap up all the parameters and pass them around as a single variable. In this case maybe two classes if parameters and constraints are distinct.
If you are not familiar with classes then structs will also do the job, especially if you aren't needing to add functions to what would be the parameters class.
I would strongly advise against global variables, in all cases, but probably especially so if you are nesting function calls.
Hi Adam,
Thanks for the inputs. I used structures and i was able to run the code smoothly. I have one more query to ask. Now i have a vector which i want to optimize.
Suppose g= [1 2 3 4 5 6 7 8 9];
lb=zeros(1,9);
ub=10*ones(1,9);
I am using the CPSO for optimization. I want to optimize 4 values from the vector g ( eg g(2),g(3),g(5),g(6)) but i need all the values to be fed to another code in order to compute the model. Is there any effective way to do it.
You can create a sub-vector easily from your full g vector as e.g.
g( [2 3 5 6] )
Thanks adam for the inputs. It helped a lot.

Accedi per commentare.

Risposte (0)

Richiesto:

il 3 Nov 2015

Commentato:

il 16 Nov 2015

Community Treasure Hunt

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

Start Hunting!

Translated by