x021.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xdata.h"
4 #include "xdefs.h"
5 #include "xmath.h"
6 #include "xphysics.h"
7 
8 namespace nmx::apps::x021 {
12 inline void ex23() {
13  using namespace gravitation;
14  // Tabelle mit drei Spalten t,z,v
15  using Data = Data<3>;
16  Data table;
17 
18  std::ofstream output = Output::get_stream("x021",
19  Output::csv, //
20  __func__);
21  output << std::setprecision(3);
22 
23  //Anfangsbedingungen
24  const double z0 = 0.0, v0 = 20.0;
25  const double g = Earth::g;
26 
27  //rechne mit kleinen Zeitschritten
28  double t = 0, dt = 1e-3, z = z0, v = v0;
29  while (z >= 0) {
30  table += { t, z, v };
31  t += dt;
32  z = z0 + v0 * t - 0.5 * g * pow(t, 2);
33  v = v0 - g * t;
34  }
35 
36  //finde Vorzeichenwechsel der Geschwindigkeit
37  auto itr = std::adjacent_find(ALL(table.data()), //
38  [](const auto &x, const auto &y) { //
39  return x[2] * y[2] < 0;
40  });
41 
42  //addiere gefundene Zeile und nächste sowie die letzte Zeile
43  View view = table.view({ itr, itr + 1 }, false, true);
44 
45  //Ausgabe
46  output << std::setprecision(3);
47  output << std::setw(5) << "t" << std::setw(10) << "z" << std::setw(10) << "v" << std::endl;
48  output << "-------------------------------" << std::endl;
49  view.save(output, Output::csv);
50  output << "-------------------------------" << std::endl;
51  //die exakten Werte
52  Data::Row r1{ v0 / Earth::g, 0.5 * pow(v0, 2) / Earth::g, 0 };
53  Data::Row r2{ 2 * v0 / Earth::g, 0, -v0 };
54  Output::csv.as_rows(output, r1, r2);
55 }
56 
60 inline void ex24() {
61  const double mass = 2.0;
62  const double force = 10.0;
63  Data<7> table;
64  const double t = 10.0;
65  //für jeden Winkel...
66  for (double phi : { 10, 30, 45, 60 }) {
67  //... berechne Daten
68  const double phirad = Math::to_radians(phi);
69  const double fx = force * cos(phirad);
70  const double fy = force * sin(phirad);
71  const double ax = fx / mass;
72  const double vx = ax * t;
73  const double x = 0.5 * ax * pow(t, 2);
74  const double work = fx * x;
75  //...und füge eine neue Reihe zur Tabelle hinzu
76  table += { phi, fx, fy, ax, vx, x, work };
77  }
78  //speichere Daten in Datei
79  std::ofstream csvstream = Output::get_stream("x021", Output::latex, __func__);
80  table.save(csvstream, Output::latex);
81 }
82 
86 inline void ex25() {
87  // Datenobjekt
88  using Data = Data<6>;
89  Data table;
90  //Masse des Geschosses
91  const double m = 200.0_g;
92  //erzeuge Daten für drei Holzblockmassen und
93  //drei Kugelgeschwindigkeiten
94  for (auto M : { 1.0, 5.0, 10.0 }) {
95  const double factor = m / (m + M);
96  for (auto v : { 200.0, 400.0, 600.0 }) {
97  const auto vp = factor * v;
98  //kinetische Energie vor dem Stoß
99  const auto ekinB = 0.5 * m * pow(v, 2);
100  //kinetische Energie nach dem Stoß
101  const auto ekinA = 0.5 * (M + m) * pow(vp, 2);
102  const auto ekinDiff = 100 * abs(ekinA - ekinB) / ekinB;
103  //speichere Daten im Datenobjekt
104  table += { M, v, vp, ekinB, ekinA, ekinDiff };
105  }
106  }
107  //erzeuge Ausgabestrom und speichere die Daten im LaTeX-Format
108  std::ofstream ofs = Output::get_stream("x021", Output::latex, __func__);
109  table.save(ofs, Output::latex);
110 }
111 
115 void ex26() {
116  using namespace gravitation; //für die universelle Gravitationskonstante
117  Data<3> table;
118  //Abstände xmin = R bis xmax = 2* R...
119  const auto xmin = Earth::radius;
120  const auto xmax = 2 * xmin;
121  //...mit Schrittweite
122  const auto dx = 1000.0_km;
123  const auto mass = 10.0;
124  const auto factor = Astronomy::G * Earth::mass * mass;
125 
126  for (double x = xmin; x < xmax; x += dx) {
127  const auto force = -factor / pow(x + xmin, 2);
128  const auto pEnergy = -factor / (x + xmin);
129  //speichere Abstand, Kraft, potenzielle Energie in Zahlentabelle
130  table += { x, force, pEnergy };
131  }
132  //erzeuge Ausgabestrom und speichere die Daten im LaTeX-Format
133  std::ofstream ofs = Output::get_stream("x021", Output::latex, __func__);
134  table.save(ofs, Output::latex);
135 }
136 
140 void ex27() {
141  using Data = Data<3>;
142  Data data;
143 
144  // Tabelle mit {m,x,y}
145  data += { 1, 0, 0 }; // Masse 1
146  data += { 2, 4, 0 }; // Masse 2
147  data += { 3, 2, 3.46 }; //Masse 3
148 
149  //Ortvektor und Masse des Schwerpunktes
150  std::array cmPosition{ 0.0, 0.0 };
151  double cmMass = 0;
152 
153  //Masse und Ortsvektor werden bei jedem Aufruf aktualisiert.
154  auto calcCM = [&cmMass, &cmPosition](const Data::Row &row) {
155  cmMass += row[0]; // addiere neue Masse hinzu
156  cmPosition[0] += row[0] * row[1]; // m_i * x_i
157  cmPosition[1] += row[0] * row[2]; // m_i * y_i
158  };
159 
160  //Wende Funktion auf jede Reihe des Datenobjekts an
161  data.apply(calcCM);
162  cmPosition[0] /= cmMass; //xcm
163  cmPosition[1] /= cmMass; //ycm
164 
165  //Gesamtmasse und Schwerpunktskoordinaten in letzte Reihe
166  data += { cmMass, cmPosition[0], cmPosition[1] };
167 
168  //Ausgabe
169  std::ofstream ofs = Output::get_stream("x021", Output::latex, __func__);
170  data.save(ofs, Output::latex);
171 }
172 
173 } // namespace nmx::apps::x021
void apply(FN fn)
apply ändere alle oder bestimmte Elemente
Definition: xdata.h:226
static const Format csv
Definition: xoutput.h:20
auto view() const
view die ganze Tabelle als view
Definition: xdata.h:258
void save(std::ostream &ofs, Format fmt) const
save Schreibe Tabelle in Datei
Definition: xdata.h:160
The Data class Eine Klasse für Zahlentabellen mit fester Anzahl von Spalten. Die Anzahl der Reihen wä...
Definition: xdata.h:22
void ex26()
ex26 Kraft und potenzielle Energie im Gravitationsfeld der Erde
Definition: x021.h:115
const auto & data() const
data lesender Zugriff auf die interne Struktur
Definition: xdata.h:43
static std::ofstream get_stream(const std::string &fname)
get_output_stream Öffnen einer Ausgabedatei falls die Datei nicht geöffnet werden kann wird ein Fehle...
Definition: xoutput.h:29
static double to_radians(double x)
Definition: xmath.h:15
void ex27()
ex27 Massenschwerpunkt von drei Teilchen
Definition: x021.h:140
The View class Sicht auf Teilmenge eines Containers.
Definition: xview.h:13
void as_rows(std::ostream &ofs, const X &x, const Y &... y) const
as_rows beliebige Anzahl von Arrays werden als Reihen ausgegeben
Definition: xfmt.h:81
#define ALL(x)
void ex23()
ex23 Teilchen wird vertikal nach oben geworfen
Definition: x021.h:12
static const Format latex
Definition: xoutput.h:17
void ex24()
ex24 Kraft beschleunigt eine Masse auf reibungsfreien Boden
Definition: x021.h:60
std::array< double, N > Row
Definition: xdata.h:25
void ex25()
ex25 Geschoss trifft auf ruhenden Holzblock
Definition: x021.h:86