How to use pointers in numerical methods?

The aim of this piece of code is to perform a numerical approximation of how the velocity vector of a particle changes over time when it's in a magnetic field.
Two methods are used: Euler's method and the Leapfrog method. Euler's only needs the starting velocity but it becomes unaccuarate after a few iterations. The leapfrog method is more stable but it needs the initial velocity and the velocity in the previous time step. Thus I have to use the Euler's method to find the velocity at instant t=0.01 and the use the initial velocity and that one for the leapfrog method.

Issue: by using the Euler method I vary the inital velocity, say v0. The leapfrog method is in a loop nested in the euler's method loop.


for(int t=0 ; t < 1; ++t)
{
//Euler's method

threevector v = v0 + (v0^b)*dt;

// why dt*(V0^b) doesn-t work does it have to do with how the program performs the tasks?
v0 =v;

//why changing the other from v=V0 to V0=v affects the results?


for ( int f=0; f<20; f++)
{
//Leapfrog method.
threevector v0;//previous instant
threevector v1;//actual velocity
threevector v2;//what I want to calculate.
I have to assign the value of v to v0 and the original value of v0 to v1.

v2= v0+(v1^b)*2*dt; //this is the approx.

}

}

HERE COMES THE QUESTION, (sorry for so much hassle). the leapfrog for loop is nested inside the euler-s method loop, hence: the value of v0(my initial speed) has changed to that of v. Thus I have two vectors v0 and v that have the same value. What I want is to be able to keep v0's value constant and just using it to point to the previous v value to perform the first method.

Another posibility would be to take the leapfrog loop out but then how can I access the value of v?

I may need to rewrite the question as it is fairly messy. Trying to sum it up again I want to know how to point to v0 original value at the same time I use it for an interation where I need it's value to change all the time.

Any ideas
^ in C and C++ is XOR not exponent.

Try seperating out the inner loop into a seperate function. This'll force you to think about the parameters more clearly. You'll have a clear understanding what's a parameter and what's local to the loop.
Sorry I did not make it clear. the XOR is overloaded in the class I wrote to calculate the cross/product between two vectors. It is no longer a XOR.

I had thought about separating the loops but then how do I access what is produced in one loop from the main or from inside another loop in the main
You can pass parameters by value/reference as necessary.
Aside

Note that it is seen as bad form to overload an operator to do some something different to its normal behaviour. The standard form of the rule is "preserve natural semantics for overloaded operators" [Sutter and Alexandrescu, "C++ Coding Standards"].

kbw's question illustrates why.

While it's naff, you should probably do something along the lines of:

v2= v0+dot_prod(v1,b)*2*dt; //this is the approx.

Andy
I want to know how to point to v0 original value at the same time I use it for an interation where I need it's value to change all the time.


You need to introduce a new variable. One variable cannot be both constant and changing.

1
2
3
const threevector v0_0 = v0; // not a ref/with a better name

modify v0...
Last edited on
Topic archived. No new replies allowed.