- #1
phyzmatix
- 313
- 0
Good day!
I'm busy with a simple little program to obtain values for a first order differential equation using the second-order Runge-Kutta method. I want the program to only print values for x = 0, x = 0.2, x = 0.4...x = 1.2 and am using the fmod(double, double) function in the cmath header to determine when x is a multiple of 0.2 (i.e. the remainder of x/0.2 = 0).
The problem I have is that this function works perfectly well up until x=0.6 when suddenly it returns a remainder of 0.2 and it's driving me freaking nuts as I can't figure out why!
You can see from the console output where it starts going wrong (bold)
FracPart 0.0000000000
x 0.1000000000
FracPart 0.1000000000
x 0.2000000000
FracPart 0.0000000000
x 0.3000000000
FracPart 0.1000000000
x 0.4000000000
FracPart 0.0000000000
x 0.5000000000
FracPart 0.1000000000
x 0.6000000000
FracPart 0.2000000000
x 0.7000000000
FracPart 0.1000000000
x 0.8000000000
FracPart 0.2000000000
x 0.9000000000
FracPart 0.1000000000
x 1.0000000000
FracPart 0.2000000000
x 1.1000000000
FracPart 0.1000000000
x 1.2000000000
FracPart 0.2000000000
x 1.3000000000
My code:
I'm using DevC++ v 4.9.9.2 and minGW.
Any help will be greatly appreciated!
phyz
I'm busy with a simple little program to obtain values for a first order differential equation using the second-order Runge-Kutta method. I want the program to only print values for x = 0, x = 0.2, x = 0.4...x = 1.2 and am using the fmod(double, double) function in the cmath header to determine when x is a multiple of 0.2 (i.e. the remainder of x/0.2 = 0).
The problem I have is that this function works perfectly well up until x=0.6 when suddenly it returns a remainder of 0.2 and it's driving me freaking nuts as I can't figure out why!
You can see from the console output where it starts going wrong (bold)
FracPart 0.0000000000
x 0.1000000000
FracPart 0.1000000000
x 0.2000000000
FracPart 0.0000000000
x 0.3000000000
FracPart 0.1000000000
x 0.4000000000
FracPart 0.0000000000
x 0.5000000000
FracPart 0.1000000000
x 0.6000000000
FracPart 0.2000000000
x 0.7000000000
FracPart 0.1000000000
x 0.8000000000
FracPart 0.2000000000
x 0.9000000000
FracPart 0.1000000000
x 1.0000000000
FracPart 0.2000000000
x 1.1000000000
FracPart 0.1000000000
x 1.2000000000
FracPart 0.2000000000
x 1.3000000000
My code:
Code:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
const double h = 0.1;
int main()
{
double x, y, kOne, kTwo, exact, error;
double a, b, alpha, beta;
double fracPart, param, intPart;
//initialises parameters
a = 0.6666666667;
b = 0.3333333333;
alpha = 1.5;
beta = 1.5;
x = 0;
y = 3;
//writes table heading to console
//cout << "x\t\t" << "y\t\t" << "Exact\t\t" << "Error\t\t" << endl;
cout.setf(ios::fixed);
cout.precision(10);
//writes new rows to table
do
{
exact = 2 + exp(x)*pow(cos(pow(x,2)),3);
error = exact - y;
fracPart = fmod(x, 0.2);
cout << "FracPart " << fracPart << endl;
/*if(fracPart == 0)
{
cout << x << "\t" << y << "\t" << exact << "\t" << error << endl;
}*/
kOne = h * ((1-(6*x*tan(pow(x,2))))*(y - 2));
kTwo = h * ((1-6*(x+alpha*h)*tan(pow((x+alpha*h),2)))*((y + beta*kOne) - 2));
y += (a*kOne + b*kTwo);
x += h;
cout << "x " << x << endl;
}while(x < 1.2);
system("PAUSE");
return 0;
}
I'm using DevC++ v 4.9.9.2 and minGW.
Any help will be greatly appreciated!
phyz