-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblatt03.cpp
More file actions
103 lines (86 loc) · 2.34 KB
/
blatt03.cpp
File metadata and controls
103 lines (86 loc) · 2.34 KB
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
#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>
using namespace std;
int main_a()
{
// a und b einlesen, eps (in Abhängigkeit von a) bestimmen.
double a, b;
cout << "a, b: " << flush;
cin >> a >> b;
if (a < 0 or b < 0 or b > a)
{
cout << "a >= b > 0 erforderlich!" << endl;
return 0;
}
double const eps = a*a * numeric_limits<double>::epsilon() / 2;
// Kontrollausgabe mit 4 Nachkommastellen im Festpunktformat.
cout << fixed << setprecision(4)
<< "U(" << a << ", " << b << ") = ";
// Definiere rekursive Hilfsvariablen.
double c = a*a - b*b, // Anfangswert c0.
w = c / 2, // 2^(n-1) * c, Anfangswert ist c/2.
U = a*a - w; // a^2 - 1.Summand (Rest wird zukzessive abgezogen)
int n = 0;
while (w > eps)
{
double a_ = a; // a_ ist eine Hilfsvariable und sichert a ...
a = (a + b) / 2; // ... das hier verändert wird...
b = sqrt(a_ * b); // ... hier jedoch der alte Wert gebraucht wird.
c *= c;
c /= 16 * a * a;
w = ldexp(c, n);
U -= w;
++n;
}
U *= 2 * M_PI / a;
cout << scientific << setprecision(15) << U << endl;
return 0;
}
int main_b()
{
// a und b einlesen.
double a, b;
cout << "a, b: " << flush;
cin >> a >> b;
if (a < 0 or b < 0 or b > a)
{
cout << "a >= b > 0 erforderlich!" << endl;
return 0;
}
double const eps = numeric_limits<double>::epsilon() / 2;
// Kontrollausgabe mit 4 Nachkommastellen im Festpunktformat.
cout << fixed << setprecision(4)
<< "U(" << a << ", " << b << ") = ";
double e = 1.0 - (b*b)/(a*a), // wurzel weglassen, e^2 durch e ersetzen.
x = e / 4,
U = 1.0;
unsigned k = 2;
while (x >= eps)
{
U -= x;
++k;
x *= e * (k-2)*k;// / ((k+2)*(k+2));
++k;
x /= k*k;
}
U *= 2 * M_PI * a;
cout << scientific << setprecision(15) << U << endl;
return 0;
}
int main()
{
char aufg;
while (true)
{
cout << "Teilaufgabe (a/b): " << flush;
cin >> aufg;
switch (aufg)
{
case 'a': return main_a();
case 'b': return main_b();
}
}
return 0;
}