]> Creatis software - CreaPhase.git/blob - octave_packages/ocs-0.1.3/nls/nls_stationary.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / ocs-0.1.3 / nls / nls_stationary.m
1 ## Copyright (C) 2006,2007,2008  Carlo de Falco            
2 ##
3 ## This file is part of:
4 ## OCS - A Circuit Simulator for Octave
5 ##
6 ## OCS is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation.
9 ##
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program (see the file LICENSE); if not,
17 ## see <http://www.gnu.org/licenses/>.
18 ##
19 ## author: Carlo de Falco <cdf _AT_ users.sourceforge.net> 
20
21 ## -*- texinfo -*-
22 ## @deftypefn{Function File} {[@var{out}, @var{niter}]} = nls_stationary @
23 ## (@var{instruct},@var{x},@var{tol},@var{maxit})
24 ## Compute the stationary state solution @var{out} of the system described
25 ## by @var{instruct}.
26 ##
27 ## @var{x} is the initial guess used by the Newton-Raphson algorithm implemented in
28 ## @code{nls_newton_raphson}, while @var{tol} and @var{maxit} are the corresponding 
29 ## parameters.
30 ## 
31 ## The optional output @var{niter} returns the number of Newton iterations
32 ## needed to reach convergence.
33 ##
34 ## @seealso{nls_newton_raphson}
35 ## @end deftypefn
36
37 function [out, varargout] = nls_stationary(outstruct,x,tol,maxit)
38
39   ## Check input
40   ## FIXME: add input check!
41   if nargin != 4
42     error("nls_stationary: wrong number of input parameters.");
43   endif
44
45   [A0,B,C] = asm_initialize_system(outstruct,x);
46   JAC = @(x) TSTSTATFUNJAC(outstruct,x,B);
47   RES = @(x) TSTSTATFUNRES(outstruct,x,B,C);
48   UPD = @(x) TSTSTATUP(outstruct,x);
49   [out,ii,resnrm] = nls_newton_raphson(x,RES,JAC,tol,maxit,0,UPD);
50
51   if nargin > 1
52     varargout{1} = ii;
53   endif
54
55 endfunction
56
57 ## Jacobian for steady state problems
58 function lhs = TSTSTATFUNJAC(outstruct,x,B,Jac,res)
59
60   if nargin < 5
61     [A1,Jac,res] = asm_build_system(outstruct,x,0);
62   endif
63   lhs = (B + Jac); 
64
65 endfunction
66 ## Residual for steady state problem
67 function rhs = TSTSTATFUNRES(outstruct,x,B,C,Jac,res)
68
69   if nargin < 6
70     [A1,Jac,res] = asm_build_system(outstruct,x,0);
71   endif
72   rhs = (res + C + B*x);
73
74 endfunction
75 ## Update for transient problem
76 function update = TSTSTATUP(outstruct,x)
77
78   [A1,Jac,res] = asm_build_system(outstruct,x,0);
79   update = {Jac,res};
80
81 endfunction