Curve fitting with a general function

Hello Everyone,
I am working on a project and I need to fit a curve with a general function. I am not sure if it can be done in Matlab. I did some search. It looks like Matlab can work if the function can be solved as a y=f(x). But what if a function can not be solved analytically like y=A*x*sin(x+B*y) (A,B are parameters). How can I use this kind of functions to do curve fitting and get parameters A,B? Thanks in advance for any help you can provide.

Answers (3)

Matt J
Matt J on 15 Aug 2013
Edited: Matt J on 15 Aug 2013
It should be straightfoward with LSQNONLIN or FSOLVE, if you have the Optimization Toolbox. The objective function would be the residual,
f(A,B) = y-A*x.*sin(x+B*y)
Or, even if you don't have the toolbox, FMINSEARCH shouldn't be too bad, with only 2 unknonws
f=@(AB) norm(y-AB(1)*x.*sin(x+AB(2)*y);
fminsearch(f, AB_guess);
If there is no known equation that you believe you can fit your data to, then you can use a spline fit, where you have a new cubic equation between every pair of points. It sounds more complicated than it is. People use them all the time. It's a numerical description rather than an analytical description. Or you might try something like SLM: http://www.mathworks.com/matlabcentral/fileexchange/24443
Matt J
Matt J on 15 Aug 2013
Edited: Matt J on 15 Aug 2013
Matlab can work if the function can be solved as a y=f(x). But what if a function can not be solved analytically like y=A*x*sin(x+B*y)
Even though 'y' appears on both the left and right hand side, you can probably make any "y=f(x) curve fitter" work by replacing the right-hand y with an artificial 3rd variable z
y=A*x*sin(x+B*z)
So, now you have y=f(x,z). As long as you feed z-data equal to the y-data to the curve fitting algorithm, it will see the exact same residuals (difference between left and right hand side) for every pair A,B that it explores. So, I think that from the point of view of the fitting software, you will get the same result as with y=A*x*sin(x+B*y).

2 Comments

Thank you Matt. This method is very clever, but it looks like it is hard to converge. I am trying to test LSQNONLIN, FSOLVE, and FMINSEARCH with the function f(A,B,C)=y-A*B*(expm(A*(x+C*y))). x and y are the column arrays from the experimental data. There is an error in defining function: Error in MuPAD command: The number of arguments is incorrect.
Another error in FMINSEARCH: Error using * Inner matrix dimensions must agree.
Matt J
Matt J on 15 Aug 2013
Edited: Matt J on 15 Aug 2013
There is an error in defining function: Error in MuPAD command: The number of arguments is incorrect.
You shouldn't be using (or have need for) any Symbolic Toolbox commands here.
x and y are the column arrays...Error using Inner matrix dimensions must agree.
I assume this error is really coming from EXPM. If x and y are column vectors and A,B,C are scalars, then EXPM won't work. Are you sure EXP isn't the appropriate thing here?

Sign in to comment.

Categories

Asked:

on 15 Aug 2013

Community Treasure Hunt

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

Start Hunting!