how to find inputs based on desired outputs?

24 views (last 30 days)
I am trying to optimize my inputs for a function that takes 15 inputs (lets call them "A") and generates 17 (lets call them "B") outputs. I also have 17 (lest call them "D") values that I want to match my function's outputs ("B") to these values. First, I have a pretty good starting points for the "A" input values that gives me a good output "B" that is pretty close to "D" values, but not close enough, and I need to tune my input to get a better match.
My question is that what is the best method to optimize the "A" values, and change them in a way that matches my desired output "D" values?
Thank you.

Accepted Answer

Alan Weiss
Alan Weiss on 12 Mar 2015
If you have Optimization Toolbox, then use the lsqnonlin solver. Your objective function is
fun = @(x)Bfunction(x)-D
where Bfunction is your function that accepts a 15-dimensional argument x and returns a 17-dimensional return B that should be as close as possible to D. lsqnonlin tries to vary x so that the sum of squares norm(B-d)^2 is minimized.
If you just have base MATLAB, then you can try to use fminsearch with the objective function
fun = @(x)sum((Bfunction(x)-D).^2)
but this will probably be slower and less accurate than using lsqnonlin.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Sina
Sina on 13 Mar 2015
Thanks for the response.
I set up the obj function, and used it in the solver but there is a problem: the solvers gives the flag that it has found an answer (flag code: 1) but the answer is way off. I don't understand why it is saying that it has reached an answer. here is the obj function output: function F = hypcom(x)
.... (the function formulas and etc)
F(1)=den5(2); F(2)=den5(3); F(3)=den5(4); F(4)=den5(5); F(5)=den5(6); F(6)=den5(7); F(7)=den5(8); F(8)=den5(9); F(9)=den5(10);
and here is the code that I am using for the solver:
ydata=[1.9741e+006 3.0864e+011 3.7988e+017 3.0816e+022 1.6837e+028 1.0273e+033 9.2107e+037 5.3048e+042 6.0499e+044];
%% x0=zeros(1,15); x0=[a1ZC a2ZC a3ZC a4ZC b1ZC b2ZC b3ZC b4ZC a1ZO a2ZO a3ZO b1ZO b2ZO b3ZO b4ZO]; %% opt = optimset('TolX',1e-15,'TolFun',1e-15,'MaxIter',1e20);
[x,resnorm,residual,exitflag]= lsqnonlin(@(x)hypcom(x)-ydata,x0,[],[],opt)
the answer that I am getting is not close to "ydata" values.
Sina

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 12 Mar 2015
There is NO best method. There are many good methods. Good or best are measures that depend strongly on your problem, depending on how fast it is to evaluate, if it is smooth, if there are constraints, how accurate an answer you need, if there are discrete parameters (integer valued), etc.
You have more outputs than inputs, so this is in general a nonlinear least squares problem, since it will generally be impossible to solve for an exact solution. Depending on the issues I mention above, tools you might use would be one of lsqnonlin, fmincon, a genetic optimizer, simulated annealing, or a particle swarm tool of some ilk. There are many variety of particle swarm tools of course to be found on the File Exchange.
If you use one of the general nonlinear minimizers, then you will need to provide a sum of squares of the errors relative to your goal output. If you use lsqnonlin, then it will form that sum of squares, so all you need do is supply the residuals.

Products

Community Treasure Hunt

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

Start Hunting!