cppIStough
- 24
- 2
EDIT: I don't know why the cpp code box below isn't appearing in proper format. I'll fix it if I can, but I don't see anyhting wrong with the syntax to display properly.
Okay, this was helpful! Here's what I have (I'll clean up the rounding errors, and to answer a few posts ago, I am familiar with convergence, stiff ODEs, and I actually have heard of JIT compilation, though only about a year ago; I'm learning C++ because I want to know it (already know Python fairly well, though I'm sure compared to many on here I'm a novice), which is why I am picking some easy math problems to get my hands dirty)
But here's what I have so far in 3 files though I'm getting errors which I'll list below the three files this program consists of:
rk4.h
rk4.cpp
ODE.cpp
In rk4.cpp there is an error with the constructor stating "no instance of overloaded function RK4::RK4" which I'm confused on. Another error in this file is with the f = f_, which reads "identifier "f" is undefined, which is why I thought I may need to declare it, but am unsure how since the function is not a member of RK4. Any help would be awesome!
Okay, this was helpful! Here's what I have (I'll clean up the rounding errors, and to answer a few posts ago, I am familiar with convergence, stiff ODEs, and I actually have heard of JIT compilation, though only about a year ago; I'm learning C++ because I want to know it (already know Python fairly well, though I'm sure compared to many on here I'm a novice), which is why I am picking some easy math problems to get my hands dirty)
But here's what I have so far in 3 files though I'm getting errors which I'll list below the three files this program consists of:
rk4.h
C++:
#pragma once
#include <iostream>
#include <vector>
class RK4 {
private:
double x0, y0, x_end, h;
public:
std::vector<double> vec_x, vec_y;
void solve();
};
rk4.cpp
C++:
#include "rk4.h"
RK4::RK4(double f_(double x, double y), double x0_, double y0_, double x_end_, double h_)
{
f = f_;
x0 = x0_;
y0 = y0_;
x_end = x_end_;
h = h_;
int n = (x_end - x0) / h;
vec_x.resize(n);
vec_y.resize(n);
for (int i = 0; i < n; ++i)
{
vec_x[i ] = i * h;
vec_y[i ] = y0;
}
}
void RK4::solve()
{
for (int i = 0; i < vec_x.size() - 1; ++i) {
double k1 = f(vec_x[i ], vec_y[i ]);
double k2 = f(vec_x[i ] + h / 2, vec_y[i ] + h * k1 / 2);
double k3 = f(vec_x[i ] + h / 2, vec_y[i ] + h * k2 / 2);
double k4 = f(vec_x[i ] + h, vec_y[i ] + h * k3);
vec_y[i + 1] = vec_y[i ] + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4);
}
}
ODE.cpp
C++:
#include "rk4.h"
#include <iostream>
double dydx_equals(double x, double y)
{
return y;
}
int main() {
double h = 0.01;
double x0 = 0.;
double y0 = 1.;
double x_end = 1.;
RK4 solver(dydx_equals, x0, y0, x_end, h);
solver.solve();
for (auto v : solver.vec_y)
{
std::cout << v << "\n";
}
return 0;
}
In rk4.cpp there is an error with the constructor stating "no instance of overloaded function RK4::RK4" which I'm confused on. Another error in this file is with the f = f_, which reads "identifier "f" is undefined, which is why I thought I may need to declare it, but am unsure how since the function is not a member of RK4. Any help would be awesome!
Last edited by a moderator: