Non-linear data fit with multiple constants

I have a non-linear equation with 3 variables I wish to be constants and only 1 variable to be fitted to x,y data.
Using fittype, I have entered the equation and tried to specify b, c & d as constants. However, the options 'problem' only seems to allow single variables to be specified as constants
ft = fittype(@(a, b, c, d,x) a*c*x./(1+c*x) + b*d*x./(1+d*x),'problem','b,c,d');
Error using fittype>iAssertValidVariableNames
The name '[b,c,d]' is not a valid MATLAB variable name.
In the next part I use "fit" to fit the data, and specify the value of the 3 constants and starting point of the fit:
f = fit(p(1:index),q(1:index),ft,'problem',[Qm2 b1 b2],'StartPoint',Qm1);
Is there another way to specify constants in the fittype?

4 Comments

If b,c and d are known constants, why don't you simply specify them with their numerical values in the fittype function ?
I've just learnt to specify them as cell arrays so using curly brackets:
ft = fittype(@(a, b, c, d,x) a*c*x./(1+c*x) + b*d*x./(1+d*x),'problem',{'b','c','d'});
This seems to work. They are variables I want constant for the fit, but they are variable in the rest of the code so can't just put in numerical values.
Or like this:
xfit = (0:0.1:1).';
yfit = 3*xfit + 0.01*(2*rand(11,1)-1);
b = 4;
fun = @(a,b,x) a/b*x;
ft = fittype( @(a,x) fun(a,b,x));
f = fit(xfit,yfit,ft)
Warning: Start point not provided, choosing random start point.
f =
General model: f(x) = fun(a,b,x) Coefficients (with 95% confidence bounds): a = 12.02 (11.99, 12.05)
Thanks! That's a neat solution.

Sign in to comment.

Answers (1)

Matt J
Matt J 31 minutes ago
Edited: Matt J 14 minutes ago
Though your post's title says the fitting problem is nonlinear, it is actually linear when b and d are known, and so has a simple analytical solution. When c is also known the solution is even simpler:
x=x(:);
p=c*x./(1+c*x);
q=(b*d)*x./(1+d*x);
a=p\(y(:)-q); %fit result

Categories

Products

Release

R2022b

Asked:

on 14 May 2026 at 13:24

Commented:

about 6 hours ago

Community Treasure Hunt

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

Start Hunting!