Fitting a data set by optimising multiple parameters

조회 수: 8 (최근 30일)
Mohamed Elasmar
Mohamed Elasmar 2024년 5월 17일
댓글: Star Strider 2024년 5월 21일
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
the cyclist
the cyclist 2024년 5월 17일
Can you upload one or two example datasets? You can use the paper clip icon in the INSERT section of the toolbar.
Mohamed Elasmar
Mohamed Elasmar 2024년 5월 17일
편집: Mohamed Elasmar 2024년 5월 17일
Thank you @the cyclist for your answer. Here is the dataset of P = 5 & T = 90.
Please consider T = 363 instead of 90 as it is the temperature and should be used in Kelvin in the equations of a1 and a2.

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2024년 5월 17일
Perhaps something like this —
T1 = readtable('Ask_17.05.2024.xlsx')
T1 = 431x2 table
a V_total _______ _______ 0 1.32 0.80556 1.33 2.4444 1.34 4.0833 1.36 5.6944 1.38 5.6944 1.39 8.1389 1.42 9.7778 1.44 10.583 1.46 13.028 1.48 14.639 1.48 16.278 1.5 17.083 1.51 18.722 1.52 21.167 1.53 21.167 1.54
a = T1.a;
a(1) = 1E-4; % Please Avoid Calculating 'log(0)'
V_total = T1.V_total;
P = 5;
T = 90 + 273
T = 363
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))
V_total_fcn = function_handle with value:
@(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)
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
X = 2x1
5.4249 0.5849
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
Mohamed Elasmar
Mohamed Elasmar 2024년 5월 21일
I actually could not understand the following part of the code:
ftns = @(theta) 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);
[theta,fval,exitflag,output,population,scores] = ga(ftns, Parms, [],[],[],[],[],[],[],[],optsAns);
toc
NrGenerations = output.generations
Nevertheless, I have added it to my code and have commented the following two lines:
x0 = rand(2,1);
X = lsqcurvefit(V_total_fcn, x0, a, V_total)
In addition, I have defined the equations of R and G. Hence, the error was: Unrecognized function or variable 'x'.
You can try yourself by defining R and G as follows:
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;
Star Strider
Star Strider 2024년 5월 21일
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.
.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by