Fitting data from file
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
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!
0 commentaires
Réponse acceptée
Rik
le 27 Sep 2022
Modifié(e) : Rik
le 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])
hold on,plot(fo)
10 commentaires
Rik
le 30 Sep 2022
If you look at the code below, you see that c starts with a NaN. So we should remove it.
I = imread(websave('testf5.png','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1139785/testf5.png'));
x = [size(I,2)/2 size(I,2)/2];
y = [0 size(I,1)];
c = improfile(I,x,y);
c = c(:,1,1); % select red chanel
x_adapted = linspace(y(1),y(2),numel(c));
x_adapted = x_adapted.'; % transpose 1x1491 to 1491x1
subplot(2,1,1)
imshow(I)
hold on
plot(x,y,'-o')
hold off
subplot(2,1,2)
plot(x_adapted,c)
ft = fittype('m*sin(n*x+o)+p');
options=fitoptions(ft);
options.StartPoint = [125 100 0 125];
options.Weights = [1,1];
find(isnan(x_adapted)),find(isnan(c))
Here is the problem, so we'll have to remove that point.
L = isnan(x_adapted) | isnan(c); % the first part is just good habit
x_adapted(L) = [];
c(L) = [];
% Now we can safely to the fit:
fo=fit(x_adapted,c,ft,StartPoint=[125 100 0 125])
figure,plot(fo)
So the fit is not great, you will have to adjust the start point.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Interpolation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!