Applying the Nonlinear Least Squares Method to Minimize the Objective Function to Find the Parameters of the Equation

The liquid-liquid equilibrium data were fitted using the NRTL equation. The equation parameters of NRTL are derived from the experimental data.
below is my code:
clc,clear
options=optimset('MaxIter',4000,'MaxFunEvals',2000000,'algorithm','levenberg-marquardt');
b0=[0,0,0,0,0,0];
[b,gamma1,gamma2,gamma3,gamma4,gamma5,gamma6]=lsqnonlin('NRTL',b0,[],[],options);
Although the code gives the result, the result is not what I want. And the format of the output gamma is also wrong. It should be a matrix of the same order as x1, but it has become something I don't understand. Can someone please give a reason? It would be best to give some solutions. Thanks!

 Accepted Answer

Your code makes strange assumptions about the output syntax of lsqnonlin.
Why do you think lsqnonlin will return values for the gamma variables in NRTL?

6 Comments

Thanks for your answer. If I want to know the calculated gamma values, I can only bring the fitting parameters (b) back to the original equations, not the output in the form in my code, is my understanding correct?
Make a function "gamma_values" in which you calculate the gamma values. This function can then be called from "NRTL" as well as from the main program after you receive the optimal parameters b.
By the way: lsqnonlin works on the equations you want to nullify, not on their sum. Thus use
f = [x1-gamma4./gamma1.*x4;x2-gamma5./gamma2.*x5;x3-gamma6./gamma3.*x6]
instead of
f=(sum((x1-gamma4./gamma1.*x4).^2+(x2-gamma5./gamma2.*x5).^2+(x3-gamma6./gamma3.*x6).^2));
The xdata are your model inputs. So either
x(:,1)+x(:,2)+x(:,3)==ones(5,1);
x(:,4)+x(:,5)+x(:,6)==ones(5,1);
is true or not - it is not influenced by the optimization since the arrays are independently prescibed.
Summarizing: It makes no sense to prescribe the above conditions.
If you want
x(:,1)+x(:,2)+x(:,3)-1.0 = 0;
x(:,4)+x(:,5)+x66-1.0 = 0
to be satisfied exactly and if you want x >= 0, use
options=optimset('MaxIter',40000,'MaxFunEvals',2000000,'algorithm','levenberg-marquardt');
x0=0.1.*ones(5,5);
lb = zeros(5,5);
ub = ones(5,5);
x=lsqnonlin('NRTLyan',x0,lb,ub,options);
together with the x(:,:) squared in your equations in function NRTLyan.
If you want x>= 0 and if it suffices if the relations
x(:,1)+x(:,2)+x(:,3)-1.0 = 0;
x(:,4)+x(:,5)+x66-1.0 = 0
are only approximately satisfied, use
options=optimset('MaxIter',40000,'MaxFunEvals',2000000,'algorithm','levenberg-marquardt');
x0=0.1.*ones(5,5);
lb = zeros(5,5);
ub = ones(5,5);
x=lsqnonlin('NRTLyan',x0,lb,ub,options);
together with
f=[x(:,1).*gamma1-gamma4.*x(:,4);x(:,2).*gamma2-gamma5.*x(:,5);x(:,3).*gamma3-gamma6.*x66;x(:,1)+x(:,2)+x(:,3)-1.0;x(:,4)+x(:,5)+x66-1.0];
If you want the relations
x(:,1)+x(:,2)+x(:,3)-1.0 = 0;
x(:,4)+x(:,5)+x66-1.0 = 0
to be satisfied exactly, but x>= 0 does not matter, only solve for x(:,i) for i=1,2,4 and calculate x(:,j) for j=3,5 from the relation.
Do you have any other suggestions?
@yu zhang I suggest you Acccept-click this answer, since your original question appears to have been resolved.
Since you have a new question and since it is related to code different from this question, I suggest you post that in a new thread.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Asked:

on 4 Jul 2022

Edited:

on 5 Jul 2022

Community Treasure Hunt

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

Start Hunting!