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);
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);
dif(k) = abs(z_calc_n(k)-z_mes(k));
fitcalc = 1/28*sum(dif/max(z_mes));
y = (1/fitcalc)^(-1/3);
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.