# How can I make a function be run with only teenager variables in matlab

4 views (last 30 days)
R@SH on 21 Oct 2021
Commented: dpb on 21 Oct 2021
Dear All,
I have a function with four variables. I want this function be run with only integer variabes. I want to define a bound for each variable and let it run within those span to find the best answer. I dont know how to define the variables to be limited to integers and not other values. for exaple:
Function (A)= optim(B,C,D,E)
B=1:1:10:
C=1:1:10:
D=2:2:10:
E=73:1:77:
It is noted that I am going to run this on Genetic algorithm toolbox. In there I have to determine the upper and lower bound of each parameter. But my problem is that it would be run with all poosible values between the upper and lower bound. I want them to be integer only. How can I do that?
I would appreciate your support in this matter
##### 2 CommentsShowHide 1 older comment
R@SH on 21 Oct 2021
Thanks you so much dear dpb for your support.
the proble is that I have to find the minimize of A in GA, and in order to to that I have to define the allowed bound and values for B,C,D and E to look around and find the minimum value for A and its corresponding B,C,D and E.
Since in GA toolbox, I will define the lower and upper bound of each parameter, here in the m.file of matlab I only have to specify that these parameters are integer and also specify the lead value of each parameter. for example:
"B" is an integer and the lead value of that would be 2.
I previousely chose:
Function A=optim(B,C,D,E)
B=x(1);
C=x(2):
D=x(3):
E=x(4);
but when I specified the upper and lower bound for each parameter in GA toolbox, I found that it consider all possibe cases and not only integers. (for example 4.5 for "B" is not acceptable)
I hope it is clear now, and also hope to have your feedback on this matter.

Image Analyst on 21 Oct 2021
Try this:
function A = optim(B, C, D, E)
% Clip values to a defined range
B = max([1, B]); % Clip so there are no values lower than 1.
B = min([10, B]); % Clip so there are no values more than 10.
% Round to nearest integer.
B = round(B);
% Clip values to a defined range
C = max([1, C]); % Clip so there are no values lower than 1.
C = min([10, C]); % Clip so there are no values more than 10.
% Round to nearest integer.
C = round(C);
% Clip values to a defined range
D = max([2, D]); % Clip so there are no values lower than 2.
D = min([10, D]); % Clip so there are no values more than 10.
% Round to nearest integer.
D = round(D);
% Clip values to a defined range
E = max([73, E]); % Clip so there are no values lower than 73.
E = min([77, E]); % Clip so there are no values more than 77.
% Round to nearest integer.
E = round(E);
##### 2 CommentsShowHide 1 older comment
dpb on 21 Oct 2021
I don't have the TB but what see the doc at https://www.mathworks.com/help/gads/mixed-integer-optimization.html?searchHighlight=integer%20optimization&s_tid=srchtitle_integer%20optimization_1 that illustrates how to set integer constraints on variables.
NB: the limitations on the types of problems that can be solved when doing this.

R2016b

### Community Treasure Hunt

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

Start Hunting!