Question

I need help fitting data in a least square sense to a nonlinear function. Given data, how do I proceed when I have following equation?

f(x) = 20 + ax + b*e^(c*2x)

So I want to find a,b and c. If it was products, I would linearize the function by takin the natural logaritm all over the function but I can not seem to do that in this case.

Thanks

Was it helpful?

Solution

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?

OTHER TIPS

You can try the cftool which is an interactive tool for fitting data. The second part I don't quite understand. It may help if you describe it in more detail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top