Genetic algorithm (GA) calculated values compared to measured
6 views (last 30 days)
Show older comments
Mattias Larsson
on 19 Mar 2018
Commented: Star Strider
on 22 Mar 2018
Hi!
I want to use a number of measured values and try to minimize the differences between the measured values and the calculated values. I calculate the values, with Cb and Lb as inputs.
Rd = 0.1; %0.1;
Rb = 164;
for i=1:num_samples
w = i*pi*2*(10^6);
part1 = (j*w*Lb*Rb);
part2 = (Rb-(w^2)*Cb*Lb*Rb+j*w*Lb);
z_calc(i) = real(Rd + part1/part2);
end
and then taking the difference between the calculated and the measured.
diff(k) = abs(z_calc_n(k)-z_mes(k));
With the differences i can apply a fitness equation
fit = 1/28*sum(diff/max(z_mes));
y = (1/fit)^(-1/3);
I want to achieve a fitness based on two variables Lb and Cb. As a total new user to GA I can't understand how to generally approach this problem in a good manner. However, when i look at the GA function and examples included i see that i can pass a number of constrains. For example non of Rb or Lb can be negative.
How should i approach the problem? Is it solvable?
B.r. Mattias
0 Comments
Accepted Answer
Star Strider
on 19 Mar 2018
It is difficult for me to understand what you are doing, and I do not understand your code.
If your fitting parameters (the parameters you want to adjust to fit your data) are ‘Cb’ and ‘Lb’, your fitness function for the ga function would be something like this:
function y = fitfcn(b, z_mes)
Cb = b(1);
Lb = b(2);
for i=1:num_samples
w = i*pi*2*(10^6);
part1 = (j*w*Lb*Rb);
part2 = (Rb-(w^2)*Cb*Lb*Rb+j*w*Lb);
z_calc(i) = real(Rd + part1/part2);
end
dif(k) = abs(z_calc_n(k)-z_mes(k));
fitcalc = 1/28*sum(dif/max(z_mes));
y = (1/fitcalc)^(-1/3);
end
You would then use ‘fitfcn’ as the fitness function argument to ga, and call it as:
[B, fval] = ga(@(b) fitfcn(b, z_mes), ...);
and will return optimised parameters: ‘B(1)=Cb’ and ‘B(2)=Lb’.
Since ‘diff’ and ‘fit’ are MATLAB function names, I changed them so as not to cause conflicts (known as ‘overshadowing’).
This is the way I would use your code to create a fitness function for the ga function. I used your code as you posted it (and as I understand it). You may need to change it to give the results you want.
4 Comments
Star Strider
on 22 Mar 2018
As always, my pleasure!
A negative resistance would be very useful, just not physically possible due to the constraints of thermodynamics.
An initial population of 500 usually works for me. You can certainly try a larger initial population.
A constrained optimization will never provide results as good as an unconstrained optimization, although it is frequently required to make physical sense.
It is difficult for me to assess your fitness function, since I have no idea what your data are. I usually use a metric analogous to norm(z_mes - z_calc) as the fitness metric, since that would minimise the difference in a least-squares sense, and always produces a non-negative result. I would assess what your fitness function is producing, to see if that could be a problem. For example, is it giving the ‘weight’ to all data that you want it to, or is it fitting one section at the expense of another?
Without knowing more about what your data and model are, my first approach would be to assess the fitness function (that may have changed since you posted it) to be certain it is doing what you want it to do.
More Answers (0)
See Also
Categories
Find more on Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!