Minimum least square fitting with multiple variable
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Stephan
am 25 Dez. 2018
Bearbeitet: Stephan
am 25 Dez. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!