# Fitting a data set by optimising multiple parameters

5 views (last 30 days)

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

### Answers (1)

Star Strider
on 17 May 2024

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

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.

.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!