How do i use the nlinfit function when there are two independent variables?

22 views (last 30 days)
v0=b(1).*A.*B/((b(2).*b(3))+(b(3).*A)+(A.*B))
This is my model. I have two independent variables A and B.v0 is the dependent variable. Now can you please help me with writing the line code for this in matlab where i have to use a nlinfit function.
For one independent variable the following code worked:
model=@(b,x) b(1).*substrate./(b(2)+substrate);
initialguess=[1 1];
[beta,R,J,CovB,MSE] = nlinfit(substrate,vo,model, initialguess);
I want to do the same thing for my above equation.Please suggest.
THANK YOU.

Accepted Answer

Star Strider
Star Strider on 23 Jun 2014
You have to pass your independent variable as a single value. If you have two independent variables, simply create a matrix from them, and make corresponding changes in your function:
AB = [A B]; % Assumes A and B are COLUMN vectors
v0=b(1).*AB(:,1).*AB(:,2)/((b(2).*b(3))+(b(3).*AB(:,1))+(AB(:,1).*AB(:,2)))
Make appropriate changes if they are row vectors, or simply transpose the row vectors to column vectors. (The nlinfit function doesn’t care. It just wants your function to return a vector that matches your dependent variable vector dimensions.)
Your call to nlinfit then becomes:
[beta,R,J,CovB,MSE] = nlinfit(AB, ...

More Answers (1)

Himaja
Himaja on 24 Jun 2014
Edited: Himaja on 24 Jun 2014
hello, I have tried using the syntax but im a bit confused.I will post my code here can u suggest as to where im going wrong?
% Assign the data vectors 2
A=[1.814966667 2.527168776 3.156463768 4.883318386 6.701415385 7.686917647 8.296990476];
B=[7.661333333 7.060064135 6.528788406 5.070909417 3.536 2.704 2.188952381];
vo=[25.9 35.6 39.2 27.9 20 10.7 8.5];
% Plot the raw data to get a look at the shape
figure(1)
clf
plot(A,vo,'o')
xlabel('Substrate Concentration (mM)','FontSize',14)
ylabel('Initial rate (mM/s)','FontSize',14)
title('Initial Rate vs. Concentration','FontSize',16)
print -depsc rawdataplot
figure(2)
clf
plot(B,vo,'o')
xlabel('Substrate Concentration (mM)','FontSize',14)
ylabel('Initial rate (mM/s)','FontSize',14)
title('Initial Rate vs. Concentration','FontSize',16)
print -depsc rawdataplot
% Calculate unknown coefficients in the model using nlinfit
AB=[A B]; % Assumes A and B are COLUMN vectors
model=@(b,AB)b(1).*AB(:,1).*AB(:,2)/((b(2).*b(3))+(b(3).*AB(:,1))+(AB(:,1).*AB(:,2)));
initialguess=[1 1];
[beta,R,J,CovB,MSE] = nlinfit(AB,vo,model, initialguess);
% 95% CI of coeffieicents
betaci = nlparci(beta,R,J);
% Plot the nonlinear fit with the raw data
figure(3)
plot(A,vo,'o')
hold on
A=0:0.1:10;
B=0:0.1:10;
AB=A.*B;
f=beta(1).*AB(:,1).*AB(:,2)/((beta(2).*beta(3))+(beta(3).*AB(:,1))+(AB(:,1).*AB(:,2))) ;
plot(AB,f,'-')
xlabel('Substrate Concentration A (mM)','FontSize',14)
ylabel('Initial rate (mM/s)','FontSize',14)
title('Initial Rate vs. Concentrtaion A','FontSize',16)
legend('Data Points','Nonlinear Fit',0)
print -depsc rawdatawithfit
% Make a residual plot
figure(4)
clf
plot(AB,0,'-',A,R,'rp')
xlabel('Substrate Concentration (mM)','FontSize',14)
ylabel('Residuals','FontSize',14)
title('Residual Plot','FontSize',16)
print -depsc nonlinresiduals
% Compute the r^2 value
ssresnonlin=sum(R.^2);
sstotnonlin=sum((vo-mean(vo)).^2);
rsqrnonlin=1-(ssresnonlin/sstotnonlin);
disp(beta(1));
disp(beta(2));
disp(beta(3));
this is the non linear regression to fit my data into the formula i have sent you before. I would be thankful to u if u would tell me my mistake. Thanks.
  3 Comments
GAGANDEEP KAUR
GAGANDEEP KAUR on 26 Dec 2020
May I please get some suggestion for multivariable output case. As I have 1 input variable(say mole fraction x) and 9 outputs e.g. say activity coefficient of 9 species yk1,yk2,yk3.........yk9, are in form of vactors, that are obtained and has to be fitted to obtain unknown parameters
Star Strider
Star Strider on 26 Dec 2020
GAGANDEEP KAUR —
The nlinfit function cannot (to my knowledge) fit more than 1 dependent variable. However, the lsqcurvefit function can.
Some restrictrions remain with respect to lsqcurvefit however, since confidence intervals on the parameters are possible with more than 1 dependent variable and only with unconstrained parameter estimates with nlparci, although it is not possible to calculate confidence intervals on the fit with more than 1 dependent variable with nlpredci.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!