philsupertramp/game-math
Loading...
Searching...
No Matches
ExplicitEuler.h
Go to the documentation of this file.
1
11#pragma once
12
13#include "../utils.h"
14#include "ode.h"
15#include <functional>
16#include <vector>
17
26ODEResult ODEExpEuler(const ODE& fun, const std::vector<double>& tInterval, const Matrix<double>& y0, double h = 0.0) {
27 size_t dim = tInterval.size();
28 size_t elem_size = y0.columns();
29 if(h == 0) { h = (tInterval[dim - 1] - tInterval[0]) / 1000; }
30
31 // % initialize result vectors
32 auto result_dim = ((tInterval[dim - 1] - tInterval[0]) / h) + 1;
33 Matrix<double> t(0, result_dim, 1, 1);
34 size_t n = result_dim;
35 auto y = zeros(n, elem_size);
36
37 t(0, 0) = tInterval[0];
38 for(unsigned long i = 0; i < y0.columns(); ++i) y(0, i) = y0(0, i);
39
40 for(size_t i = 1; i < n; i++) {
41 auto cur_t = t(i - 1, 0);
42 Matrix<double> cur_y = y(i - 1);
43 Matrix<double> fun_value = fun(cur_t, cur_y);
44
45 auto yi = cur_y + fun_value * h;
46 y.SetRow(i, yi.GetSlice(0, 0, 0, elem_size - 1));
47 t(i, 0) = cur_t + h;
48 }
49 return { y, t };
50}
51
ODEResult ODEExpEuler(const ODE &fun, const std::vector< double > &tInterval, const Matrix< double > &y0, double h=0.0)
Definition: ExplicitEuler.h:26
Definition: Matrix.h:42
void SetRow(size_t index, const Matrix< T > &other)
Definition: Matrix.h:541
size_t columns() const
Definition: Matrix.h:198
Matrix< double > zeros(size_t rows, size_t columns, size_t elements=1)
std::function< Matrix< double >(double, Matrix< double >)> ODE
alias for ODE
Definition: ode.h:17
Definition: ode.h:25