Equivalent to Excel Solver in Matlab?

EDIT: I edited my question to be more specific as the horchler comment helped me get started.

I have data in excel that I use to analyze optimization. In excel, using a solver is easy: I can select a cell with a formula, and then select the cells that I need to change, add constraints and then minimize it. But I get lost when it comes to the Matlab optimization process. The documentation seems to give only examples of optimization problems that analyze simple single-line functions, such as f (x) = - (x1) (x2) (x3). I cannot figure out how to apply these examples in my own case.

The function I'm trying to maximize is relatively complex. As input, it accepts many scalar variables, as well as several structures that contain data that are used in the calculations.

My problem is that I am trying to maximize the value of a function by modifying three scalar variables, leaving input constants constant (since they are data). More specifically, my function looks something like this:

function x = NameOfFunction (w1, w2, w3, a, b, c, Structure1, Structure2, Structure3) 

I would like to maximize x by changing only the variables w1, w2 and w3. In other words, I would like Matlab to tell me the values ​​of w1, w2, and w3 that maximize x, leaving all the other variables alone. Any insight is greatly appreciated.

+4
source share
1 answer

No limits

You will probably find a solution with fmincon or fminunc in MATLAB. For example, using fminunc because its syntax is a little cluttered, you can start by defining the cost function in a separate file called "NameOfFunction.m":

 function cost = NameOfFunction(w, a, b, c, Structure1, Structure2, Structure3) % Your code goes here, just remember that you return a scalar-valued cost from % this function. 

Note that fminunc and the like will try to minimize this cost function. If you need to maximize it, just multiply the final cost by -1 at the end. Then you create a handle to your function in your main file:

 h = @(w)NameOfFunction(w, a, b, c, Structure1, Structure2, Structure3); 

Where w is the vector of variables you want to optimize:

 w = [w1, w2, w3]; 

This basically masks your function with all its inputs as the function you want to optimize, w as far as fminunc is concerned. This allows you to pass your parameters a , b , c , Structure , Structure2 and Structure3 into your NameOfFunction cost NameOfFunction without fminunc , touching them. Now you can call fminunc on your descriptor with the original assumption for your vector w :

 w0 = [w1_init, w2_init, w3_init]; [w, fval] = fminunc(h, w0); 

And fminunc should find the optimal values ​​for your vector w , which minimizes (note that it is looking for the minimum) your cost function.

With restrictions

In this case, you would most likely use fmincon . If your constraints are in the forms of the upper and lower bounds of each of your parameters that you optimize, then put them in vectors:

 ub = [w1_upper, w2_upper, w3_upper]; lb = [w1_lower, w2_lower, w3_lower]; 

And call the same descriptor as before using fmincon :

 [w, fval] = fmincon(h, w0, [], [], [], [], lb, ub); 

Four [] in the example above are just placeholders for parameters that you are not using. fmincon can handle more complex constraints; check the documentation (linked at the beginning of this discussion) for more details.

+5
source

Source: https://habr.com/ru/post/1489157/


All Articles