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?
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.