File: nonlin.texi

package info (click to toggle)
octave2.1 1%3A2.1.73-13
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 37,028 kB
  • ctags: 20,874
  • sloc: cpp: 106,508; fortran: 46,978; ansic: 5,720; sh: 4,800; makefile: 3,186; yacc: 3,132; lex: 2,892; lisp: 1,715; perl: 778; awk: 174; exp: 134
file content (135 lines) | stat: -rw-r--r-- 3,056 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
@c DO NOT EDIT!  Generated automatically by munge-texi.

@c Copyright (C) 1996, 1997 John W. Eaton
@c This is part of the Octave manual.
@c For copying conditions, see the file gpl.texi.

@node Nonlinear Equations
@chapter Nonlinear Equations
@cindex nonlinear equations
@cindex equations, nonlinear

Octave can solve sets of nonlinear equations of the form
@iftex
@tex
$$
 f (x) = 0
$$
@end tex
@end iftex
@ifinfo

@example
F (x) = 0
@end example
@end ifinfo

@noindent
using the function @code{fsolve}, which is based on the @sc{Minpack}
subroutine @code{hybrd}.

@anchor{doc-fsolve}
@deftypefn {Loadable Function} {[@var{x}, @var{info}, @var{msg}] =} fsolve (@var{fcn}, @var{x0})
Given @var{fcn}, the name of a function of the form @code{f (@var{x})}
and an initial starting point @var{x0}, @code{fsolve} solves the set of
equations such that @code{f(@var{x}) == 0}.

If @var{fcn} is a two-element string array, the first element names
the function @math{f} described above, and the second element names
a function of the form @code{j (@var{x})} to compute the Jacobian
matrix with elements
@tex
$$ J = {\partial f_i \over \partial x_j} $$
@end tex
@ifinfo

@example
           df_i
jac(i,j) = ----
           dx_j
@end example
@end ifinfo

You can use the function @code{fsolve_options} to set optional
parameters for @code{fsolve}.
@end deftypefn


@anchor{doc-fsolve_options}
@deftypefn {Loadable Function} {} fsolve_options (@var{opt}, @var{val})
When called with two arguments, this function allows you set options
parameters for the function @code{fsolve}.  Given one argument,
@code{fsolve_options} returns the value of the corresponding option.  If
no arguments are supplied, the names of all the available options and
their current values are displayed.

Options include

@table @code
@item "tolerance"
Nonnegative relative tolerance.
@end table
@end deftypefn


Here is a complete example.  To solve the set of equations
@iftex
@tex
$$
 \eqalign{-2x^2 + 3xy + 4\sin(y) - 6 &= 0\cr
           3x^2 - 2xy^2 + 3\cos(x) + 4 &= 0}
$$
@end tex
@end iftex
@ifinfo

@example
-2x^2 + 3xy   + 4 sin(y) = 6
 3x^2 - 2xy^2 + 3 cos(x) = -4
@end example
@end ifinfo

@noindent
you first need to write a function to compute the value of the given
function.  For example:

@example
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
@end example

Then, call @code{fsolve} with a specified initial condition to find the
roots of the system of equations.  For example, given the function
@code{f} defined above,

@example
[x, info] = fsolve ("f", [1; 2])
@end example

@noindent
results in the solution

@example
x =

  0.57983
  2.54621

info = 1
@end example

A value of @code{info = 1} indicates that the solution has converged.

The function @code{perror} may be used to print English messages
corresponding to the numeric error codes.  For example,

@example
@group
perror ("fsolve", 1)
     @print{} solution converged to requested tolerance
@end group
@end example