Strange results using Curve Fitting tool

Hello,
I'm trying to fit a function of the type "a1*cos(b1*x+c1) + a2*cos(b2*x+c2)" to my data using the curve fitting tool -> custom equation. However, the fitting results are terrible (since I know the data, I know the results should be reasonably good).
Thus, I tested the function "a1*sin(b1*x+c1) + a2*sin(b2*x+c2)" and the results remain terrible! The weirdest thing, though, is that when I use the "sum of sine" option (which is the exact same thing) the results are great, as expected.
Any ideas?

2 Comments

If the "sum of sine" option worked, why do you still require a custom solution?
To verify if I was inputting the custom function correctly.

Sign in to comment.

Answers (1)

Matt J
Matt J on 1 Nov 2020
Edited: Matt J on 1 Nov 2020
Not many, since without your data and code, we cannot reproduce what you are seeing. One possible culprit though is the initial guess of the parameters. If you're going to use a custom model, you need to supply your own strategy for selecting an initial guess, since Matlab has no educated way of choosing one automatically for a custom equation. Since you did not mention any such strategy, I can only assume you just let the curve fitter pick an arbitrary initial guess, and that can certainly lead to poor results.
Also, I would recommend that you parametrize the equation terms asymmetrically, so that the solver can distinguish between the them, e.g., "a1*cos(b1*x+c1) + (a1+a2)*cos(b2*x+c2)"

5 Comments

It's possible it's due to the initial parameters. I'm going to look into that. Thanks
Sergio
Sergio on 1 Nov 2020
Edited: Sergio on 1 Nov 2020
Well, unless I'm doing something wrong, it does not seem to be the initial parameters either.
To make things clearer: using the Curve Fitting tool app, I get an R-square = 0.9986 for the model f(x) = a1*sin(b1*x+c1) + a2*sin(b2*x+c2).
Now, when I try to build a custom model with that exact same function, i.e.:
ft = fittype('a1*sin(b1*x+c1)+a2*sin(b2*x+c2)','independent','x');
or
ft = fittype(@(a1,b1,c1,a2,b2,c2,x) a1*sin(b1*x+c1)+a2*sin(b2*x+c2));
Then
[model,gof,output]= fit(x,y,ft);
I get an R-square close to zero.
Even when I force the initial parameters close to the optimal ones obtained using the app (" 'start',[1.1 0.73 -2.8 0.87 0.06 3] "), the results are still terrible.
Thanks in advance.
No, the app yields a great fit.
The problem is when I try to reproduce it using the command line.
Bear in mind that I’m only doing this to make sure I’m using the correct sintaxes. Once done, I’ll use other custom functions to fit my data.
Thiw worked,
>> ffit=fit(x,y,ft,'StartPoint',[1 1 0.74 0.064 -2.797 3.059] );
>> plot(ffit,x,y);scf
It sure did! It was the order of the starting parameters, right?
Thanks a lot for your help!

Sign in to comment.

Categories

Asked:

on 1 Nov 2020

Edited:

on 1 Nov 2020

Community Treasure Hunt

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

Start Hunting!