You can use the nlinfit
tool, which doesn't require the Curve Fitting Toolbox (I don't think...)
Something like
f = @(b,x)(20 + b(1)*x + b(2)*exp(b(3)*2*x));
beta0 = [1, 1, 1];
beta = nlinfit(x, Y, f, beta0);
When MATLAB solves this least-squares problem, it passes the coefficients into the anonymous function f
in the vector b
. nlinfit
returns the final values of these coefficients in the beta
vector. beta0
is an initial guess of the values of b(1)
, b(2)
, and b(3)
. x
and Y
are the vectors with the data that you want to fit.
Alternatively, you can define the function in its own file, if it is a little more complicated. For this case, you would have something like (in the file my_function.m
)
function y = my_function(b,x)
y = 20 + b(1)*x + b(2)*exp(b(3)*2*x);
end
and the rest of the code would look like
beta0 = [1, 1, 1];
beta = nlinfit(x, Y, @my_function, beta0);
See also: Using nlinfit in Matlab?