# Constrained Non linear least squares returns equation parameters much different from true set

11 views (last 30 days)
Yaron Aviezer on 1 Sep 2020
Commented: Yaron Aviezer on 1 Sep 2020
Hello all,
I'm pacticing 3-4 parametrs fitting for kinetic rate equations, started with a simple example and looked at lsqnonlin option.
I'm getting results that are fare from my initial set even for different initial guess.
Also checked the levenberg-marquardt option (It is unconstrained).
Thank you,
YA
zzx % For cleaning = clc +clear all
n=50;
x = linspace(0.5,4.1,n);
y=linspace(0.5,4.1,n);
p0=[1.75 1.4 2.5 1.8];
z=zeros(n);
k=0;
w=[x,y];
for i = 1:n
for j=1:n
k=k+1;
z(i,j)=p0(1)*x(i)^p0(2)+(y(j)^p0(3))+p0(4);
t(k)=z(i,j);
end
end
nexttile
surf(x,y,z)
title('True plot')
t0=t;
t=t0+randn(size(t0))*0.1;
fun=@(p)p(1)*w(1)^p(2)+(w(2)^p(3))+p(4)-t;
p00=[1.8 1.1 2.1 1.3];
lb=[1 1 2 0];
ub=[4 5 5 2];
options = optimoptions('lsqnonlin','Display','iter');
p=lsqnonlin(fun,p00,lb,ub,options)
for i = 1:n
for j=1:n
k=k+1;
z1(i,j)=p(1)*x(i)^p(2)+(y(j)^p(3))+p(4);
t(k)=z(i,j);
end
end
nexttile
surf(x,y,z1)
title('Regression plot')

Alan Weiss on 1 Sep 2020
I don't know exactly where your error is, but I reworked your code, and in my reworked version lsqnonlin returns the correct answer.
N = 50;
v = linspace(0.5,4.1,N);
[X,Y] = meshgrid(v);
p0=[1.75 1.4 2.5 1.8];
Z = p0(1)*X.^p0(2) + (Y.^p0(3)) + p0(4);
nexttile
surf(X,Y,Z)
title('True plot')
t = Z + randn(size(Z))*0.1;
fun = @(p)p(1)*X.^p(2)+(Y.^p(3)) + p(4) - t;
p00 = [1.8 1.1 2.1 1.3];
lb = [1 1 2 0];
ub = [4 5 5 2];
options = optimoptions('lsqnonlin','Display','iter');
p = lsqnonlin(fun,p00,lb,ub,options)
Z2 = p(1)*X.^p(2) + (Y.^p(3)) + p(4);
nexttile
surf(X,Y,Z2)
title('Regression plot')
Alan Weiss
MATLAB mathematical toolbox documentation

#### 1 Comment

Yaron Aviezer on 1 Sep 2020
The difference is the full matrix form.
Looks that it works well also in unconstrained version.
Thank you sir

R2020a

### Community Treasure Hunt

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

Start Hunting!