Minimum least square fitting with multiple variable

조회 수: 4 (최근 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 CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by