Clear Filters
Clear Filters

Curve Fitting for a function with 3 fitting parameters.

23 views (last 30 days)
I have imported a file in MATLAB which has 2 columns. One column has data of time (t) and other column has the data of ratio. I have a function which is (At^2)/((B(e)^Ct)-1). A,B,C are fitting parameters. I have plotted the curve for ratio. I want MATLAB to find the values of ABC considering different times t such that the curve of the ratio and the curve of the function, fit. The outcome looks something like this. The red dots is the curve for ratio and I want MATLAB to generate the blue curve which fits when A,B,C are solved. I have seen curve fitting documentation but fixed functions are used. I have a different function to use so please help me out.

Answers (3)

Bjorn Gustavsson
Bjorn Gustavsson on 18 Dec 2020
Edited: Bjorn Gustavsson on 18 Dec 2020
For this I typically use standard non-linear least-square-fitting. That only requires some optimisation-routine, like fminsearch. Something like this should work:
curve_fcn = @(ABC,t) (ABC(1)*t.^2)./(ABC(2)*exp(ABC(3)*t)-1);
err_fcn = @(par,t,y,fcn) sum((y-fcn(par,t)).^2);
res_fcn = @(par,t,y,fcn) (y-fcn(par,t)); % if you have the optimisation toolbox lsqnonlin is often preferable
ABC0 = [1 1 1]; % Some sensible start-guess for the optimisation.
ABSbest = fminsearch(@(ABC) err_fcn(ABC,t,y,curve_fcn),ABC0);
% Test example:
y0 = curve_fcn([1,2,1/4],t) + 0.5*randn(size(t));
ABC0 = [1 1 1];
ABCbest = fminsearch(@(ABC) err_fcn(ABC,t,y0,curve_fcn),ABC0);
If you have the optimization toolbox the lsqnonlin function is often much faster. If you know your data you also know the measurement errors and their statistics, then you might also strongly benefit from a properly weighted least-square fit, or if the measurements have a not-normal-distribution optimizing the proper likelihood-function.
HTH

Daniel Pollard
Daniel Pollard on 18 Dec 2020
Bjorn's method will work. I'm just offering a second option, from the file exchange:
This has worked for me, it's a bit of a learning curve (although that could just be me), but works great.

Matt J
Matt J on 18 Dec 2020
Edited: Matt J on 18 Dec 2020
Also useful,
With this tool, you would effectively eliminate "A" as an unknown.

Community Treasure Hunt

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

Start Hunting!