(
const double&x, std::valarray<double> *y,
54std::valarray<double> *dy) {
55 const doubleomega = 1.F;
57dy[0][1] = -omega * omega * y[0][0];
67y[0][0] = std::cos(x);
68y[0][1] = -std::sin(x);
86std::valarray<double> *y, std::valarray<double> *dy) {
88 doubletmp_x = x + 0.5 * dx;
90std::valarray<double> tmp_y = y[0] + dy[0] * (0.5 * dx);
108std::valarray<double> *y,
boolsave_to_file =
false) {
109std::valarray<double> dy = y[0];
113fp.open(
"midpoint_euler.csv", std::ofstream::out);
115std::perror(
"Error! ");
119std::size_t L = y->size();
122std::clock_t t1 = std::clock();
125 if(save_to_file && fp.is_open()) {
128 for(
inti = 0; i < L - 1; i++) {
129fp << y[0][i] <<
",";
131fp << y[0][L - 1] <<
"\n";
136}
while(x <= x_max);
138std::clock_t t2 = std::clock();
143 return static_cast<double>(t2 - t1) / CLOCKS_PER_SEC;
157 const double&step_size,
158 conststd::valarray<double> &Y0) {
160std::valarray<double> y = Y0;
162std::ofstream fp(
"exact.csv", std::ostream::out);
164std::perror(
"Error! ");
167std::cout <<
"Finding exact solution\n";
169std::clock_t t1 = std::clock();
172 for(
inti = 0; i < y.size() - 1; i++) {
175fp << y[y.size() - 1] <<
"\n";
180}
while(x <= X_MAX);
182std::clock_t t2 = std::clock();
183 doubletotal_time =
static_cast<double>(t2 - t1) / CLOCKS_PER_SEC;
184std::cout <<
"\tTime = "<< total_time <<
" ms\n";
192int main(
intargc,
char*argv[]) {
195std::valarray<double> Y0 = {1.f, 0.f};
199std::cout <<
"\nEnter the step size: ";
200std::cin >> step_size;
203step_size = std::atof(argv[1]);
207 doubletotal_time =
midpoint_euler(step_size, X0, X_MAX, &Y0,
true);
208std::cout <<
"\tTime = "<< total_time <<
" ms\n";
int main()
Main function.
double midpoint_euler(double dx, double x0, double x_max, std::valarray< double > *y, bool save_to_file=false)
Compute approximation using the midpoint-Euler method in the given limits.
void midpoint_euler_step(const double dx, const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Compute next step approximation using the midpoint-Euler method.
void save_exact_solution(const double &X0, const double &X_MAX, const double &step_size, const std::valarray< double > &Y0)
void problem(const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
void exact_solution(const double &x, std::valarray< double > *y)
Exact solution of the problem. Used for solution comparison.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4