MATLAB: How to insert the output of ‘Polyfitn’ directly to an optimization function in the same code

curve fittinggenetic algorithmoptimization

Hello,
since a day I am working on this problem:
I generated a fit model f(X1,X2,X3) out of a data table with:
p = polyfitn([v,f,d],Ra,4)
Afterwards I try to convert it into a format which I could directly use for Genetic Algorithm Optimization.
functionRa = polyn2sym(p)
functionOptim = matlabFunction(functionRa)
If I try to use GA like this:
[X,fval] = ga(functionOptim,3,[],[],[],[],[LB1 LB2 LB3], [UB1 UB2 UB3])
I get the following error: _ Failure in initial user-supplied fitness function evaluation. GA cannot continue_
I would be very grateful if someone has an easy solution for my problem!
Thank you in advance!
Lukas

Best Answer

Hi,
the problem you have is X1, X2 and X3 can not be passed to your function handle by ga, when ga tries to find optimal values for them.
2 Options:
1. Replace X1 by x(1), X2 by x(2)... in your function handle so that it looks like below and only depends from x (no more from X1, X2, X3):
@(x)x(1).*(-2.772627949933848e11)-x(2).*2.825366193148432e14+x(3).*2.084627182 etc.etc.
2. Write a function that assigns the components from x to the values X1,X2,X3 and then calculates the result and gives it back to ga:
function out = calc_poly(x)
X1 = x(1);
X2 = x(2);
X3 = x(3);
out = X1.*(-2.772627949933848e11)-X2.*2.825366193148432e14+X3.*2.084627182 etc.etc.
end
ga and all the other solvers take only one vector (usually x) and pass it to a function. If you want to optimize for X1, X2, X3 you should do one of the ways above. So you enable ga to give the decision variables as one vector to the objective function.
Best regards
Stephan