Iniciar sesiónCrear cuenta

# Dev - C++ - Ayuda con Codigo de Atractor y uso de if

Vista:

## Ayuda con Codigo de Atractor y uso de if

Publicado por Alejandra (7 intervenciones) el 16/05/2015 23:14:27
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
```#include <cmath>
```
```#include <cstdlib>
```
```#include <fstream>
```
```#include <iostream>
```
```#include <math.h>
```
`using namespace std;`
```#include "vector.hpp"
```
`using namespace cpl;`
```double sigma = 10; // Lorenz model constants in textbook
```
`double b = 8.0 / 3.0;`
`double r = 28;`
`Vector f(Vector txyz) {`
`	double t = txyz[0];`
`	double x = txyz[1];`
`	double y = txyz[2];`
`	double z = txyz[3];`
`	Vector f(4);`
`	f[0] = 1;`
`    f[1] = - sigma * x + sigma * y;`
`    f[2] = - x * z + r * x - y;`
`    f[3] = x * y - b * z;`
`    return f;`
`}`
` `
```void RK4Step( // 4th order Runge-Kutta
```
```Vector& y, // extended solution vector
```
```double h) // step size
```
`{`
`        Vector k1 = h * f(y);`
`    Vector k2 = h * f(y + 0.5 * k1);`
`    Vector k3 = h * f(y + 0.5 * k2);`
`    Vector k4 = h * f(y + k3);`
`    y += (k1 + 2 * k2 + 2 * k3 + k4) / 6.0;`
`}`
` `
```Vector txyz(4); // global variable to hold t,x,y,z
```
```void initialize() { // initial conditions in textbook
```
` `
`        txyz[0] = 0.0;`
`    txyz[1] = 0.0;`
`    txyz[2] = 1.0;`
`    txyz[3] = 0.0;`
`}`
` `
```double dt = 0.001; // time step for integration
```
`void findNextCrossing() {`
```        Vector txyzOld = txyz; // save the old point
```
` `
`    while (true) {`
```        RK4Step(txyz, dt); // take a step
```
```        // check whether y changes sign, i.e., crosses y = 0
```
`        if (txyz[2] * txyzOld[2] < 0)`
`            break;`
`        else`
`            txyzOld = txyz;`
`    }`
` `
```// use linear interpolation like in projectile.cpp to find intersection
```
`double r = txyzOld[2] / txyz[2];`
`for (int i = 0; i < 4; i++)`
`    txyz[i] = (txyzOld[i] + r * txyz[i]) / (r + 1);`
`}`
` `
`int main(int argc, char *argv[]) {`
`        cout << " Trajectory and Poincare Section for the Lorenz Attractor\n"`
`         << " using 4th order Runge-Kutta with time step dt = " << dt << "\n"`
`         << " sigma = " << sigma << ", b = " << b << ", r = " << r << endl;`
` `
`    initialize();`
`    cout << " initial conditions: x = " << txyz[1] << "\t"`
`         << ", y = " << txyz[2] << "\t" << ", z = " << txyz[3] << endl;`
` `
```    // transient trajectory
```
`    double t = 50;`
`    string fileName = "transient.data";`
`    cout << " Integrating to time t = " << t << "\n"`
`         << " trajectory in file " << fileName << endl;`
`    ofstream dataFile(fileName.c_str());`
`    dataFile << txyz[0] << "\t" << txyz[1] << "\t"`
`             << txyz[2] << "\t" << txyz[3] << "\n";`
`    int step = 0;`
`    int skip = 5;`
`    while (txyz[0] < t) {`
`        RK4Step(txyz, dt);`
```        if (++step % skip != 0) // record every skip steps
```
`           continue;`
`        dataFile << txyz[0] << "\t" << txyz[1] << "\t"`
`                 << txyz[2] << "\t" << txyz[3] << "\n";`
`    }`
`    dataFile.close();`
```    // Poincare section
```
`    fileName = "section.data";`
`    int points = 1000;`
`    cout << " Finding " << points << " Poincare section points at y = 0\n"`
`        << " section data in file " << fileName << endl;`
`    dataFile.open(fileName.c_str());`
`    for (int point = 0; point < points; point++) {`
` `
`        findNextCrossing();`
`        dataFile << txyz[0] << "\t" << txyz[1] << "\t"`
`                 << txyz[2] << "\t" << txyz[3] << "\n";`
`    }`
`    t = 1000;`
` `
` `
`         for (int point = 0; point < points; point++) {`
`                RK4Step(txyz, dt);`
`                if ((txyz[2] = sqrt(b * (r - 1))) && (txyz[1] >= sqrt(b *(r - 1))))`
`                              { cout << " z = " << txyz[3] << "," << "1"  << endl;`
` `
` `
`                  }`
`                if ((txyz[2] = (-1) * sqrt(b * (r - 1))) && (txyz[1] <= (-1) * (sqrt(b * (r - 1)))))`
`                     { cout << " z = " << txyz[3] << "," << "0" << endl; }`
` `
`                }`
` `
`}`

Buenas ese es mi código pero no se que ocurre al final con los ciclos for e if tengo un problema no me entra a los ciclos, entonces no se que estoy colocando mal.
2do) En el segundo if, si yo coloco else if me arroja un resultado distinto, alguien podria ayudarme?
Gracias de antemano
Valora esta pregunta
0

## Ayuda con Codigo de Atractor y uso de if

Publicado por vangodp (287 intervenciones) el 17/05/2015 04:17:32
con if else se te imprime una cosa u otra. Ya con 2 ifs son 2 cosas aparte pueden suceder una u otra, o incluso ambas.

No se que problema tienes pero al ejecutar tu código me da:
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
`Trajectory and Poincare Section for the Lorenz Attractor`
` using 4th order Runge-Kutta with time step dt = 0.001`
` sigma = 10, b = 2.66667, r = 28`
` initial conditions: x = 0      , y = 1 , z = 0`
` Integrating to time t = 50`
` trajectory in file transient.data`
` Finding 1000 Poincare section points at y = 0`
` section data in file section.data`
` z = 39.4771,1`
` z = 39.2799,1`
` z = 39.085,1`
` z = 38.8922,1`
` z = 38.7016,1`
` z = 38.513,1`
` z = 38.3266,1`
` z = 38.1422,1`
` z = 37.9599,1`
` z = 37.7796,1`
` z = 37.6014,1`
` z = 37.4251,1`
` z = 37.2507,1`
` z = 37.0783,1`
` z = 25.8854,0`
` z = 25.8884,0`
` z = 25.8914,0`
` z = 25.8944,0`
` z = 25.8973,0`
` z = 25.9003,0`
` z = 25.9033,0`
` z = 25.9063,0`
` z = 25.9092,0`
` z = 25.9122,0`
` z = 25.9151,0`
` z = 25.918,0`
` z = 25.921,0`
` z = 25.9239,0`
` z = 25.9268,0`
` z = 25.9297,0`
` z = 25.9326,0`
` z = 25.9355,0`
` z = 25.9383,0`
` z = 25.9412,0`
` z = 25.9441,0`
` z = 25.9469,0`
` z = 25.9498,0`
` z = 25.9526,0`
` z = 25.9555,0`
` z = 25.9583,0`
` z = 25.9611,0`
` z = 25.9639,0`
` z = 25.9667,0`
` z = 25.9695,0`
` z = 25.9723,0`
` z = 25.9751,0`
` z = 25.9779,0`
` z = 25.9807,0`
` z = 25.9834,0`
` z = 25.9862,0`
` z = 25.9889,0`
` z = 25.9917,0`
` z = 25.9944,0`
` z = 25.9971,0`
` z = 25.9998,0`
` z = 26.0026,0`
` z = 26.0053,0`
` z = 26.008,0`
` z = 26.0106,0`
` z = 26.0133,0`
` z = 26.016,0`
` z = 26.0187,0`
` z = 26.0213,0`
` z = 26.024,0`
` z = 26.0266,0`
` z = 26.0293,0`
` z = 26.0319,0`
` z = 26.0346,0`
` z = 26.0372,0`
` z = 26.0398,0`
` z = 26.0424,0`
` z = 26.045,0`
` z = 26.0476,0`
` z = 26.0502,0`
` z = 26.0528,0`
` z = 26.0553,0`
` z = 26.0579,0`
` z = 26.0605,0`
` z = 26.063,0`
` z = 26.0656,0`
` z = 26.0681,0`
` z = 26.0706,0`
` z = 26.0732,0`
` z = 26.0757,0`
` z = 26.0782,0`
` z = 26.0807,0`
` z = 26.0832,0`
` z = 26.0857,0`
` z = 26.0882,0`
` z = 26.0907,0`
` z = 26.0931,0`
` z = 26.0956,0`
` z = 26.0981,0`
` z = 26.1005,0`
` z = 26.103,0`
` z = 26.1054,0`
` z = 26.1078,0`
` z = 26.1103,0`
` z = 26.1127,0`
` z = 26.1151,0`
` z = 26.1175,0`
` z = 26.1199,0`
` z = 26.1223,0`
` z = 26.1247,0`
` z = 26.1271,0`
` z = 26.1294,0`
` z = 26.1318,0`
` z = 26.1342,0`
` z = 26.1365,0`
` z = 26.1389,0`
` z = 26.1412,0`
` z = 26.1436,0`
` z = 26.1459,0`
` z = 26.1482,0`
` z = 26.1506,0`
` z = 26.1529,0`
` z = 26.1552,0`
` z = 26.1575,0`
` z = 26.1598,0`
` z = 26.1621,0`
` z = 26.1644,0`
` z = 26.1666,0`
` z = 26.1689,0`
` z = 26.1712,0`
` z = 26.1734,0`
` z = 26.1757,0`
` z = 26.1779,0`
` z = 26.1802,0`
` z = 26.1824,0`
` z = 26.1846,0`
` z = 26.1869,0`
` z = 26.1891,0`
` z = 26.1913,0`
` z = 26.1935,0`
` z = 26.1957,0`
` z = 26.1979,0`
` z = 26.2001,0`
` z = 26.2023,0`
` z = 26.2044,0`
` z = 26.2066,0`
` z = 26.2088,0`
` z = 26.2109,0`
` z = 26.2131,0`
` z = 26.2152,0`
` z = 26.2174,0`
` z = 26.2195,0`
` z = 26.2216,0`
` z = 26.2238,0`
` z = 26.2259,0`
` z = 26.228,0`
` z = 26.2301,0`
` z = 26.2322,0`
` z = 26.2343,0`
` z = 26.2364,0`
` z = 26.2385,0`
` z = 26.2406,0`
` z = 26.2426,0`
` z = 26.2447,0`
` z = 26.2468,0`
` z = 26.2488,0`
` z = 26.2509,0`
` z = 26.2529,0`
` z = 26.255,0`
` z = 26.257,0`
` z = 26.259,0`
` z = 26.261,0`
` z = 26.2631,0`
` z = 26.2651,0`
` z = 26.2671,0`
` z = 26.2691,0`
` z = 26.2711,0`
` z = 26.2731,0`
` z = 26.2751,0`
` z = 26.277,0`
` z = 26.279,0`
` z = 26.281,0`
` z = 26.283,0`
` z = 26.2849,0`
` z = 26.2869,0`
` z = 26.2888,0`
` z = 26.2908,0`
` z = 26.2927,0`
` z = 26.2946,0`
` z = 26.2966,0`
` z = 26.2985,0`
` z = 26.3004,0`
` z = 26.3023,0`
` z = 26.3042,0`
` z = 26.3061,0`
` z = 26.308,0`
` z = 26.3099,0`
` z = 26.3118,0`
` z = 26.3137,0`
` z = 26.3156,0`
` z = 26.3174,0`
` z = 26.3193,0`
` z = 26.3212,0`
` z = 26.323,0`
` z = 26.3249,0`
` z = 26.3267,0`
` z = 26.3286,0`
` z = 26.3304,0`
` z = 26.3322,0`
` z = 26.3341,0`
` z = 26.3359,0`
` z = 26.3377,0`
` z = 26.3395,0`
` z = 26.3413,0`
` z = 26.3431,0`
` z = 26.3449,0`
` `
`Process returned 0 (0x0)   execution time : 7.873 s`
`Press any key to continue.`
Por si te interesa.
No te puedo ayudar mucho por que no soy tan espabilado en las mates, no se que quieres hacer con ese código.
Valora esta respuesta
0