Substitute variable and find maximum of the function
    3 views (last 30 days)
  
       Show older comments
    
Let's denote a function   , where x and y belong to interval [1,5]. I would like to find maximum of this function, knowing that
, where x and y belong to interval [1,5]. I would like to find maximum of this function, knowing that  can have only two values : 0 or 1. So the code should check for what values of
 can have only two values : 0 or 1. So the code should check for what values of  we can possibly get maximum and then calculate it within given intervat. At the end maximum should be printed.
 we can possibly get maximum and then calculate it within given intervat. At the end maximum should be printed.
 , where x and y belong to interval [1,5]. I would like to find maximum of this function, knowing that
, where x and y belong to interval [1,5]. I would like to find maximum of this function, knowing that  can have only two values : 0 or 1. So the code should check for what values of
 can have only two values : 0 or 1. So the code should check for what values of  we can possibly get maximum and then calculate it within given intervat. At the end maximum should be printed.
 we can possibly get maximum and then calculate it within given intervat. At the end maximum should be printed.My approach was to first substitute  with permutations of 0 and 1, it's 4 combinations in total. Knowing
 with permutations of 0 and 1, it's 4 combinations in total. Knowing  we could find max within given intervals simply by creating a linspace
 we could find max within given intervals simply by creating a linspace 
 with permutations of 0 and 1, it's 4 combinations in total. Knowing
 with permutations of 0 and 1, it's 4 combinations in total. Knowing  we could find max within given intervals simply by creating a linspace
 we could find max within given intervals simply by creating a linspace x = linspace(1,10);
for x and y, then meshgriding x and y, and in the end finding max using max(max(z)). The problem is, I don't know how to make a solution that will work fast enough to substitute 8 variables, for a more complicated function. I asked a similar question on this forum and this is the answer :
syms z(x,y) q1 q2
z(x,y)=(((sin(x).*(1+q1))./(3))+(1+q2))./(log10(y+1)-10);
A=perms([-1 1]);
c = linspace(1,5,100);
d=c;
[C,D] = meshgrid(c,d);
for i=1:size(A,1)
    Z(x,y)=subs(z,{q1,q2},{A(i,1),A(i,2)})
    Z_max(i) = max(max(double(vpa(Z(C,D),2))));
end
It works fine for  , but it takes too much time to compute the result for 8 variables (
, but it takes too much time to compute the result for 8 variables ( ,
, ). The only thing that's needed is maximum.
). The only thing that's needed is maximum.
 , but it takes too much time to compute the result for 8 variables (
, but it takes too much time to compute the result for 8 variables ( ,
, ). The only thing that's needed is maximum.
). The only thing that's needed is maximum.2 Comments
Accepted Answer
  Ameer Hamza
      
      
 on 5 Apr 2020
        The only function provided by Mathworks, of which I am aware, which can efficiently handle such mixed integer nonlinear programming problems is ga(): https://www.mathworks.com/help/gads/ga.html. However, you need to have global optimization toolbox to use this function
fun = @(x, q) (sin(x(1)).^2 + log(x(2)))./(x(1) + q(1) + q(2));
lb = [1;1;0;0]; % lower bounds
ub = [5;5;1;1]; % upper bounds
[sol, f] = ga(@(x) -fun(x(1:2), x(3:4)), 4, [], [], [], [], lb, ub, [], [3 4]);
Result: 
>> sol
sol =
    1.0000    5.0000         0         0
>> f
f =
   -2.3175
13 Comments
  Ameer Hamza
      
      
 on 7 Apr 2020
				John, I think the objective function still makes sense in the restricted domain, as mentioned in the question. The singularity is excluded from the domain of optimization. Also, the range of (-inf, inf) is the property of the objective function of every linear programming problem, but still, we are happy to find its solution on the constrained domain.
More Answers (1)
  John D'Errico
      
      
 on 5 Apr 2020
        Yes. It is probably best, IF you have some completely general function where you won't tell us the function, to use GA. Ameer is correct, and I have added a +1 vote for that answer because it is the most general solution. The goal of my answer is to discuss a different approach.
First, don't create numbered named unknowns like that. It will just cause problems in your code when you have many of them.
The simplest way to generate all combinations of such a binary variable is to use a binary representation of the variables.
nq = 2;
Q = dec2bin(0:2^nq-1) - '0'
Q =
     0     0
     0     1
     1     0
     1     1     
So rather than using meshgrid, which will get messy for 3 or 6 or 8 variables, just use dec2bin.
fun = @(x,y,q) (sin(x).^2 + log(y))./(x + q(1) + q(2));
Now, you might substitute each combination as the rows of Q into this function, using a loop. Perhaps:
for i = 1:size(Q,1)
  funi = @(x,y) fun(x,y,Q(i,:));
  % now just find the optimum value of funi, for this combination of the variables q.
  ...
end
You might decide to use fminsearch, or perhaps use a better, more efficient optimizer. Using linspace on one of the variables is not a good idea. Learn to use optimizers properly, and you will find your code becomes much more efficient.
I won't write any serious code for this, since that objective is clearly just random garbage, not a serious function that you might be hoping to work with.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


