8 #include <gsl/gsl_errno.h> 9 #include <gsl/gsl_matrix.h> 10 #include <gsl/gsl_odeiv2.h> 24 int func(
double x,
const double yin[],
double yout[],
void *params) {
26 yout[0] = yin[0] - 2 * x / yin[0];
36 return std::sqrt(2 * x + 1);
44 gsl_odeiv2_system sys = {
51 const gsl_odeiv2_step_type *type = gsl_odeiv2_step_rk4;
53 gsl_odeiv2_driver *d =
54 gsl_odeiv2_driver_alloc_y_new(&sys,
62 for (
double xi = 0; xi < 2; xi += 0.25) {
63 int status = gsl_odeiv2_driver_apply(d, &x, xi, y);
64 if (status != GSL_SUCCESS) {
65 printf(
"error, return value=%d\n", status);
69 printf(
"%.5e %.5e %.5e\n", x, y[0],
fexact(x));
72 gsl_odeiv2_driver_free(d);
97 int odefn(
double t,
const double fin[],
double fout[]) {
98 fout[0] = fin[0] - 2 * t / fin[0];
104 auto exactSolutionFn = [](
double t) {
return sqrt(2 * t + 1); };
107 ODESolver ode{ myObj, 1e-2, 1e-9, 0 };
108 ode.set_init_conditions(0, { 1 });
109 double t = 0, dt = 0.25;
111 const double numSolution = ode[0];
112 const double exactSolution = exactSolutionFn(t);
113 const double absError = abs(numSolution - exactSolution);
114 data += { t, numSolution, exactSolution, absError };
134 const double acceleration = 2;
135 const double x0 = 0, v0 = 0;
139 auto obj = [](
double t,
const double fin[],
double fout[]) {
147 auto v = [&acceleration](
double t) {
148 return acceleration * t;
150 auto x = [&acceleration](
double t) {
151 return 0.5 * acceleration * pow(t, 2);
155 ODESolver myOde{ obj, 1e-2, 1e-9, 0 };
156 myOde.set_init_conditions(0, { x0, v0 });
157 double t = 0, dt = 0.5;
159 const double xnum = myOde[0];
160 const double xexact = x(t);
161 const double vnum = myOde[1];
162 const double vexact = v(t);
163 const double verror = abs(vnum - vexact);
164 const double xerror = abs(xnum - xexact);
167 data += { t, xnum, xexact, xerror, vnum, vexact, verror };
182 const double mass = 40.0;
183 const double mu = 0.4;
184 const double force = 200.0;
185 const double s = 5.0;
188 const double x0 = 0, v0 = 0;
192 const double a = force / mass - mu * g;
193 const double vs = std::sqrt(2 * a * s);
194 const double tval = vs / a;
195 std::cout << std::setw(3) << tval <<
"," 196 << std::setw(3) << vs << std::endl;
202 auto acceleration = [=](
double t,
217 Ode ode{ acceleration, 1e-2, 1e-9, 0 };
218 ode.set_init_conditions(0, { x0, v0 });
219 double t = 0, dt = 0.01;
222 data += { t, ode[0], ode[1], 0.5 * mass * pow(ode[1], 2) };
void ex1()
ex1 Beispiel (Vorlage)
auto get_output_stream(const char *name, Format fmt)
get_output_stream generiere Ausgabestrom
void save(std::ostream &ofs, Format fmt) const
save Schreibe Tabelle in Datei
The Data class Eine Klasse für Zahlentabellen mit fester Anzahl von Spalten. Die Anzahl der Reihen wä...
int func(double x, const double yin[], double yout[], void *params)
func rechte Seite der Differenzialgleichung
The Odeiv2 class numerische Lösung eines Systems von N Differenzialgleichungen Schnittstelle zur gsl...
void ode2()
ex2 DGL 2. Grades
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...
void ode3()
ode3 Konstante Kraft wirkt auf Holzblock
auto select_total_rows(size_t n, size_t start=0, bool l=true) const
get_total_rows generiere View mit eine Instanz vom Typ View bestimmter Anzahl von Spalten ...
void ode1()
ode Lösung einer DGL
static const Format latex
double fexact(double x)
fexact exakte Lösung
static constexpr double g