MATLAB: Non linear least squares for a system of equations

lsqnonlinsystem of equations

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

Best 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.