Global optimization using symbolic equations

5 views (last 30 days)
I want to do global optimization using Multistart or GlobalSearch (curve fitting mainly with lsqcurvefit and fmincon); I want to use symbolic math toolbox because it gives me more flexibility; the problem is that I do not know how to set my problem; my objectives are as follows:
  1. Enter the model as string expression (one by one, the strings are in cell array and each time I press enter, the solver will take one model and try to find the best fit on the same experimental data- it is a kind of testing the validity of the model or its representativity of the data- models are of different number of parameters between 10 to 16 parameters, data points are about 1500 sets)
  2. Convert the string to symbolic expression
  3. Calculate the derivatives
  4. Convert the symbolic to matlab function
  5. Pass it to solver and find optimum parameters (with the ability to set the range or fix the parameter)
the solver needs an array of parameters, but I do not have the parameters in a form of array like par(1), par(2) ..., I tried to use symvar but it extracts the parameters in the expression only; in short I am lost so any kind of help or idea will be appreciated. I found a good document ( here ) by Alan Weiss which is very nice document but I could not ajust it to my case, but the idea is similar; thanks in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 9 May 2016
YourSymbolicExpression = sym(YourString);
SV = symvar(YourSymbolicExpression);
f = matlabFunction(YourSymbolicExpression, {SV});
gr = gradient(YourSymbolicExpression, SV);
grf = matlabFunction(gr, 'vars', {SV});
hess = hessian(YourSymbolicExpression, SV);
hessf = matlabFunction(hess, 'vars', {SV});
Now f and grf will be the handles of anonymous function which take a row vector of values. f would in itself be suitable as an objective function and grf would be suitable as a gradient function. However if you are using gradient functions (or hessian) with fmincon trust-region-reflective then you need pack them all into one function that only assigns outputs conditionally. You can do that with something like,
Frm = @(x) FunGrad(x, f, grf, hessf);
and pass Frm to fmincon,
where FunGrad is something like,
function [F,g, h] = FunGrad(x, Fun, Grad, Hess)
F = Fun(x);
if nargout > 1
g = Grad(x);
end
if nargout > 2
h = Hess(x);
end
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!