53 lines
1.4 KiB
C++
53 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include "api.hpp"
|
|
|
|
struct State
|
|
{
|
|
double x1;
|
|
double x2;
|
|
double x3;
|
|
};
|
|
|
|
inline State add_scaled(const State& left, const State& right, double scale){
|
|
return {
|
|
left.x1 + scale * right.x1,
|
|
left.x2 + scale * right.x2,
|
|
left.x3 + scale * right.x3,
|
|
};
|
|
}
|
|
|
|
// СОДУ
|
|
inline State derivative(const State& state,
|
|
const SolverParameters& parameters){
|
|
return {
|
|
-parameters.k1 * state.x1,
|
|
parameters.k1 * state.x1 - parameters.k2 * state.x2,
|
|
parameters.k2 * state.x2
|
|
};
|
|
}
|
|
|
|
inline State runge_kutta_step(const State& state,
|
|
const SolverParameters& parameters){
|
|
double h = parameters.step;
|
|
|
|
State k1 = derivative(state, parameters);
|
|
State k2 = derivative(add_scaled(state, k1, h / 2), parameters);
|
|
State k3 = derivative(add_scaled(state, k2, h / 2), parameters);
|
|
State k4 = derivative(add_scaled(state, k3, h), parameters);
|
|
|
|
return {
|
|
state.x1 + h / 6.0 * (k1.x1 + 2 * k2.x1 + 2 * k3.x1 + k4.x1),
|
|
state.x2 + h / 6.0 * (k1.x2 + 2 * k2.x2 + 2 * k3.x2 + k4.x2),
|
|
state.x3 + h / 6.0 * (k1.x3 + 2 * k2.x3 + 2 * k3.x3 + k4.x3)
|
|
};
|
|
}
|
|
|
|
inline State state_from_point(const SolutionPoint& point){
|
|
return { point.x1, point.x2, point.x3 };
|
|
}
|
|
|
|
inline SolutionPoint make_step(double t, const State& state){
|
|
return { t, state.x1, state.x2, state.x3 };
|
|
}
|