Can I pass 2 equations into nlinfit function?

Hello, I am using the nlinfit function to estimate parameters (constants) for an equation with my data. In fact I have 2 different equations that depend on the same constants. Is there a way for me to input 2 different functions and from the nlinfit function retrieve values for the constants that provide the best fit over the two equations? Hopefully my question makes sense.
For example:
equation 1: y1= A*B*x / (1 + B*x + C)
equation 2: y2=A*B*x / (1 + B*x + C)^2
My data is for y1 and y2 as functions of x

 Accepted Answer

If I remember correctly, nlinfit cannot, however lsqcurvefit can:
This works:
x = 1:10;
y1 = rand(1,10);
y2 = rand(1,10);
objfcn = @(b,x) [b(1)*b(2).*x./(1 + b(2).*x + b(3)); b(1)*b(2).*x./(1 + b(2).*x + b(3)).^2];
B = lsqcurvefit(objfcn, rand(3,1), x, [y1;y2]);
Make appropriate changes to work with your data.
.

4 Comments

Thank you! I will give this a try.
My pleasure!
Note that if ‘y1’ and ‘y2’ (and ‘x’) are column vectors, two changes are necessary:
objfcn = @(b,x) [b(1)*b(2).*x./(1 + b(2).*x + b(3)), b(1)*b(2).*x./(1 + b(2).*x + b(3)).^2];
and:
[y1, y2]
That should work.
Thank you, I had the time to go back to my code today to make this change and it looks to do what I had hoped for!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!