Fitting a data set by optimising multiple parameters
Show older comments
Hello,
I am developing a model for simulating experimental data sets. Each data set corresponds to two specific variables (P and T). The model is generally represented by the following equation:
V_total = V1 + V2 + A log (a/a1) + B log (a/a2)
where a = 0:1.5:700;
The terms of V1, V2, A and B are calculated through specific equations so you can consider them constants. The coefficients of a1 and a2 are functions of P and T as follows:
a1 = x1 * P^0.5 * exp(16.95 - (5052 / T))
a2 = x2 * P^0.5 * exp(8.47 - (2526 / T))
For each data set, I want to obtain the best fitting by optimising coefficients x1 and x2.
For better understanding, here is a plot of the data set of P = 5 & T = 90. I randomly used x1 = 0.1 and x2 = 0.005.

I would be grateful if someone helps me in solving this issue.
Thank you in advance.
2 Comments
the cyclist
on 17 May 2024
Can you upload one or two example datasets? You can use the paper clip icon in the INSERT section of the toolbar.
Mohamed Elasmar
on 17 May 2024
Edited: Mohamed Elasmar
on 17 May 2024
Answers (1)
Perhaps something like this —
T1 = readtable('Ask_17.05.2024.xlsx')
a = T1.a;
a(1) = 1E-4; % Please Avoid Calculating 'log(0)'
V_total = T1.V_total;
P = 5;
T = 90 + 273
V1 = rand; % Provide Missing Value
V2 = rand; % Provide Missing Value
A = rand; % Provide Missing Value
B = rand; % Provide Missing Value
a1 = @(x,a) x(1) * sqrt(P) * exp(16.95 - (5052 / T));
a2 = @(x,a) x(2) * sqrt(P) * exp(8.47 - (2526 / T));
V_total_fcn = @(x,a) V1 + V2 + A*log(a/a1(x,a)) + B*log(a/a2(x,a))
x0 = rand(2,1);
X = lsqcurvefit(V_total_fcn, x0, a, V_total)
figure
plot(a, V_total, '.', 'DisplayName','Data')
hold on
plot(a, V_total_fcn(X,a), '-r', 'DisplayName','Regression Fit')
hold off
grid
xlabel('a')
ylabel('V\_total')
legend('Location','best')
This may work without further changes with the correct values for ‘V1’, ‘v2’, ‘A’, and ‘B’, however it may be necessary to use the real, imag, or abs functions to deal with the complex results if they persist after that.
.
5 Comments
Mohamed Elasmar
on 21 May 2024
Edited: Mohamed Elasmar
on 21 May 2024
It's not an unstable behaviour, but the usual dependence of the solution on the initial guess.
If you only know a range where the unknown parameters are located, use "MultiStart":
Star Strider
on 21 May 2024
Edited: Star Strider
on 21 May 2024
My pleasure!
I cannot run this because ‘R’ and ‘G’ are missing. (They are required for ‘V2’ and ‘V_total_fcn’.)
The problem with nonlinear parameter estimation is that the results are sensitive (extremely sensitive in some instances) to the initial parameter estimates, especially if there are multiple local minima in the response surface. The genetic algorithm optimisation should work, and should give the global values for the ‘x’ parameter vector that give the best overall fit. (I am also not constrainiing ‘x’ in this instance.)
I will run this when the required paramters appear.
Table1 = readtable('Ask_17.05.2024.xlsx');
a = Table1.a(1:15:end,:); % Reduction of the exp. data
V_total = Table1.V_total(1:15:end,:); % Reduction of the exp. data
a(1) = 1E-2; % An arbitrary starting value
P = 5;
T = 90 + 273;
A = 0.0379;
B = 0.0338;
V1 = 1.2148;
R = 0.02777 .* ((1./(143.5860 .* (1 - ((1.1261e-08 .* a) ./ (3e-5 + (1.1261e-08 .* a)))).^1.5)) + (1./(143.5860 .* (1 - ((5.6303e-09 .* a) ./ (3e-5 + (5.6303e-09 .* a)))).^1.5)));
G = 8.9717e-3 .* (a .* 10).^0.3;
V2 = a .* 0.36 .* (6.7399e-04 + R); % where R is a function of 'a' and is defined through a specific equation.
a1 = @(x,a) x(1) * sqrt(4.2575) * 20.8113;
a2 = @(x,a) x(2) * sqrt(4.2575) * 4.5619;
V_total_fcn = @(x,a) V1 + V2 + (A .* log(a./(a1(x,a) .* (1 - G)))) + (B .* log(a./a2(x,a) .* (1 - G))); % Final equation of V_total, where G is also a function of 'a' and is defined through a specific equation.
% x0 = rand(2,1);
% X = lsqcurvefit(V_total_fcn, x0, a, V_total)
ftns = @(x) norm(V_total - V_total_fcn(x,a));
PopSz = 500;
Parms = 2;
optsAns = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-3, 'MaxGenerations',5E3, 'FunctionTolerance',1E-10); % Options Structure For 'Answers' Problems
tic
% [theta,fval,exitflag,output,population,scores] = ga(ftns, Parms, [],[],[],[],zeros(Parms,1),Inf(Parms,1),[],[],optsAns);
[X,fval,exitflag,output,population,scores] = ga(ftns, Parms, [],[],[],[],[],[],[],[],optsAns);
toc
fprintf('\nParameters —\n\t\tx(1) = %.6f\n\t\tx(2) = %.6f\n\n',X)
NrGenerations = output.generations
FitnessValue = fval
V_t_mdl = fitnlm(a,V_total, V_total_fcn, X)
X = V_t_mdl.Coefficients.Estimate;
fprintf('\nTweaked Parameters —\n\t\tx(1) = %.6f\n\t\tx(2) = %.6f\n\n',X)
figure
plot(a, V_total_fcn(X,a),'Color','#D95319','LineWidth',1.3)
hold on
plot(a, V_total,'o','MarkerEdgeColor','#D95319','LineWidth',1.3)
hold off
lgd = legend('Simulation','Experiment','Location','southeast');
EDIT — (21 May 2024 at 17:01)
Changed code to include definitions for the missing parameters and the fitnlm call to provide statistics and to tweak the parameters returned by ga.
.
Mohamed Elasmar
on 21 May 2024
Star Strider
on 21 May 2024
My revised code (edited a few minutes ago in my previous Comment) implements the genetic algorithm (ga), and the additional assignments define the fitness function ‘ftns’ and the options structure I use with my ga calls.
I did not previously correct my code because I could not run it, so there are several obvious errors.. The current version runs and gives a reasonable result. I added a fitnlm call both to provide statistics on the parameters and the fit, and to tweak the parameters to give the best result. (The ga function actually has a version of this as part of its options and refers to it as a hybrid parameter estimation.)
There may still be some differences in the estiamted parameters between the runs, however they should be within the confidence intervals for any set of estimated parameters. The fit in general is quite good.
.
Categories
Find more on Genetic Algorithm 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!



