nonlinear and linear regression
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Mhmmd Sjj
il 15 Feb 2021
Risposto: William Rose
il 30 Mar 2021
I have created a script plus a function to use for nonlinear least square optimization. I have compared it with linear regression and also with built-in functions in MATLAB such as fminsearch,fminunc, lsqnonlin. The results for all regression models are surprisingly the same and I don't know why. Can anyone help me with that please?Here is my function:
function result = NonLsq(w,x,y)
ei = -(w(1).*x+w(2))+y;
result = sum(ei.^2);
end
And the following is my main script:
clc; clear; close all;
% { Linear And Nonlinear Curvefitting}
%% 1. One Dimensional data
x = [0.5 1 2 3 4];
y = [10.4 5.8 3.3 2.4 2];
xMin = min(x);
xMax = max(x);
n = 100; % Number of data which sould be interpolated
xInterp = linspace(xMin,xMax,n);
yInterp1 = interp1(x,y,xInterp);
yInterp2 = interp1(x,y,xInterp,'spline');
%% 2. NonLinear Least Square
% Initial Guess
g = @(w,x,y) (w(1).*x+w(2))-y;
X01 = [0.15 0.55];
X02 = [0.4 0.8];
X03 = [0.7 48];
% X0 = [0.15 0.55]';
Options1 = optimset('Display','Iter','TolX',1e-5);
Options2 = optimset('Display','on');
Options3 = optimset('MaxIter',50,'TolFun',1e-4);
p1_Nonlin = fminsearch(@NonLsq,X01,Options1,x,y);
p2_Nonlin = fminunc(@NonLsq,X02,Options2,x,y);
p3_Nonlin = lsqnonlin(g,X03,[],[],Options3,x,y);
plot(x,y,'o','MarkerSize',8,'LineWidth',3,'MarkerFaceColor','k');
hold on
grid on
plot(xInterp,yInterp1,'r--','LineWidth',2)
hold on
plot(xInterp,yInterp2,'b:','LineWidth',2)
legend('Spline INterpolated')
hold on
plot(xInterp,pLinear_Interp,'k*','LineWidth',2)
plot(xInterp,P1_Nonlin_Interp,'c.','LineWidth',2,'MarkerSize',12)
hold on
plot(xInterp,P2_Nonlin_Interp,'m','LineWidth',2)
hold on
plot(xInterp,P3_Nonlin_Interp,'g','LiNEwidth',2)
legend('Original Data','Linear Interpolatn','Linear Spline','Linear Regression'...
,'FminSearch','FminUnc','LsqNonLinear')
Could it be related to the function I'm trying to optimize?
0 Commenti
Risposta accettata
William Rose
il 30 Mar 2021
You get the same results because your model g() is linear in w(1) and w(2):
g = @(w,x,y) (w(1).*x+w(2))-y;
The error function NonLsq() for the noninear case uses the same linear model. Thus linear and nonlinear fits find the same solution.
By the way, you do not need the dot-multiply in ei=-(w(1).*x+w(2))+y. Since w(1) is a scalar, you can do ei=-(w(1)*x+w(2))+y. The same is true for the deifnition of g().
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Systems of Nonlinear Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!