Question about 2 output optimization

Hi guys!
I'm struggling to formulate a typical optimization problem into something than can be solved by MATLAB and was hoping you could assist me with this. I do have optimization toolbox available on my machine, so can use any function offered by the toolbox.
I have a software simulating a very complex non-linear model, that takes a set of inputs and gives me 2 outputs. In this case I am running a batch of simulations, only changing 3 input parameters, so I basically have a matrix of 3 input values and a matrix of output values. My aim is to correlate 1-st output parameter to match real-life data as closely as possible, while keeping the 2nd output parameter equal to a certain value. In other words I am trying to minimize error between real-life data and simulated data (what would be a typical error function for such case?), while imposing an equality constraint on the second output. I obviously cannot write the whole simulation in terms of single f(x) function to minimize.
Any thoughts on how to write such optimization problem and what function is typically used for something like this ?
Thank you for any help!

 Accepted Answer

Alan Weiss
Alan Weiss on 24 Sep 2014
This sounds straightforward to solve in Optimization Toolbox. You have a nonlinear equality constraint (the value of the second output) and want to minimize the difference between the first output and some data that you already have (maybe the sum of squares of the differences of the components if it is multidimensional).
You can easily formulate this for fmincon. To save computation time, use the technique described here for the case when your nonlinear constraint and objective function are given by the same computation.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

9 Comments

Alan, thank you for your help! This is exactly what I was looking for!
I should have also included information on some of the "gotchas" in optimizing a simulation or ODE. In particular, you might need to take larger-than-default finite difference steps.
Alan Weiss
MATLAB mathematical toolbox documentation
Kokalz
Kokalz on 24 Sep 2014
Edited: Kokalz on 24 Sep 2014
Thank you, Alan.
One more thing I'm slightly confused about:
I don't have an actual function to minimize. What I have is a set of input and output data. For minimization problem I can use sum of squares error, as mentioned by you above. However, how do I relate my simulation inputs and outputs to that function that I am trying to minimize, using fmincon ? The input is 1x2 vector and the output is [1x1000] [1x1] cell
Sorry, I do not understand your specific case. You have some measure that you are trying to minimize, correct? Your original post said that you are trying to "match" data. Well, quite often people use the sum of squares of differences to be the measure, and you try to minimize that measure. Whatever that measure is, as long as it is a scalar you can use fmincon to minimize it.
If you want to try to explain more specifically what you are doing, we can try to understand and help. But I will be away for a few days, so please do not take offense if it takes several days for me to answer you.
Alan Weiss
MATLAB mathematical toolbox documentation
Hi Alan,
It's not urgent, so any help is highly appreciated. I think I understand what you were saying though. I'll try to explain myself a little better this time.
Basically I have a large non-linear model, that takes in 2 input values and spits out 2 output values. Lets shows this as:
[in1, in2] -> {simulation model} -> [out1 out2]
I do not have access to the stuff going on inside the simulation model, only the input and output parameters. I can run say a batch of 250 simulations to improve the sensitivity study.
Now, in1 and in2 are single numbers, such as
in1 = 100,
in2 = 200
However:
out1 = [1x1000] % vector
out2 = [1x1] % value
I also have real-life experimental data:
RLout1 = [1x1000]
RLout2 = [1x1]
My aim is to tune the in1 and in2 parameters such that:
1) Minimise the error between out1 and RLout1, while
2) Have out2 = RLout2
So that basically makes it a a typical optimization problem, where I am minimizing an error (sum of squared errors ?) between the out1 and RLout1 vectors, while imposing the equality constraint.
I guess what you are saying is that my error is functioning as f(x) that I need to minimize. I can write that function as
min f(x) = SSE(RL_Data, predicted_Data)
But I just can't get my head around how I would be relating my simulated data and the optimization function. What would be the function linking real life data, simulated data and optimization predictions ? Do I fit a quadratic equation to my error between real life data and simulated data and then use that equation for optimization? Does this question make sense ?
Huge thank you for your help!
It is difficult for me to understand exactly what the problem is. I will attempt to walk you through some steps and see if they help you.
  • Begin by writing an objective function. The function must be of the form
function f = myobjective(x, RLout1)
[out1,out2] = simulation(x(1),x(2)); % x(1) is in1, x(2) is in2
f = sum((out1 - RLout1).^2); % sum of squares of differences
  • Write the nonlinear constraint function
function [c,ceq] = myconstraint(x,RLout2)
[out1,out2] = simulation(x(1),x(2));
c = [];
ceq = out2 - RLout2;
  • Write your input syntax, assuming that RLout1 and RLout2 are in your workspace
fun = @(x)myobjective(x,RLout1);
constr = @(x)myconstraint(x,RLout2);
For details on this syntax, see Passing Extra Parameters.
  • Call fmincon.
[solution,error] = fmincon(fun,x0,[],[],[],[],[],[],constr)
Again, for speed you should use the technique for when the objective and constraints are in the same function, and you might want to use larger-than-default finite difference steps.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Hi Alan,
Thank you for your help!
I can see why you are getting confused. Perhaps I did not explain myself properly.
Basically, I cannot stick 'simulation' function into the matlab script, as you have written in step 1. The model that takes inputs & produces results is a separate application, and I cannot put it inside MATLAB. I can, however, run a batch of simulations and have large number of input and respective output values. So the question I was trying to ask in the previous point is how do I write my objective function such, that I only use input & output values (if that is of course possible) ?
Does this make sense?
If it is impossible for MATLAB to communicate with your simulation, then I do not see how you can profitably use MATLAB optimization functions. The functions query various values of the control variables (x) and use the resulting objective function values to guide a search for the lowest objective function consistent with constraints.
But an idea just popped into my head. I suppose that you might write a grid of control variable values, evaluate the objective and constraints on this grid, and then construct an interpolating function that fmincon could evaluate. For an example using this technique, see Pattern Search Climbs Mt. Washington. The objective function, terrainfun, is just a call to interp2. You could do the same with the nonlinear constraint.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
That's exactly what I was looking for! I'll give it a go. Huge thank you for your help!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 24 Sep 2014

Commented:

on 7 Oct 2014

Community Treasure Hunt

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

Start Hunting!