Maybe this example would give you the basics of using the genetic algorithm (GA) to minimize a multivariate function. The problem to find the roots of a Cubic function given by
.Since the cubic function has no global minima, and the GA only minimizes a given function, then the root-finding problem must be reformulated to become a convex optimization problem.
y1 = x.^3 - 10*x.^2 + 31*x - 30;
y2 = abs(x.^3 - 10*x.^2 + 31*x - 30);
plot(x, y1, 'linewidth', 1.5)
title('Roots of a Cubic function')
plot(x, y2, 'linewidth', 1.5)
title('Absolute value of the Cubic function')
To setup the fitness function for GA, you can do as follows:
f = @(x) abs(x(1).^3 - 10*x(1).^2 + 31*x(1) - 30) + abs(x(2).^3 - 10*x(2).^2 + 31*x(2) - 30) + abs(x(3).^3 - 10*x(3).^2 + 31*x(3) - 30);
rootx = ga(f, nvars, A, b, Aeq, beq, lb, ub)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.