Minimum least square fitting with multiple variable

2 ビュー (過去 30 日間)
Darlington Mensah
Darlington Mensah 2018 年 12 月 25 日
編集済み: Stephan 2018 年 12 月 25 日
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

回答 (1 件)

Stephan
Stephan 2018 年 12 月 25 日
編集済み: Stephan 2018 年 12 月 25 日
Hi,
you could use the resnorm to compare the quality of different approaches:
[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

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by