Question about iteration

Hello, I am working on an assignment using Euler's method and i need the X value to be tabulated every 0.1 unit. However I am having issues with correct tabulation. Here is my source code and output:

Source Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

#define E        2.71828182845904523536

double F(double x){return x * pow(E, x) + pow(E, x);}
double Fanalytical(double x){return x * pow(E, x) + 1;}

double A(double x, double y){return y - (x * x) + 1;}
double Aanalytical(double x){return (x + 1) * (x + 1) - 0.5 * pow(E, x);}

int main()
{
	double h, x, y, endP1, endP2;
	cout << "Step Size (0.1, 0.01, 0.001, 0.0001): ";
	cin >> h;

	cout << "End points: ";
	cin >> endP1 >> endP2;

	cout << "Initial value of Y: ";
	cin >> y;
	
	cout << "X-Value         Euler         Analytical       Error\n";
	for(x = endP1; x < endP2 + .0001; x += h)
	{
		if(int(x*10000) % 1000 == 0)
		{
			cout << fixed << setprecision(1) << x << setprecision(7) << "           "
				 << y << "       " << Aanalytical(x) << "       " << Aanalytical(x) - y << endl;
		}
		y += h * A(x,y);
	}
	return 0;
}


The output when the step size = 0.1 is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Step Size (0.1, 0.01, 0.001, 0.0001): 0.1
End points: 0 2
Initial value of Y: .5
X-Value         Euler         Analytical       Error
0.0           0.5000000       0.5000000       0.0000000
0.1           0.6500000       0.6574145       0.0074145
0.2           0.8140000       0.8292986       0.0152986
0.3           0.9914000       1.0150706       0.0236706
0.4           1.1815400       1.2140877       0.0325477
0.5           1.3836940       1.4256394       0.0419454
0.6           1.5970634       1.6489406       0.0518772
0.7           1.8207697       1.8831236       0.0623539
0.9           2.2952314       2.3801984       0.0849671
1.2           3.0569430       3.1799415       0.1229986
1.3           3.3186373       3.4553517       0.1367144
1.4           3.5815010       3.7324000       0.1508990
1.5           3.8436511       4.0091555       0.1655044
1.6           4.1030162       4.2834838       0.1804676
1.7           4.3573178       4.5530263       0.1957085
1.8           4.6040496       4.8151763       0.2111267
1.9           4.8404546       5.0670528       0.2265982
2.0           5.0635000       5.3054720       0.2419719
Press any key to continue . . .


and when it is 0.001:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Step Size (0.1, 0.01, 0.001, 0.0001): 0.001
End points: 0 2
Initial value of Y: .5
X-Value         Euler         Analytical       Error
0.0           0.5000000       0.5000000       0.0000000
0.1           0.6573370       0.6574145       0.0000775
0.2           0.8291384       0.8292986       0.0001603
0.3           1.0148221       1.0150706       0.0002485
0.4           1.2137452       1.2140877       0.0003425
0.5           1.4251970       1.4256394       0.0004424
0.6           1.6483921       1.6489406       0.0005485
0.7           1.8824627       1.8831236       0.0006609
0.8           2.1264496       2.1272295       0.0007799
0.9           2.3792929       2.3801984       0.0009056
1.0           2.6398211       2.6408591       0.0010380
Press any key to continue . . .


The problem is it randomly won't output the correct iterations from 0.0 - 2.0 incrementing every 0.1. The only time it works is when the step size equal 0.0001. Any thoughts or suggestions?
Last edited on
Hey everyone! Nevermind, I fixed the problem. When I used a counter variable for incrementing x instead of the if(int(x*10000)%1000 == 0) the iterations came out correctly.
Topic archived. No new replies allowed.