# Nonlinear regression with two variables

27 views (last 30 days)
Dipon Sarkar on 20 Apr 2020
Edited: Ameer Hamza on 22 Apr 2020
Hi, Im not really adept at programming but I need to fit a non linear regression model : y=a*(T-c)*(1-exp(b*(T-d)))(1-10^(e-pH)) where I have the values for y, T and pH. I used The curve fitting tool with nonlinearleastsquaremethod and a trust region algorithm, to fit a simpler version of the model where I just had the y and T.
Can someone please tell me how to solve this by uing either the curve fitting tool or writting a script?
Dipon Sarkar on 21 Apr 2020
thats the regression coefficient that I would like to know from fitting the data. In the model its caled 'pHmin', which is the theoritical minimum pH required for the growth

Alex Sha on 21 Apr 2020
Hi, Dipon, post your y, T and pH data please, if possible.
Alex Sha on 22 Apr 2020
Hi, Dipon, rather than Matlab, the results above are obtained by using a package 1stOpt, easy for using, and with the ability of global optimization, no need to guess the initial start-values of each parameter, the codes look like:
Parameter a,b,c,d,e;
Variable y,T,pH;
Function y=a*(T-c)*(1-exp(b*(T-d)))*(1-10^(e-pH));
Data;
//y T pH
0.1157 5 5.5
0.1233 5 5.57
0.1865 12 5.56
0.2520 12 5.61
0.2519 15 5.53
0.2814 15 5.42
0.3680 18 5.37
0.3186 18 5.56
0.4549 22 5.42
0.4757 25 5.45
0.4559 25 5.49
0.4241 30 5.57
0.4697 30 5.46
0.4953 35 5.50
0.5767 35 5.42
0.4163 40 5.58
0.4279 40 5.53
a little more better result:
Root of Mean Square Error (RMSE): 0.0327506128180404
Sum of Squared Residual: 0.0182342448792722
Correlation Coef. (R): 0.971783944265729
R-Square: 0.944364034332657
Parameter Best Estimate
---------- -------------
a -3.90156564244422E-13
b 0.0540580605547428
c 52.4138409784489
d -5.49253907297291
e 15.4605277403042
You may try Matlab, however, with free start-values of each parameter, it will be very hard or impossible to obtain results better than above.

Ameer Hamza on 22 Apr 2020
The problem you have posted has several local optimal solutions. Alex posted a solution using 1stOpt. You can also do something similar using global optimization toolbox in MATLAB. However, due to the existance of several local optima, which solution is applicable to your problem depends on the bounds on the parameters. Try the following code. I have also posted a comparison with the parameters posted by Alex for comparison; however, for further narrowing down the solution, you need to specify the bound on each parameter.
X = ...
[0.1157 5 5.5
0.1233 5 5.57
0.1865 12 5.56
0.2520 12 5.61
0.2519 15 5.53
0.2814 15 5.42
0.3680 18 5.37
0.3186 18 5.56
0.4549 22 5.42
0.4757 25 5.45
0.4559 25 5.49
0.4241 30 5.57
0.4697 30 5.46
0.4953 35 5.50
0.5767 35 5.42
0.4163 40 5.58
0.4279 40 5.53];
y = X(:,1);
T = X(:,2);
pH = X(:,3);
Xdata = [T pH];
Ydata = y;
y = @(param, xdata) param(1).*(xdata(:,1)-param(3)).*(1-exp(param(2).*(xdata(:,1)-param(4)))).*(1-10.^(param(5)-xdata(:,2)));
problem = createOptimProblem('lsqcurvefit','x0',rand(1,5),...
'objective', y, 'xdata', Xdata, 'ydata', Ydata);
ms = MultiStart;
solMs = run(ms,problem,50); %% <<<---- solution by MATLAB function
sol_1st_opt = [-5.8541753677852E-13 0.0540580580588969 52.4138416996001 -5.49253844700665 15.2843009735593];
plot3(T, pH, Ydata, 'b+-', ...
T, pH, y(solMs, Xdata), 'r+--', ...
T, pH, y(sol_1st_opt, Xdata), 'm+-.', ...
'LineWidth', 2)
xlabel('T');
ylabel('pH');
zlabel('y');
legend({'Original Data', 'MultiStart MATLAB', '1stOpt'},...
'FontSize', 14, 'Location', 'best')

### Categories

Find more on Linear and Nonlinear Regression 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!

Translated by