Asked by Saber_phy
on 15 Jun 2018 at 1:45

Hi

I am a student enrolled in the 2nd year physics Master's degree, i am a beginner in Matlab and i have the same problem like Mattias, can you help me and is it was possible to get your e-mail.

My problem is to make a fit of a nonlinear equation to data with the genetic algorithm and to minimize R, my model function is

R*(1-exp(-t/rate))

The industrial data Rexp and t are known: these are two vectors that give the variation of Rexp as a function of time (t)

rate also is known and equal to 40

I declared my function (fitnes function) as follows:

function S=ga_Newton(R)

t=[0 2 4 6 8 10 12 14 16 18 20 22 24 26 28];

Rexp=[1 .7987e-05 3.07996e-05 3.90531e-05 1.89562e-05 2.82794e-05 5.55377e-05

4.75723e-05 6.2447e-05 5.35282e-05 5.16775e-05 4.67144e-05 4.63215e-05 6.62753e-05

3.72551e-05 3.54374e-05];

for i=1:15

S(i) =sum((Rexp(i)-R*(1-exp(-t(i)./40)))^2);

end

The function

S (i) = sum ((Rexp (i) -R * (1-exp (-t (i) ./ 40))) ^ 2)

I use it in the methods of Gauss Newton and Levenberg marquardt and I got a good result of R which is in the order of 0.000059 but with the genetic algorithm I found no result.

Answer by Walter Roberson
on 15 Jun 2018 at 3:26

Accepted Answer

Replace

for i=1:15 S(i) =sum((Rexp(i)-R*(1-exp(-t(i)./40)))^2); end

with

S = sum((Rexp-R*(1-exp(-t./40))).^2);

Saber_phy
on 16 Jun 2018 at 0:26

Walter Roberson
on 16 Jun 2018 at 1:29

I have attached the exact files I used. Execution requires less than 3 seconds.

More generally, the approach I would use would be to use the Symbolic Toolbox to prepare the function to be minimized:

t=[0 2 4 6 8 10 12 14 16 18 20 22 24 26 28];

Rexp=[1.7987e-05 3.07996e-05 3.90531e-05 1.89562e-05 2.82794e-05 5.55377e-05 4.75723e-05 6.2447e-05 5.35282e-05 5.16775e-05 4.67144e-05 4.63215e-05 6.62753e-05 3.72551e-05 3.54374e-05];

syms R prediction = R*(1-exp(-t./40)); actual = Rexp; residue = sum((actual - prediction).^2); F = matlabFunction(residue);

nvars = 1; A = []; b = []; Aeq = []; beq = []; lb = []; ub = []; nonlcon = []; options = gaoptimset('PlotFcn', {@gaplotbestf @gaplotbestindiv @gaplotexpectation @gaplotstopping}, ... 'TolFun', 1e-13, 'Generations', 1000); [R, fval, exitflag, output] = ga(F, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options);

fprintf('best R: %g\n', R); fprintf('best residue: %g\n', fval);

Note: you will not get especially close to the true minima. The true minima is at approximately 0.000128381339932429 which can be quickly determined:

[R, fval] = fminunc(F, 0)

Saber_phy
on 16 Jun 2018 at 2:05

thank you very much :) :)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.