Fitting data from file

3 views (last 30 days)
Piotr on 27 Sep 2022
Commented: Rik on 30 Sep 2022
I am trying to fit data from a file. For that I have created data with simply a sine wave function. Here is my code:
load test_fit
Error using load
Unable to find file or directory 'test_fit.txt'.
x = test_fit(:,1);
y = test_fit(:,2);
But the results I got are clearly not good:
I'm just wondering if I should specify somwhere the number of itterations or the fitting constrains?
Thank you in advance for any help or comments!

Accepted Answer

Rik on 27 Sep 2022
Edited: Rik on 27 Sep 2022
You can specify those parameters, but what is much more effective is providing good initial guesses (and not mixing up x and y):
x = test_fit(:,1);
y = test_fit(:,2);
fo=fit(x,y,'a*sin(b*x+c)+d','StartPoint',[1 4 0 2])
fo =
General model: fo(x) = a*sin(b*x+c)+d Coefficients (with 95% confidence bounds): a = 1 (1, 1) b = 4 (4, 4) c = 0.3927 (0.3927, 0.3927) d = 2 (2, 2)
hold on,plot(fo)
Rik on 30 Sep 2022
If my answer helped you, please consider marking it as accepted answer.

Sign in to comment.

More Answers (0)


Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by