Main Content

# 不含和含雅可比矩阵的非线性最小二乘

`$\sum _{k=1}^{10}{\left(2+2k-{e}^{k{x}_{1}}-{e}^{k{x}_{2}}\right)}^{2},$`

`${F}_{k}\left(x\right)=2+2k-{e}^{k{x}_{1}}-{e}^{k{x}_{2}},$`

### 求解不含雅可比矩阵的问题

```x0 = [0.3,0.4]; % Starting guess [x,resnorm,res,eflag,output] = lsqnonlin(@myfun,x0); % Invoke optimizer```
```Local minimum possible. lsqnonlin stopped because the size of the current step is less than the value of the step size tolerance. ```

`disp(x)`
``` 0.2578 0.2578 ```
`disp(resnorm)`
``` 124.3622 ```
`disp(output.funcCount)`
``` 72 ```

### 求解包含雅可比矩阵的问题

`${J}_{kj}\left(x\right)=\frac{\partial {F}_{k}\left(x\right)}{\partial {x}_{j}}.$`

`${F}_{k}\left(x\right)=2+2k-{e}^{k{x}_{1}}-{e}^{k{x}_{2}},$`

`$\begin{array}{l}{J}_{k1}\left(x\right)=-k{e}^{k{x}_{1}}\\ {J}_{k2}\left(x\right)=-k{e}^{k{x}_{2}}.\end{array}$`

`opts = optimoptions(@lsqnonlin,'SpecifyObjectiveGradient',true);`

```lb = []; % No bounds ub = []; [x2,resnorm2,res2,eflag2,output2] = lsqnonlin(@myfun2,x0,lb,ub,opts);```
```Local minimum possible. lsqnonlin stopped because the size of the current step is less than the value of the step size tolerance. ```

`disp(x2)`
``` 0.2578 0.2578 ```
`disp(resnorm2)`
``` 124.3622 ```

`disp(output2.funcCount)`
``` 24 ```

### 辅助函数

```function F = myfun(x) k = 1:10; F = 2 + 2*k-exp(k*x(1))-exp(k*x(2)); end```

```function [F,J] = myfun2(x) k = 1:10; F = 2 + 2*k-exp(k*x(1))-exp(k*x(2)); if nargout > 1 J = zeros(10,2); J(k,1) = -k.*exp(k*x(1)); J(k,2) = -k.*exp(k*x(2)); end end```