Minimum least square fitting with multiple variable
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello
I am trying to fit my data with a linear trend using MLS fitting. However, I don't understand how the initial guess affects the final results. I realized that by changing the initial guess from x0 = [1 1], the result of my variable change and I'm confused about deciding which one gives me the best fitting.
My working code and data are found below
data = readtable('data.xlsx');
xdata = table2array(data(:,1));
ydata = table2array(data(:,3));
x = linspace(min(xdata), max(xdata));
fun = @(a)a(1).*xdata + a(2) - ydata;
x0 = [1 1];
x1 = lsqnonlin(fun, x0);
figure
plot(xdata,ydata,'o')
hold on
plot(x, x1(1).*x + x1(2))
hold off
0 commentaires
Réponses (1)
Stephan
le 25 Déc 2018
Modifié(e) : Stephan
le 25 Déc 2018
Hi,
[x, resnorm] = lsqnonlin(...)
But the question is, why do you use a nonlinear approach for a linear problem? The kind of problem you have is usually solved optimal by mldivide (optimal in sense of least squares):
data = sortrows(readtable('data.xlsx'));
xdata = table2array(data(:,1));
ydata = table2array(data(:,3));
xdata(:,2)=1;
x = xdata\ydata;
scatter(xdata(:,1),ydata,'or')
hold on
plot(xdata(:,1), x(1).*xdata(:,1)+x(2))
hold off
fprintf('Results:\nx(1)=%.15f\nx(2)=%.9f',x(1),x(2))
Best regards
Stephan
0 commentaires
Voir également
Catégories
En savoir plus sur Linear and Nonlinear Regression 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!