First, note that you are using the factorial function in your model. So you cannot simply use the factorial function. In Matlab it is only defined for non-negative inputs. Instead use gamma() function. It is equivalent to factorial(n) = gamma(n+1) for integer value, but it also extended to non-integer and negative values.
Now, for solving the problem, if you have the global optimization toolbox, then my first suggestion would be to use genetic algorithm ga() to solve the problem. ga() has the option to enforce integer constraints. Following is the code
Table = csvread('tracer_data_reactorA_new.csv')';
time = Table(1,:);
tracer_conc = Table(2,:);
int_tot = 0;
for i = 1:size(time,2)-1
int_tot = int_tot + (0.5*tracer_conc(i)+0.5*tracer_conc(i+1))*(time(i+1)-time(i));
end
esp = tracer_conc/int_tot;
obj_fun = @(n_reac) norm(fun(n_reac, time) - esp);
n_reac_sol = ga(obj_fun, 1, [], [], [], [], [], [], [], 1);
function E_n = fun(n_reac,time)
tau = 1.2584;
tau_series = tau/n_reac;
fact = gamma(n_reac);
E_n = (time.^(n_reac-1).*exp((-time)./tau_series))./(fact.*(tau_series^n_reac));
end
If you don't have ga() function available to you, then you can follow the Image Analyst's advice and solve the problem for real numbers and finally round off the value. Note that this may not always work, since the objective function may not always be convex. However, In this case, it seems to work and both ga and lsqcurvefit gives very close output
Table = csvread('tracer_data_reactorA_new.csv')';
time = Table(1,:);
tracer_conc = Table(2,:);
int_tot = 0;
for i = 1:size(time,2)-1
int_tot = int_tot + (0.5*tracer_conc(i)+0.5*tracer_conc(i+1))*(time(i+1)-time(i));
end
esp = tracer_conc/int_tot;
obj_fun = @(n_reac) norm(fun(n_reac, time) - esp);
n_optimal = lsqcurvefit(@fun, 1, time, esp);
n_optimal = round(n_optimal);
function E_n = fun(n_reac,time)
tau = 1.2584;
tau_series = tau/n_reac;
fact = gamma(n_reac);
E_n = (time.^(n_reac-1).*exp((-time)./tau_series))./(fact.*(tau_series^n_reac));
end
Best Answer