do loop only running once

I am writing a program to solve a quadratic function iteratively by having values of x converge, with the program solving for x until the difference between the new value of x and the old value of x is less then 0.0001. My do loop is only executing once and I cannot figure out why.

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
#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

double a;
double b;
double c;
double difference;
double x = 4;
double Xnew;

int main(){
	//this section asks for the three numbers of the quadratic function
	cout << "a = ";
	cin >> a;
	cout << endl << "b = ";
	cin >> b;
	cout << endl << "c = ";
	cin >> c;

	do {
		Xnew = ((10 * x) - 6 / (3 * x));
		difference = abs(x) - abs(Xnew);
		cout << Xnew << " , " << difference;
	
	} while(difference > 0.0001 && difference < -0.0001);
}
Hmm ... while(difference > 0.0001 && difference < -0.0001);
I can't think of any numbers which are both positive and a little bit larger than zero and also are negative and a little bit less than zero, all at the same time.

You might find abs(difference) useful.
http://www.cplusplus.com/reference/cmath/abs/
Last edited on
In line 28, do you mean the opposite, i.e.

while(difference <= 0.0001 && difference > -0.0001);
I changed line 28 (revised code below), but the loop is still only running once.

 
(abs(difference) < 0.0001 && abs(difference) > -0.0001)
What's the point of the second comparison?

abs() will always return a positive number which will be covered by the first test. Comparing a positive number > a negative number will always be true.
You've combined 2 positives to make a negative. Why compare against negative values after fetching the absolute value?

abs(x) will always give you a positive value or 0.

abs(difference) will therefore always be greater than -0.0001

You want something like while (abs(difference) < 0.0001)

Unfortunately, you're still in trouble, because abs() expects and returns int, so your doubles will all be truncated and you end up comparing a bunch of zeros.

If you want to loop UNTIL the difference falss in the range -0.0001 t0 +0.0001, try:

while(difference > 0.0001 || difference < -0.0001);

However, looking at your code, you're headed for an infinite loop, since you don't change x in the loop. You'll just get the same results over and over again.

or do this: difference = abs(x - Xnew); and while (difference > 0.00001);

The calculations look a bit suspect. The values of a, b and c are not used.

What I would expect to see is the evaluation of f(x) and f'(x). the next value of x is then calculated as x - f(x) / f'(x)

See for example: http://www.analyzemath.com/calculus/applications/newtons_method.html
I revised the program code again, actually using a, b, and c this time, and fixing the loop, but after I enter the a, b, and c, I'm getting this:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// revised code
#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

double a;
double b;
double c;
double difference;
double x = 4;
double Xnew;

int main(){
	//this section asks for the three numbers of the quadratic function
	cout << "a = ";
	cin >> a;
	cout << endl << "b = ";
	cin >> b;
	cout << endl << "c = ";
	cin >> c;

	do {
		Xnew = ((a * x) - b / (c * x));
		difference = x - Xnew;
		cout << Xnew << " , " << difference << endl;
		x = Xnew;
	} while(difference > 0.0001 || difference < -0.0001);
}

//output
3.94949e+096 , -3.55454e+096
3.94949e+097 , -3.55454e+097
3.94949e+098 , -3.55454e+098
3.94949e+099 , -3.55454e+099
3.94949e+100 , -3.55454e+100
3.94949e+101 , -3.55454e+101
3.94949e+102 , -3.55454e+102
3.94949e+103 , -3.55454e+103
3.94949e+104 , -3.55454e+104
3.94949e+105 , -3.55454e+105
3.94949e+106 , -3.55454e+106
3.94949e+107 , -3.55454e+107
3.94949e+108 , -3.55454e+108
3.94949e+109 , -3.55454e+109
3.94949e+110 , -3.55454e+110
3.94949e+111 , -3.55454e+111
3.94949e+112 , -3.55454e+112
3.94949e+113 , -3.55454e+113
3.94949e+114 , -3.55454e+114
3.94949e+115 , -3.55454e+115
3.94949e+116 , -3.55454e+116
3.94949e+117 , -3.55454e+117
3.94949e+118 , -3.55454e+118
3.94949e+119 , -3.55454e+119
3.94949e+120 , -3.55454e+120
3.94949e+121 , -3.55454e+121
3.94949e+122 , -3.55454e+122
3.94949e+123 , -3.55454e+123
3.94949e+124 , -3.55454e+124
3.94949e+125 , -3.55454e+125
3.94949e+126 , -3.55454e+126
3.94949e+127 , -3.55454e+127
3.94949e+128 , -3.55454e+128
3.94949e+129 , -3.55454e+129
3.94949e+130 , -3.55454e+130
3.94949e+131 , -3.55454e+131
3.94949e+132 , -3.55454e+132
3.94949e+133 , -3.55454e+133
3.94949e+134 , -3.55454e+134
3.94949e+135 , -3.55454e+135
3.94949e+136 , -3.55454e+136
3.94949e+137 , -3.55454e+137
3.94949e+138 , -3.55454e+138
3.94949e+139 , -3.55454e+139
3.94949e+140 , -3.55454e+140
3.94949e+141 , -3.55454e+141
3.94949e+142 , -3.55454e+142
3.94949e+143 , -3.55454e+143
3.94949e+144 , -3.55454e+144
3.94949e+145 , -3.55454e+145
3.94949e+146 , -3.55454e+146
3.94949e+147 , -3.55454e+147
3.94949e+148 , -3.55454e+148
3.94949e+149 , -3.55454e+149
3.94949e+150 , -3.55454e+150
3.94949e+151 , -3.55454e+151
3.94949e+152 , -3.55454e+152
3.94949e+153 , -3.55454e+153
3.94949e+154 , -3.55454e+154
3.94949e+155 , -3.55454e+155
3.94949e+156 , -3.55454e+156
3.94949e+157 , -3.55454e+157
3.94949e+158 , -3.55454e+158
3.94949e+159 , -3.55454e+159
3.94949e+160 , -3.55454e+160
3.94949e+161 , -3.55454e+161
3.94949e+162 , -3.55454e+162
3.94949e+163 , -3.55454e+163
3.94949e+164 , -3.55454e+164
3.94949e+165 , -3.55454e+165
3.94949e+166 , -3.55454e+166
3.94949e+167 , -3.55454e+167
3.94949e+168 , -3.55454e+168
3.94949e+169 , -3.55454e+169
3.94949e+170 , -3.55454e+170
3.94949e+171 , -3.55454e+171
3.94949e+172 , -3.55454e+172
3.94949e+173 , -3.55454e+173
3.94949e+174 , -3.55454e+174
3.94949e+175 , -3.55454e+175
3.94949e+176 , -3.55454e+176
3.94949e+177 , -3.55454e+177
3.94949e+178 , -3.55454e+178
3.94949e+179 , -3.55454e+179
3.94949e+180 , -3.55454e+180
3.94949e+181 , -3.55454e+181
3.94949e+182 , -3.55454e+182
3.94949e+183 , -3.55454e+183
3.94949e+184 , -3.55454e+184
3.94949e+185 , -3.55454e+185
3.94949e+186 , -3.55454e+186
3.94949e+187 , -3.55454e+187
3.94949e+188 , -3.55454e+188
3.94949e+189 , -3.55454e+189
3.94949e+190 , -3.55454e+190
3.94949e+191 , -3.55454e+191
3.94949e+192 , -3.55454e+192
3.94949e+193 , -3.55454e+193
3.94949e+194 , -3.55454e+194
3.94949e+195 , -3.55454e+195
3.94949e+196 , -3.55454e+196
3.94949e+197 , -3.55454e+197
3.94949e+198 , -3.55454e+198
3.94949e+199 , -3.55454e+199
3.94949e+200 , -3.55454e+200
3.94949e+201 , -3.55454e+201
3.94949e+202 , -3.55454e+202
3.94949e+203 , -3.55454e+203
3.94949e+204 , -3.55454e+204
3.94949e+205 , -3.55454e+205
3.94949e+206 , -3.55454e+206
3.94949e+207 , -3.55454e+207
3.94949e+208 , -3.55454e+208
3.94949e+209 , -3.55454e+209
3.94949e+210 , -3.55454e+210
3.94949e+211 , -3.55454e+211
3.94949e+212 , -3.55454e+212
3.94949e+213 , -3.55454e+213
3.94949e+214 , -3.55454e+214
3.94949e+215 , -3.55454e+215
3.94949e+216 , -3.55454e+216
3.94949e+217 , -3.55454e+217
3.94949e+218 , -3.55454e+218
3.94949e+219 , -3.55454e+219
3.94949e+220 , -3.55454e+220
3.94949e+221 , -3.55454e+221
3.94949e+222 , -3.55454e+222
3.94949e+223 , -3.55454e+223
3.94949e+224 , -3.55454e+224
3.94949e+225 , -3.55454e+225
3.94949e+226 , -3.55454e+226
3.94949e+227 , -3.55454e+227
3.94949e+228 , -3.55454e+228
3.94949e+229 , -3.55454e+229
3.94949e+230 , -3.55454e+230
3.94949e+231 , -3.55454e+231
3.94949e+232 , -3.55454e+232
3.94949e+233 , -3.55454e+233
3.94949e+234 , -3.55454e+234
3.94949e+235 , -3.55454e+235
3.94949e+236 , -3.55454e+236
3.94949e+237 , -3.55454e+237
3.94949e+238 , -3.55454e+238
3.94949e+239 , -3.55454e+239
3.94949e+240 , -3.55454e+240
3.94949e+241 , -3.55454e+241
3.94949e+242 , -3.55454e+242
3.94949e+243 , -3.55454e+243
3.94949e+244 , -3.55454e+244
3.94949e+245 , -3.55454e+245
3.94949e+246 , -3.55454e+246
3.94949e+247 , -3.55454e+247
3.94949e+248 , -3.55454e+248
3.94949e+249 , -3.55454e+249
3.94949e+250 , -3.55454e+250
3.94949e+251 , -3.55454e+251
3.94949e+252 , -3.55454e+252
3.94949e+253 , -3.55454e+253
3.94949e+254 , -3.55454e+254
3.94949e+255 , -3.55454e+255
3.94949e+256 , -3.55454e+256
3.94949e+257 , -3.55454e+257
3.94949e+258 , -3.55454e+258
3.94949e+259 , -3.55454e+259
3.94949e+260 , -3.55454e+260
3.94949e+261 , -3.55454e+261
3.94949e+262 , -3.55454e+262
3.94949e+263 , -3.55454e+263
3.94949e+264 , -3.55454e+264
3.94949e+265 , -3.55454e+265
3.94949e+266 , -3.55454e+266
3.94949e+267 , -3.55454e+267
3.94949e+268 , -3.55454e+268
3.94949e+269 , -3.55454e+269
3.94949e+270 , -3.55454e+270
3.94949e+271 , -3.55454e+271
3.94949e+272 , -3.55454e+272
3.94949e+273 , -3.55454e+273
3.94949e+274 , -3.55454e+274
3.94949e+275 , -3.55454e+275
3.94949e+276 , -3.55454e+276
3.94949e+277 , -3.55454e+277
3.94949e+278 , -3.55454e+278
3.94949e+279 , -3.55454e+279
3.94949e+280 , -3.55454e+280
3.94949e+281 , -3.55454e+281
3.94949e+282 , -3.55454e+282
3.94949e+283 , -3.55454e+283
3.94949e+284 , -3.55454e+284
3.94949e+285 , -3.55454e+285
3.94949e+286 , -3.55454e+286
3.94949e+287 , -3.55454e+287
3.94949e+288 , -3.55454e+288
3.94949e+289 , -3.55454e+289
3.94949e+290 , -3.55454e+290
3.94949e+291 , -3.55454e+291
3.94949e+292 , -3.55454e+292
3.94949e+293 , -3.55454e+293
3.94949e+294 , -3.55454e+294
3.94949e+295 , -3.55454e+295
3.94949e+296 , -3.55454e+296
3.94949e+297 , -3.55454e+297
3.94949e+298 , -3.55454e+298
3.94949e+299 , -3.55454e+299
3.94949e+300 , -3.55454e+300
3.94949e+301 , -3.55454e+301
3.94949e+302 , -3.55454e+302
3.94949e+303 , -3.55454e+303
3.94949e+304 , -3.55454e+304
3.94949e+305 , -3.55454e+305
3.94949e+306 , -3.55454e+306
3.94949e+307 , -3.55454e+307
1.#INF , -1.#INF
1.#INF , -1.#IND
Press any key to continue . . .
Last edited on
In one way that looks like progress, it is actually looping. But the calculation and testing of difference will need to use abs() - at least if you want to keep things simple.

I'm not sure what formula is being used in the calculation:
 
    Xnew = ((a * x) - b / (c * x));
it doesn't look at all like the Newton-Raphson method.

Which algorithm are you using to derive Xnew?
Topic archived. No new replies allowed.