Nonlinear regression with 2 independent variables

9 views (last 30 days)
I want to fit a nonlinear model to a set of experimental data. It has 1 dependent variable, i, and 2 independent variables, td and Tr. I have 7 values for Tr, 7 for td and, therefore, 49 for i. The model I want to fit is this:
i = k*Tr^m/((c+td)^n).
So, I want to determine the values of the parameters k, m, c and n. I've been working on this for days and could not find a solution. Could anyone help me out?
Thanks in advance.
Marcus.
  3 Comments
Marcus Souza
Marcus Souza on 6 May 2016
I have Curve Fitting and Optimization toolboxes, but didn't use them. I tried to use fitnlm function to adjust the model, but I think my problem is probably related on how to dispose the variables. I have i as a 7x7 matrix, Tr and td as a 7x1 array (each one).

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 6 May 2016
This is actually straightforward if not all that well-documented. You have to combine your independent variables into a matrix, then refer to the matrix columns within your regression function as the individual independent variables. This will work with fminsearch, nlinfit, and lsqcurvefit. (I don’t have the Curve Fitting Toolbox, so I can’t comment on it.) Note that here ‘i’ also has to be a column vector, so if you use nlinfit or lsqcurvefit, use it as ‘i(:)’ as I did in my code here.
Using fminsearch (core MATLAB function, so no Toolboxes required):
% Original Equation: i = k*Tr^m/((c+td)^n)
% MAPPING: b(1) = k, b(2) = m, b(3) = c, b(4) = n
i_fcn = @(b,x) b(1).*x(:,2).^b(2) ./ (b(3) + x(:,1)).^b(4); % Regression Equation
x = [td(:) Tr(:)]; % Independent Variable Matrix
b0 = rand(4,1); % Initial Parameter Estimates
SSECF = @(b) sum((i(:) - i_fcn(b,x)).^2); % Sum-Squared-Error Cost Function
[b_estd, SSE] = fminsearch(SSECF, b0); % Estimate Parameters
i_fit = i_fcn(b_estd,x); % Generate Fit Line
I tested this with random data and it runs without error. It should give you the result you want.
  10 Comments
Star Strider
Star Strider on 6 May 2016
It would've been quite helpful to know that earlier.
I’m not certain if ‘Tr’ defines the rows and ‘td’ the columns, or if I have those reversed. Either way, it would be easy to modify this code to make them work, by simply transposing your ‘i’ matrix in the fit routines. The surface plot of your function and the fitted values suggests a decent fit:
[Trm,tdm] = meshgrid(Tr, td);
x = [tdm(:) Trm(:)]; % Independent Variable Matrix
b0 = rand(4,1); % Initial Parameter Estimates
SSECF = @(b) sum((i(:) - i_fcn(b,x)).^2); % Sum-Squared-Error Cost Function
[b_estd, SSE] = fminsearch(SSECF, b0); % Estimate Parameters
i_fit = i_fcn(b_estd,x); % Generate Fit Line
[beta,R,J,CovB,MSE,ErrorModelInfo] = nlinfit(x, i(:), i_fcn, b_estd);
i_fit2 = i_fcn(beta,x); % Generate Fit Line
i_mtx = reshape(i, 7, 7)';
i_fit2_mtx = reshape(i_fit2, 7, 7)';
figure(1)
surf(Trm, tdm, i_mtx)
hold on
stem3(Trm, tdm, i_fit2_mtx)
hold off
grid on
xlabel('T_r')
ylabel('t_m')
zlabel('\iti\rm (\itT_r,t_d\rm)')
Anand Shirke
Anand Shirke on 25 Jan 2019
Dear Star Strider,
I have a similar problem where I have to estimate the Constants. I tried your same code and am facing some errors. I am attaching the code and error picture.
Please help.
Trv = [2 5 10 15 20 25 50];
tdv = [5 10 15 20 25 30 60];
i=[112.8353 145.7318 166.5839 178.0802 186.0354 192.1177 210.6576 ; 14.9915 19.3622 22.1326 23.6600 24.7170 25.5251 27.9883 ; 19.3381 24.9760 28.5498 30.5200 31.8834 32.9258 36.1033 ; 22.3542 28.8714 33.0025 35.2800 36.8561 38.0611 41.7341 ; 25.1041 32.4230 37.0623 39.6200 41.3899 42.7432 46.8680 ; 27.5879 35.6310 40.7292 43.5400 45.4851 46.9722 51.5051 ; 38.1440 49.2647 56.3137 60.2001 62.8893 64.9454 71.2129];
Tr = linspace(min(Trv), max(Trv), length(i));
td = linspace(min(tdv), max(tdv), length(i));
i_fcn = @(b,x) b(1).*x(:,2).^b(2) ./ (b(3) + x(:,1)).^b(4);
x = [td(:) Tr(:)];
b0 = rand(4,1);
SSECF = @(b) sum((i(:) - i_fcn(b,x)).^2);
[b_estd, SSE] = fminsearch(SSECF, b0);
i_fit = i_fcn(b_estd,x);
AjithNLM.JPG

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!