Fitting data from file

3 views (last 30 days)
Piotr
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:
load test_fit
Error using load
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?
Thank you in advance for any help or comments!

Accepted Answer

Rik
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):
test_fit=load(websave('test_fit.txt','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137860/test_fit.txt'));
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)
  10 Comments
Rik
Rik on 30 Sep 2022
If my answer helped you, please consider marking it as accepted answer.

Sign in to comment.

More Answers (0)

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!