# 具有雅可比矩阵的大型稀疏非线性方程组

`$\begin{array}{l}F\left(1\right)=3{x}_{1}-2{x}_{1}^{2}-2{x}_{2}+1,\\ F\left(i\right)=3{x}_{i}-2{x}_{i}^{2}-{x}_{i-1}-2{x}_{i+1}+1,\\ F\left(n\right)=3{x}_{n}-2{x}_{n}^{2}-{x}_{n-1}+1.\end{array}$`

```n = 1000; xstart = -ones(n,1); fun = @nlsf1; [x,fval,exitflag,output] = fsolve(fun,xstart);```
```Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. ```

`disp(norm(fval))`
``` 2.7603e-13 ```
`disp(output.funcCount)`
``` 7007 ```

`fsolve` 可以准确地求解方程，但需要进行数千次函数计算。

```options = optimoptions('fsolve','SpecifyObjectiveGradient',true); [x2,fval2,exitflag2,output2] = fsolve(fun,xstart,options);```
```Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. ```
```options.Algorithm = 'trust-region'; [x3,fval3,exitflag3,output3] = fsolve(fun,xstart,options);```
```Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. ```
`disp([norm(fval2),norm(fval3)])`
``` 1.0e-08 * 0.0000 0.1065 ```
`disp([output2.funcCount,output3.funcCount])`
``` 7 5 ```

```options.PrecondBandWidth = 1; [x4,fval4,exitflag4,output4] = fsolve(fun,xstart,options);```
```Equation solved, inaccuracy possible. fsolve stopped because the vector of function values is near zero, as measured by the value of the function tolerance. However, the last step was ineffective. ```
`disp(norm(fval4))`
``` 3.1185e-05 ```
`disp(output4.funcCount)`
``` 6 ```
`disp(output4.cgiterations)`
``` 8 ```

`'PrecondBandWidth'` 选项设置导致 `fsolve` 给出稍微不太准确的答案，以残差范数来衡量。函数计算的数量略有增加，从 5 个增加到 6 个。求解器的解过程包含少于十次共轭梯度迭代。

```options.PrecondBandWidth = 0; [x5,fval5,exitflag5,output5] = fsolve(fun,xstart,options);```
```Equation solved, inaccuracy possible. fsolve stopped because the vector of function values is near zero, as measured by the value of the function tolerance. However, the last step was ineffective. ```
`disp(norm(fval5))`
``` 2.0057e-05 ```
`disp(output5.funcCount)`
``` 6 ```
`disp(output5.cgiterations)`
``` 19 ```

```options = optimoptions('fsolve','SpecifyObjectiveGradient',true,'Algorithm','levenberg-marquardt'); [x6,fval6,exitflag6,output6] = fsolve(fun,xstart,options);```
```Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. ```
`disp(norm(fval6))`
``` 5.2545e-15 ```
`disp(output6.funcCount)`
``` 6 ```

```t = table([norm(fval);norm(fval2);norm(fval3);norm(fval4);norm(fval5);norm(fval6)],... [output.funcCount;output2.funcCount;output3.funcCount;output4.funcCount;output5.funcCount;output6.funcCount],... 'VariableNames',["Residual" "Fevals"],... 'RowNames',["Default" "Default+Jacobian" "Trust-Region+Jacobian" "Trust-Region+Jacobian,BW=1" "Trust-Region+Jacobian,BW=0" "Levenberg-Marquardt+Jacobian"])```
```t=6×2 table Residual Fevals __________ ______ Default 2.7603e-13 7007 Default+Jacobian 2.5891e-13 7 Trust-Region+Jacobian 1.0646e-09 5 Trust-Region+Jacobian,BW=1 3.1185e-05 6 Trust-Region+Jacobian,BW=0 2.0057e-05 6 Levenberg-Marquardt+Jacobian 5.2545e-15 6 ```

```function [F,J] = nlsf1(x) % Evaluate the vector function n = length(x); F = zeros(n,1); i = 2:(n-1); F(i) = (3-2*x(i)).*x(i)-x(i-1)-2*x(i+1) + 1; F(n) = (3-2*x(n)).*x(n)-x(n-1) + 1; F(1) = (3-2*x(1)).*x(1)-2*x(2) + 1; % Evaluate the Jacobian if nargout > 1 if nargout > 1 d = -4*x + 3*ones(n,1); D = sparse(1:n,1:n,d,n,n); c = -2*ones(n-1,1); C = sparse(1:n-1,2:n,c,n,n); e = -ones(n-1,1); E = sparse(2:n,1:n-1,e,n,n); J = C + D + E; end end```