# Fitting data from file

3 views (last 30 days)
Piotr on 27 Sep 2022
Commented: Rik on 30 Sep 2022
Hello,
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:
Unable to find file or directory 'test_fit.txt'.
x = test_fit(:,1);
y = test_fit(:,2);
fo=fit(y,x,'a*sin(b*x+c)+d');
plot(fo,y,x)
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?

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);
plot(x,y,'*')
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

### Categories

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