Octave で方程式を解く

# Octave で方程式を解く

## 1.　線形連立方程式

### 1.1.　正確な方法


```matlab
format long
A = [5 -1 3; -1 2 -2; 3 -2 3];
b = [7; -1; 4];
y = A \ b
```

    y =
    
       1.000000000000003
       0.999999999999994
       0.999999999999992
    


### 1.2.　繰り返し計算

#### 1.2.1.　共役勾配法（Conjugate Gradients Squared method） `cgs()`

    usage:
    x = cgs (A, b, rtol, maxit, M1, M2, x0)
    x = cgs (A, b, rtol, maxit, P)
    [x, flag, relres, iter, resvec] = cgs (A, b, …)
    
    A        正方行列
    b        定数ベクトル
    rtol     許容誤差（デフォルトで 1e-6）
    maxit    収束計算のループ回数（デフォルトで min(20, numel(b)）
    x0       初期値
    flag　　　　　　　　　　結果　　0 なら収束，1 なら収束しなかった，3 なら"停滞"
    relres   相対残差
    iter     収束までのループ回数
    resvec   ループごとの相対誤差


```matlab
cgs(A,b)
```

    cgs converged at iteration 3 to a solution with relative residual 2.141707e-12
    ans =
    
       1.000000000001680
       0.999999999999331
       1.000000000001284
    


#### 1.2.2.　Preconditioned Conjugate Gradient iterative method `pcg()`

    usage:
    x = pcg (A, b, tol, maxit, m1, m2, x0, …)
    [x, flag, relres, iter, resvec, eigest] = pcg (…)


```matlab
pcg(A, b)
```

    pcg converged at iteration 3 with relative residual 1.79326e-14
    ans =
    
       0.999999999999986
       1.000000000000001
       0.999999999999986
    


#### 1.2.3.　Preconditioned Conjugate Residuals iterative method `pcr()`

    usage:
    x = pcr (A, b, tol, maxit, m, x0, …)
    [x, flag, relres, iter, resvec] = pcr (…)


```matlab
pcr(A, b)
```

    pcr: converged in 3 iterations. 
    pcr: the initial residual norm was reduced 4.38322e+14 times
    ans =
    
       1.000000000000038
       0.999999999999907
       0.999999999999897
    


#### 1.2.4.　Quasi-Minimal Residual iterative method (without look-ahead) `qmr()`

    usage:
    x = qmr (A, b, rtol, maxit, M1, M2, x0)
    x = qmr (A, b, rtol, maxit, P)
    [x, flag, relres, iter, resvec] = qmr (A, b, …)


```matlab
qmr(A, b)
```

    qmr converged at iteration 3 to a solution with relative residual 1.254498e-14
    ans =
    
       0.999999999999992
       1.000000000000003
       0.999999999999990
    


#### 1.2.5.　Bi-conjugate gradient iterative method `bicg()`

    usage:
    x = bicg (A, b, rtol, maxit, M1, M2, x0)
    x = bicg (A, b, rtol, maxit, P)
    [x, flag, relres, iter, resvec] = bicg (A, b, …)


```matlab
bicg(A, b)
```

    bicg converged at iteration 3 to a solution with relative residual 1.788163e-14
    ans =
    
       0.999999999999985
       1.000000000000004
       0.999999999999990
    


#### 1.2.6.　stabilizied Bi-conjugate gradient iterative method `bicgstab()`

    usage:
    x = bicgstab (A, b, rtol, maxit, M1, M2, x0)
    x = bicgstab (A, b, rtol, maxit, P)
    [x, flag, relres, iter, resvec] = bicgstab (A, b, …)


```matlab
bicgstab(A, b)
```

    bicgstab converged at iteration 2.5 to a solution with relative residual 1.245051e-14
    ans =
    
       1.000000000000161
       0.999999999999634
       0.999999999999601
    


## 2.　1 変数非線形方程式の解 `fzero()`

    f(x) = 0 を解く。

    usage:
    fzero (fun, x0)
    fzero (fun, x0, options)
    [x, fval, info, output] = fzero (…)
    fcn     非線形方程式
    x0      初期値
    options "FunValCheck", "OutputFcn", "TolX", "MaxIter", "MaxFunEvals"
    info    結果　　1:収束した，0:指定回数内に収束しなかった，-5: 特異点の解が得られた


```matlab
fzero("f", 3)
```

    error: invalid function handle, unable to find function for @f
    error: called from
        fzero at line 178 column 6


## 3.　非線形連立方程式の解 `fsolve()`

    usage:
    fsolve (fcn, x0, options)
    [x, fvec, info, output, fjac] = fsolve (fcn, …)
    fcn     非線形方程式
    x0      初期値
    options "FunValCheck", "OutputFcn", "TolX", "TolFun", "MaxIter",
            "MaxFunEvals", "Jacobian", "Updating", "ComplexEqn",
            "TypicalX", "AutoScaling", "FinDiffType"
    info    1: 収束した，0: 指定回数内に収束しなかった


```matlab
 function y = f (x)
   y(1) = -2*x(1)^2 + 3*x(1)*x(2)   + 4*sin(x(2)) - 6;
   y(2) =  3*x(1)^2 - 2*x(1)*x(2)^2 + 3*cos(x(1)) + 4;
 endfunction
 
[x, info] = fsolve ("f", [1; 2])
```

    x =
    
       0.579829087539860
       2.546209205762386
    
    info =
    
      -1.748468037021667e-11   3.004396731398629e-11