On this page we will try to show you, how to use the C-XSC class library with some short and simple examples.
Example 1 - An Introduction
We will start with the following simple program showing you how to use intervals, compute one operation and print out the result:
11 cout <<
"a/b = " << a/b << endl;
The Scalar Type interval.
The namespace cxsc, providing all functionality of the class library C-XSC.
Let's start investigating the interesting lines. With the line
you include the functionality of the interval class of C-XSC in your program. After that you have to inform the compiler about the namespace cxsc - where all classes and methods of C-XSC are stored in - to use C-XSC without fully qualified identifiers.
Then you declare your variables and assign adequate values in the following lines.
Finally you want to print out the result for your desired computation.
10 cout <<
"a/b = " << a/b << endl;
So it's just that easy to use C-XSC.
Example 2 - Input / Output
10 cout <<
"Please enter real a: ";
13 cout << SetPrecision(7,4);
22 cout << SetPrecision(18,15);
32 cout << SetPrecision(10,7);
Example 3 - Compute all zeros of a function
14DerivType f (
const DerivType& x )
16 return (x-1)*( exp(-3*x) - power(sin(x),3) );
28 int NumberOfZeros, i, Error;
30 cout << SetPrecision(23,15) << Scientific;
32 cout <<
"Search interval : ";
33 cin >> SearchInterval;
34 cout <<
"Tolerance (relative): ";
39 AllZeros(f,SearchInterval,Tolerance,Zero,Unique,NumberOfZeros,Error);
41 for ( i = 1; i <= NumberOfZeros; i++) {
42 cout << Zero[i] << endl;
44 cout <<
"encloses a locally unique zero!" << endl;
46 cout <<
"may contain a zero (not verified unique)!" << endl;
48 cout << endl << NumberOfZeros <<
" interval enclosure(s)" << endl;
49 if (Error) cout << endl << AllZerosErrMsg(Error) << endl;
Example 4 - Interval Newton method
13 return sqrt(y) + (y+1)*cos(y);
18 return 1/(2*sqrt(x)) + cos(x) - (x+1)*sin(x);
23 return Sup( f(Inf(x))*f(Sup(x)) ) < 0.0 && !(0.0 <= deriv(x));
29 cout << SetPrecision(20,15);
31 cout <<
"Starting interval is [2,3]" << endl;
36 cout <<
"Actual enclosure is " << x << endl;
37 x = (
mid(x)-f(
mid(x))/deriv(x)) & x;
39 cout <<
"Final enclosure of the zero: " << x << endl;
42 cout <<
"Criterion not satisfied!" << endl;
cvector mid(const cimatrix_subv &mv) noexcept
Returns the middle of the matrix.
Example 5 -
1#include "l_interval.hpp"
12 cout << SetDotPrecision(16*stagprec, 16*stagprec-3) << RndNext;
16 cout <<
"a/b = " << a/b << endl;
The Multiple-Precision Data Type l_interval.
Example 6 -
5#include "l_interval.hpp"
14 return sqrt(y) + (y+1)*cos(y);
19 return 1/(2*sqrt(x)) + cos(x) - (x+1)*sin(x);
24 return Sup( f(Inf(x))*f(Sup(x)) ) < 0.0 && !(0.0 <= deriv(x));
32 cout <<
"Starting interval is [2,3]" << endl;
33 cout << SetDotPrecision(16*stagprec, 16*stagprec-3) << RndNext;
41 cout <<
"Diameter of actual enclosure: " <<
real(
diam(x)) << endl;
42 x = (
mid(x)-f(
mid(x))/deriv(x)) & x;
44 cout <<
"Final enclosure of the zero: " << x << endl;
47 cout <<
"Criterion not satisfied!" << endl;
The Multiple-Precision Data Type l_real.
cvector diam(const cimatrix_subv &mv) noexcept
Returns the diameter of the matrix.
Example 7 -
14 Z[3] = -0.522 * Y[1] * Y[2];
21 Y[1] = 0.0; Y[2] = 1.0; Y[3] = 1.0;
30 for (
int i=1; i<=3; i++) {
32 K2 = h * F(x + h / 2, Y + K1 / 2);
33 K3 = h * F(x + h / 2, Y + K2 / 2);
34 K4 = h * F(x + h, Y + K3);
35 Y = Y + (K1 + 2 * K2 + 2 * K3 + K4) / 6;
37 cout << SetPrecision(10,6) << Dec;
38 cout <<
"Step: " << i <<
", "
39 <<
"x = " << x << endl;
40 cout <<
"Y = " << endl << Y << endl;
Example 8 -
13 cout <<
"Please enter the matrix dimension n: "; cin >> n;
16 cout <<
"Please enter the matrix A:" << endl; cin >> A;
17 cout <<
"Please enter the matrix B:" << endl; cin >> B;
19 for (
int i=1; i<=n; i++) accumulate(accu, A[i], B[Col(i)]);
25 cout << SetPrecision(12,6) << RndNext << Dec;
26 cout <<
"Trace of product matrix: " << result << endl;
The Data Type cdotprecision.