Monte Carlos Method: Particular Equation Style Required

I require a numerical program to generate a random equation for me with the requirements listed below. This curve is for a velocity profile of an object, must have positive velocity at all times and has limits on the maximum acceleration and deceleration. I desire to create a slightly different curve each time I generate one, still meeting all the requirements.

The requirements for the function, v(t) for 0 < t < t_max, is:

global max < b

global min = 0

v(0) = v(t_max) = 0

v ' (t)_max <= a

v ' (t)_min >= c

integral ( v(t) ) = d

The following two requirements, as seen on the image, add no new information to the problem:

v ' (0) <= a

v ' (t_max) >= c

An image with this information is shown below:

Any help on this problem would be appreciated. If it is very lengthy to explain how to do kindly point me in the right direction as I have some maths textbooks.

Re: Monte Carlos Method: Particular Equation Style Required

1. is superfluous if Unless, as I suspect is the case, is a number you know ahead-of-time. Certainly, if on the interval in question, then is the distance traveled, whatever that is.

2. Depending on the maximum slopes of the velocity, a and c, and the required distance traveled, d, you may not be able to produce a function satisfying all those requirements. In particular, if the required distance traveled, d, is large compared with a and c, you will not be able to exhibit the required function.

3. My solution: From the origin, start rising at the maximum slope a. Backwards from the endpoint, descend at the maximum slope c. Then fiddle with what's in-between those two lines to make the area equal to d. You could try to make the area under the extreme lines equal to half of d, and account for the other half of d by various shenanigans that allow you to change things and still keep the same area.

Re: Monte Carlos Method: Particular Equation Style Required

Notice that if you have n functions which satisfy the constraints, then any normalised linear combinations of them will also match the constraints

ie, if you have a collection of n functions which satisfy the contraints, then the following does as well:

with the resitrictions and

So, you can generate your fluctuations by randomising the weights . My suggested algorithm for you would be:
(1) Manually find a few (say, 10) functions which satisfy your contraints using any method (eg, trial and error). This should be feasible enough, and youll only have to do it once.
(2) use a random number generator to assign a weight to each function you found in (1)
(3) then plot the function g(t) corresponding to those randomly chosen weights

to create more realisations, you can generate a new set of random numbers for step (2).

.

Notes

To Generate random weights you can generate random numbers by any method (all maths programs shold be able to do this), then use

a particularly simple implementation is with n=2 since you could then use a single random number generator to get betgween 0 and 1, then use

Obviously the n functions you find will need to be dsignificantly different if you want to see significant fluctions

the types of paths you observe will be heavily dependent on the shapes of the functions you choose, which may be unrealistic for your purpose. i would try to choose functions that look different to each other.

PS: Although i started this post by saying "notice that", you should check the result in case i made a mistake.

Last edited by SpringFan25; July 29th 2011 at 01:27 PM.
Reason: Clarification and some more detail on the method.

Re: Monte Carlos Method: Particular Equation Style Required

Yes, d is known and the values a, b, c are also known. Yes, since this is a velocity profile the derivative is acceleration and the integration is the displacement of the object.

For example, let us choose the values of:

distance (integral) = d = 74m

max accn = a = 2m/s^2

max dccn = c = -4m/s^2

t_max = 20 seconds.

One function which achieves this is a max acceleration for 2 seconds, holding 4m/s for 17 seconds, and max deceleration for 1 second. This generates a function in a trapezium shape with no negative values, meeting all the requirements.

I wish, however, to find a function (continuous) which meets these conditions (notice I have removed the constraint for the global maximum (b) as this is taken care of by the maximum rates of change).

SpringFan25:

Notice that if you have n functions which satisfy the constraints, then any normalised linear combinations of them will also match the constraints

Yes, your idea of normalised linear combinations is a good idea. My first question would be: how to I generate the continuous functions in the first place?

I have the following ideas so far:

A numerical solver to solve y = a.x^(n) + b.x^(n-1) + ... &c. with the various constrains put into the numerical program.

Re: Monte Carlos Method: Particular Equation Style Required

Minor point: I think that trapezium functions are continuous but not differentiable. I agree that they meet the conditions as written in post #1.

Major point: if you are going to allow non-differentiable functions then the problem is dramatically simplified. You can just start/finish your graph with two short lines with slope a and c respectively. For simplicity, choose the length of those lines so that the area underneath them is 1.

Now all you need to do is choose some random points in the middle and connect them with straight lines. Scale up/down these middle points by a constant factor so that the area under them is d-1. This will bring the total area under the graph to as required.

If re-scaling causes the global maximum constraint to be breached, discard your middle points points and randomly choose some more. Alternatively reduce the highest point and then repeat the scaling factor calculation.

Edit: I think ackbeet already suggested the above in post #2.

Many combinations will be discarded but a computer could make thousands/millions of attempts in a very short time.

Poosible Smooth functionsNotice that there is no constraint on v' except at t_max and t_min. Trigonometric functions (specifically, combinations of sine waves) look promising if you run out of simpler options (polynomials etc):

they can be combined to make many different shapes.

Because they are periodic they can be easily parameterized to meet the slope restrictions at t_min and t_max.

An upper bound on the global maximum is also easy to compute. (the global maximum cant be more than the sum of the amplitudes of your waves)

The integrals should also be tractable as long as you confine yourself to simple functions, eg combinations of sine waves with different periods/phases/amplitudes. The integral calculations will be slightly easier if you chose half-periods that fit into the period (t_max - t_min) a whole number of times.

One minor challenge will be ensuring that the global minimum is not breached but this can be overcome by trial and error.

PS: I expect the above is tractable but i haven't actually tried it myself! I wont look at it further unless you actually need smooth functions.

Re: Monte Carlos Method: Particular Equation Style Required

it was too much fun to ignore so I've attached an excel implementation of the smooth trig functions approach i suggested above.

The main changes from my above post are

t_min is assumed 0

i was wrong to say that there was no restriction on v'() except at t_min and t_max. i had misread your first post.

All sine waves complete an odd number of half periods in the time interval. This makes all graphs symettric so v(0) = v(tmax) =0. We also have v'(0) = -v(t_max).

To simplify calculations all sine waves have a phase shift of 0 or

Ive used a stronger gradient constraint than you need. I used |v'(t)| < min(|a|,|c|). This is useful because an upper bound on |v'(t)| can be easily calculated from the amplitude and period of the sine waves (derivation below)

There is a big "calculate me" button to generate a new random graph. it requires macros enabled to run. I dont accept responsibility if this breaks your computer/life etc.

Upper bound on |v'(t)| I'll derive this. Suppose v(t) is the sum of sine waves with ammplitude , period , and phase-shift as follows:

Differentiate the first equation. This gives:

Since it follows that

And we can sum as many sine waves as we want, adding up the upper bounds for each. This is handy since v(t) is the sum of the sine waves:

(the left hand inequality is valid. Think "triangle inequality" with lots of variables.)
How does this help?
Note that
and,

Combine the above pair of results:

it follows that
So testing that will ensure that the acceleration constraints are met.

Instead of evaluating |v'(t)| everywhere, It's sufficient to test the upper bound of |v'(t)| that was derived in the previous part of this post. ie the test is:

Meeting all the constraintsfor the rest of this post assume that v(t) is constructed as follows:

v(0) = v(tmax) = 0This is the easiest one. Consider time 0:

so a sufficient condition for is

so the phase shift should be . Since the period is anyway, so only unique phase shifts are 0 and .

What about ?
The sin function returns to its starting point every half-period. So its suffiient to ensure that the interval has a whole number of half periods in it. Then we can use .

(in fact, we will want an odd number of half-periods later on.).

c < v'(t) < a
I already showed that a sufficient condition is

Total displacement = d
This is the tricky one. It's very convenient to restrict the period of each sine wave so that an odd number of half periods fit in the interval . This means that the number of completed oscilations will always be 1.5, 2.5, 3.5, 4.5 etc. This is important because the integral of a full oscillation is always zero.

So we only need to compute the integral of the remaining half oscillation of each wave. We already established the the phase shift is 0 or . For the case where the phase shift is 0, This area is equal to:

For the case where the phase shift is the half-period area is equal to

But remember that the sin function has this property:

So for the phase shifted case the area is just -1 times the area we just calculated:

Finally, a single formula covering both cases:

(if you dont udnerstand what the -1 factor is doing at the end, it evaluates to 0 if there is no phase shift, and -1 if there is a phase shift)

Hence the area under the graph is:

We require the area under the graph to be exactly d. This is easy (compared to all that integration!). Simply set new coefficients:

This scales up the entire function so that the total area will be d.

Obviously, this scale up/down may brake the acceleration constraints, since the slopes will also be scaled. The attached workbook checks this, and tries a new set of if there is a breach.

Algorithm1) Randomly choose subject to the rules derived above, ie:

= 0 or

2) Calcualte adjusted

3) Test the adjusted coefficients meet the accelleration constraints. If they dont, discard and return to step 1. The test is

The attached workbook does the above. I've chosen ranges on that produce reasonably varied output.

However these functions are not ideal as the low gradient at the start means that small fluctuations from any other waves you put on top can easily make the overall value of v(t) negative at low values of t.