objectiveFunction = @(params) nlaSimResults(params);
upperBound = [25000 0.8];
options = optimoptions('ga','UseParallel',true,'UseVectorized',false,'PopulationSize',popSize,'MaxGenerations',genSize,'OutputFcn',@gaOutputFunction,'Display','iter');
[bestParams, minCost, ~, optResults, optPop, optScores] = ga(objectiveFunction,2,[],[],[],[],lowerBound,upperBound,[],options);
if saveFlag == 1; save('OptimResulst','bestParams','minCost','optResults','optScores','optimTime','popSize','genSize'); end
function cost = nlaSimResults(params)
assignin('base','Altitude',Altitude);
assignin('base','Mach',Mach);
cost = logsout{1}.Values.Data(1);
otherVars = [otherVars; logsout{2}.Values.Data logsout{3}.Values.Data];
assignin('base','otherVars',otherVars);
function [state, options, flag] = gaOutputFunction(options, state, flag)
persistent popHistory bestValues bestIndividuals otherVarsHistory
[bestScore, bestIdx] = min(state.Score);
bestValues = [bestValues; bestScore];
bestIndividuals = [bestIndividuals; state.Population(bestIdx, :)];
popHistory(state.Generation + 1) = {state.Population};
otherVarsHistory{state.Generation + 1} = otherVars;
assignin('base','popHistory',popHistory);
assignin('base','bestValues',bestValues);
assignin('base','bestIndividuals',bestIndividuals);
assignin('base','otherVarsHistory',otherVarsHistory);