Not all equations will match the functional form of the default equations, nor can they all be easily expressed by the simple character syntax used on the Nonlinear Regression page. For more complicated equations we can instead define a custom function and tell MATLAB to use that instead.
As an example of more complicated function, consider the pH step response of a first-order system with an integrator,
where t is time, and Kp and tau are the fit coefficients. The first thing to do is create a function that returns a value for Y given t, Kp and τ. Note that the FIT function requires the dependent (response) variable to be defined as y and the independent (predictor) variable to be assigned x.
To use MATLAB to fit the equations Kp and tau to the above equation requires two steps:
Create a function for the equation (this needs to be its own separate function file).
Pass the function to the FIT function (you can do this in a script or another function).
The output will be the same kind of fit object that we've seen before so you can plot it as usual. For the above example we'd have something like this for the function file:
function Y = myFitFunc(x, K, tau)
Y = 40*K.*(x - tau.*(1 - exp(-x./tau)));
end
And for the fitting procedure itself:
t = [0 1 2 3 4 5 6 7 8]; % timestamps of measured pH data
pH = [7.62 7.67 7.85 8.24 8.72 9.37 10.07 10.80 11.34]; % measured pH data
pH = pH - pH(1); % control theory always works in deviation variables
ft = fittype('myFitFunc(x, K, tau)');
fobj = fit(t', pH', ft, 'StartPoint', [1 1])
fobj =
General model:
fobj(x) = myFitFunc(x, K, tau)
Coefficients (with 95% confidence bounds):
K = 0.02579 (0.0141, 0.03748)
tau = 5.678 (1.857, 9.499)