简体   繁体   中英

Fitting data in least square sense to nonlinear equation

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM