MATLAB Answers

0

Non linear least squares for a system of equations

Asked by Sofia Philippou on 11 Sep 2019
Latest activity Commented on by Sofia Philippou on 1 Oct 2019
Hi, I want to estimate 3 parameters using non linear least squares (lsqnonlin) from a system of 3 equations. I know how to write the code for one equation but not sure how to specify for 3 equations?
This is what I am trying to do:
arg Min Σ(Y - X)^2 for 3 Ys and 3 Xs.
Y1 = exp(-x(2)*0.5)*Ydata+(1-exp(-x(2)))*x(1)+(x(3)^2
Y2 = exp(-x(2)*0.1)*Ydata+(1-exp(-x(2)))*x(1)+(x(3)^2
Y3 = exp(-x(2)*0.15)*Ydata+(1-exp(-x(2)))*x(1)+(x(3)^2
X1data = ...
[1,1.5,1.6 etc]
X2data = ...
[2.5, 2,7,2.8 etc]
X3data = ...
[2.7,2.8,3.0 etc]
Ydata = ...
[1.5,1.6,1.7 etc]
For 1 equation (Y1 & X1) I would have written the following code:
fun = @(x)exp(-x(2)*0.5)*Ydata+(1-exp(-x(2)))*x(1)+(x(3)^2-X1data;
x0 = [1.08, 0.4, 0.1];
x = lsqnonlin(fun,x0)
For 3 equations would this be the correct specification?
fun = @(x)exp(-x(2)*0.5)*Ydata+(1-exp(-x(2)))*x(1)+(x(3)^2-X1data+exp(-x(2)*0.1)*Ydata+(1-exp(-x(2)))*x(1)+(x(3)^2-X2data+exp(-x(2)*0.15)*Ydata+(1-exp(-x(2)))*x(1)+(x(3)^2-X3data;
x0 = [1.08, 0.4, 0.1];
x = lsqnonlin(fun,x0)
Many thanks in advance

  0 Comments

Sign in to comment.

1 Answer

Answer by Torsten
on 11 Sep 2019
 Accepted Answer

fun = @(z)[z(1)^0.5*Ydata+z(2)-X1data,...
z(1)^0.1*Ydata+z(2)-X2data,...
z(1)^0.15*Ydata+z(2)-X3data];
z0 = [1 1];
z = lsqnonlin(fun,z0)
where I set
z(1) = exp(-x(2))
z(2) = (1-exp(-x(2)))*x(1)+x(3)^2
So you need to fit in 2 instead of 3 unknown parameters.

  23 Comments

If Y1, Y2, Y3 and Ydata in your three equations from above stand for the logarithmized data, then no - it does not change anything.
Yes all the data are logs. Y1, Y2, Y3 and Xdata
Hi, not sure if I should post it as a separate question but will the code look like this if I want to estimate the 3 parameters not as a system of equations but as per below?NLLS.PNG
xdata =...
[1.39, 1.22, 1.25, 1.35, 1.35];
y1data = ...
[2.96, 2.90, 2.92, 2.96, 2.97];
y2data =...
[1.75, 1.77, 1.78, 1.80, 1.83];
y3data =...
[1.65, 1.69, 1.70, 1.74, 1.75];
y4data =...
[1.71, 1.68, 1.69, 1.74, 1.75];
y5data =...
[1.78, 1.845, 1.85, 1.87, 1.88];
lb = [0, 0, 0];
ub = [inf, inf, inf];
fun = @(x)[exp(-x(2)*3/12)*xdata+(1-exp(-x(2)*3/12))*x(1)+((x(3)^2)/(4*x(2)))*(1-exp(-2*x(2)*3/12))-y1data+exp(-x(2)*6/12)*xdata+(1-exp(-x(2)*6/12))*x(1)+((x(3)^2)/(4*x(2)))*(1-exp(-2*x(2)*6/12))-y2data+exp(-x(2)*9/12)*xdata+(1-exp(-x(2)*9/12))*x(1)+((x(3)^2)/(4*x(2)))*(1-exp(-2*x(2)*9/12))-y3data+exp(-x(2)*12/12)*xdata+(1-exp(-x(2)*12/12))*x(1)+((x(3)^2)/(4*x(2)))*(1-exp(-2*x(2)*12/12))-y4data+exp(-x(2)*18/12)*xdata+(1-exp(-x(2)*18/12))*x(1)+((x(3)^2)/(4*x(2)))*(1-exp(-2*x(2)*18/12))-y5data];
x0 = [2.08, 0.75, 0.3];
x = lsqnonlin(fun,x0,lb,ub)

Sign in to comment.