11 void addY(vector
<float> *y
) { this->y
= y
; }
12 void addDYDX(vector
<float> *dydx
) { this->dydx
= dydx
; }
13 void setDerivs(void (*pderivs
)(float, vector
<float> *, vector
<float> *));
15 virtual void getNextCalc(void) {};
16 void setN(unsigned int n
) { this->n
= n
; }
17 void setStep(float step
) { this->step
= step
; }
18 void setX(float x
) { this->x
= x
; }
22 void (*pderivs
)(float, vector
<float> *, vector
<float> *);
28 void odeSolver::printY(void)
30 vector
<float>::iterator it
;
32 for (it
= y
->begin(); it
!= y
->end(); it
++)
35 void odeSolver::setDerivs(void (*pderivs
)(float,
39 this->pderivs
= pderivs
;
42 // Based on code from "Numerical Recipes in C, 2nd Ed."
43 class odeSolverRK4
: public odeSolver
46 void getNextCalc(void)
50 vector
<float> dym(n
), dyt(n
), yt(n
);
57 for (i
= 0; i
< n
; i
++)
58 yt
[i
] = (*y
)[i
] + hh
* (*dydx
)[i
];
59 pderivs(xh
, &yt
, &dyt
);
62 for (i
= 0; i
< n
; i
++)
63 yt
[i
] = (*y
)[i
] + hh
* dyt
[i
];
64 pderivs(xh
, &yt
, &dym
);
67 for (i
= 0; i
< n
; i
++) {
68 yt
[i
] = (*y
)[i
] + step
* dym
[i
];
71 pderivs(x
+ step
, &yt
, &dyt
);
74 for (i
= 0; i
< n
; i
++)
75 (*y
)[i
] = (*y
)[i
] + h6
* ((*dydx
)[i
] + dyt
[i
] + 2.0 * dym
[i
]);
82 void myderivs(float x
, vector
<float> *y
, vector
<float> *dydx
)
85 (*dydx
)[0] = x
* cos(x
* (*y
)[0]);
102 myos
.setDerivs(myderivs
);
107 for (int i
= 0; i
< 1000; i
++) {