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 comentarios

Torsten
Torsten el 14 de Mayo de 2026
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.
Torsten
Torsten el 14 de Mayo de 2026
Editada: Torsten el 14 de Mayo de 2026
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)
Lawrence
Lawrence el 15 de Mayo de 2026
Thanks! That's a neat solution.

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 14 de Mayo de 2026
Editada: Matt J el 15 de Mayo de 2026
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=p(:); y=q(:);
x=x(1:index); y=y(1:index);
P=c*x./(1+c*x);
Q=(b*d)*x./(1+d*x);
ft = fittype(@(a, b, c, d,x) a*c*x./(1+c*x) + b*d*x./(1+d*x),'problem',{'b','c','d'});
f=cfit(ft, P\(y(:)-Q)) ; %fit result

Categorías

Productos

Versión

R2022b

Etiquetas

Preguntada:

el 14 de Mayo de 2026

Editada:

el 15 de Mayo de 2026

Community Treasure Hunt

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

Start Hunting!

Translated by