CppNoddy  0.92
Loading...
Searching...
No Matches
NewtonIter.cpp
Go to the documentation of this file.
1/// \file NewtonIteration.cpp
2/// \ingroup Tests
3/// \ingroup Generic
4/// A vector Newton iteration to find
5/// a root of \f[ f(z) = z^3 - 1 \f] by splitting
6/// it into a vector equation (real & imaginary parts).
7
8#include <Newton_bundle.h>
9
10namespace CppNoddy
11{
12 namespace Example
13 {
14 /// Defines the problem: here z^3 -1 = 0, in vector form
15 /// using real and imaginary parts.
16 class VCube_root_problem : public Residual<double>
17 {
18 public:
19
20 VCube_root_problem() : Residual<double>( 2 ) {}
21
22 /// The residual function
23 /// \param z The independent variable
24 /// \param f The residual function f(z)
26 {
27 f[ 0 ] = z[ 0 ] * z[ 0 ] * z[ 0 ] - 3 * z[ 0 ] * z[ 1 ] * z[ 1 ] - 1.;
28 f[ 1 ] = 3 * z[ 0 ] * z[ 0 ] * z[ 1 ] - z[ 1 ] * z[ 1 ] * z[ 1 ];
29 }
30 };
31 } // end Example namespace
32} // end CppNoddy namespace
33
34
35using namespace CppNoddy;
36using namespace std;
37
38int main()
39{
40 cout << "\n";
41 cout << "=== Newton: vector residual root test ==============\n";
42 cout << "\n";
43
44 // Instantiate the problem
45 Example::VCube_root_problem residual_problem;
46 // A Newton object
47 Newton<double> newton( &residual_problem );
48
49 // Set an initial guess
50 DenseVector<double> guess( 2, 0.0 );
51 guess[ 0 ] = -0.5;
52 guess[ 1 ] = 1.5;
53
54 try
55 {
56 newton.iterate( guess );
57 }
58 catch (const std::runtime_error &error )
59 {
60 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
61 return 1;
62 }
63
64 const double tol = 1.e-7;
65 if ( ( abs( guess[ 0 ] + 0.5 ) > tol ) ||
66 ( abs( guess[ 1 ] - sqrt( 3. ) / 2. ) > tol ) )
67 {
68 cout << "\033[1;31;48m * FAILED \033[0m\n";
69 guess.dump();
70 cout << abs( guess[ 1 ] - sqrt( 3. ) / 2. ) << "\n";
71 cout << abs( guess[ 0 ] + 0.5 ) << "\n";
72 return 1;
73 }
74 cout << "\033[1;32;48m * PASSED \033[0m\n";
75 return 0;
76
77}
@ f
Definition: BVPBerman.cpp:15
int main()
Definition: NewtonIter.cpp:38
A shorter bundled include file for Newton iteration problems.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
void dump() const
Dump to std::cout.
Definition: DenseVector.cpp:64
Defines the problem: here z^3 -1 = 0, in vector form using real and imaginary parts.
Definition: NewtonIter.cpp:17
void residual_fn(const DenseVector< double > &z, DenseVector< double > &f) const
The residual function.
Definition: NewtonIter.cpp:25
A vector NEWTON iteration class.
Definition: Newton.h:25
void iterate(DenseVector< _Type > &x)
The Newton iteration method.
Definition: Newton.cpp:35
A base class to be inherited by objects that define residuals.
Definition: Residual.h:15
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt