Help with graph function (linear growth with plateau)

kernowboy

New member
Joined
Apr 8, 2009
Messages
7
I need to draw a very particular kind of line (approximating it using a logarithmic curve is not going to be sufficient). Any help would be very gratefully appreciated!

qcgqnEs.png


The line will be straight from 0,0 to Point_A.

It will be a curve from Point_A to 1,1.
The gradient of this curved section will initally be the same as the gradient of the straight section, but will decrease gradually (linearly), reaching 0 when x=1.

Point_A will always have X and Y coordinates in the range 0 to 1.
Point_A will also always be above the straight line from 0,0 to 1,1 (ie. in the half of the chart shaded darker), in case that makes a difference.

For any value of X between 0 and 1, I need a formula to calculate Y.
In fact, it really just needs to be for any vaue of X between Ax and 1, since the straight section is relatively trivial.


EZYniGO.png


Here's what I've tried so far...
I'm extending the line from 0,0 to A, to find the value of Y when X is 1 (see the pink line).
When X=1, Y = Ay / Ax
eg. Y = 0.5 / 0.25 = 2

The pink straight line is always above the ideal curved line, but by how much?
I now know that when X=1, the difference between the Y values of my straight line and my ideal curved line is Ay / Ax (which is 2 in this example) .
At X=Ax, the difference is 0.
For any given point between them, the difference will be somewhere between - but it's not a linear relationship.

For any given point on the line (Point_P), I'm finding 1 - Px (Line_B), and expressing it as a proportion of 1 - Ax (Line_C).
B = (Px - Ax) / (1 - Ax)

When Px=Ax, B=0.
When Px=1, B=1.

If I raise B to the power of 1.5, and subtract that from the straightline formula, I get the correct result here - but it only holds true for this example.
eg. If X=0.5,
B = (0.5 - 0.25) / (1 - 0.25) = 0.33333
B ^ 1.5 = 0.19245
Y = (0.5 * 0.5 / 0.25) - 0.19245 = 0.80755

So, how on Earth do I calculate the right exponent to use?
Or am I doing something totally wrong to begin with?
 
I am not sure why you bothered to extend the line passed point A, but it dos not matter.

We will find the equation of the line using the points (0,0)&(.25, .5) AND using (0,0) & (1,2).

Using (0,0)&(.25, .5) you should see rather quickly that the y value is always twice the x value. This translate to y=2x.
Using (0,0) & (1,2) you should see rather quickly that the y value is always twice the x value. This translate to y=2x.
So we have part of the equation of the graph which is: y = 2x if 0< x < .5

For x>.5: I would use an exponential graph, as you said you can't use a logarithmic graph. The equation should be in the form 1 - be^(-cx).
Since you did not post this problem in the calculus section I will supply you with something else. b*c =1

EDIT:We need for bc=0 but that makes my formula not possible.
 
Last edited:
It's not clear whether you are taking into account the desire for the slope (gradient) to be 0 at x=1.

One approach I see that would be simple geometrically would be to make the curve be a circle tangent to the initial segment and to y=1.

Another, similar to your idea, is to suppose that the curve has the form y = 1 - a(1-x)^n, where a and n are to be determined. (A logarithmic or exponential function would never have derivative 0.)

This ensures that f(1) = 1 and f'(1) = 0. You need to write equations that say f(Ax) = Bx, and f'(Ax) = Bx/Ax. Solve for a and n, and you have a possible curve. (I'd want to confirm that it looks like what you want.)
 
It's not clear whether you are taking into account the desire for the slope (gradient) to be 0 at x=1.
Sadly I did take that into account. That is why I said bc=1, which of course is wrong. Thanks!
 
Sadly I did take that into account. That is why I said bc=1, which of course is wrong. Thanks!

My comment about the slope of 0 was directed to the OP, before you wrote; though I did add in the bit about exponential functions just before I submitted, in response to you.
 
It's not clear whether you are taking into account the desire for the slope (gradient) to be 0 at x=1.

One approach I see that would be simple geometrically would be to make the curve be a circle tangent to the initial segment and to y=1.

Another, similar to your idea, is to suppose that the curve has the form y = 1 - a(1-x)^n, where a and n are to be determined. (A logarithmic or exponential function would never have derivative 0.)

This ensures that f(1) = 1 and f'(1) = 0. You need to write equations that say f(Ax) = Bx, and f'(Ax) = Bx/Ax. Solve for a and n, and you have a possible curve. (I'd want to confirm that it looks like what you want.)

Thank you both for your help!

It is important that the gradient be 0 when X=1, and not before (though I'm not concerned with any values of X outside the range Ax to 1).

I'd be delighted with any solution that works, regardless of how it works. If I knew what calculus is, perhaps I would have posted in that section of the forum, but sadly I don't :(

I'd thought about trying to use a circular arc, but is it circular?
If the gradient at 1,1 is 0, then the centre of the circle must have x=1.
The centre of the circle must also be on a line perpendicular to the line section 0,0 to A, originating from point A.
But using the example of A = x:0.25, y:0.5, that would put the centre of the circle at x:1, y:0.125.
The problem is that the distance from there to 1,1 is 0.875, while the distance to point A is 0.839, which means it can't be a circle.

I'm afraid you've completely lost me with the second suggestion - how might I go about determining "a" and "n"?

Thanks again for the help :)
 
I carried out my suggestion, and though I'd normally leave it for you to do (or tell us you can't do the calculus), the result is so nice I have to share it:


You can change a and b, or just drag P(a,b), to see the curve for different points P. I think it probably does exactly what you had in mind.
 
Thank you very much!
It's not exactly what I was aiming for, but it's close enough that I think it should probably do the job nicely.

My only criticism would be that it doesn't appear to create one continuous curve from A to 1,1 - the line tends to curve only gently at either end (close to A and 1,1) and much more sharply in the middle.

At the risk of moving the goalposts though, it got me to thinking about this:

M24xH4U.png



The purple lines are more what I was envisioning.
Firstly, I can see now that the change in gradient is probably not linear at all - the change in gradient is the same at each node, but those nodes don't appear to be spaced evenly (unless it's just my inaccurate drawing).
Now, what kind of formula would give this result, but in the form of a continuous smooth curve obviously, rather than a series of straight line sections?

Again, many thanks for all the help so far :)
 
I'm sure you realize that we can't be expected to know "exactly what you are envisioning" when you haven't really said what that "very particular kind of line" is.

It would help a lot if you could tell us the purpose of this curve, so we could determine what the real requirements are. Don't try to explain it in mathematical terms that you don't fully understand; just tell us what's important to you. (If you don't know calculus, then you don't really know what a "continuous smooth curve" or "linear change in gradient" means.)

From what I see now, you may want some sort of conic section; I can see the first example as something close to a circle, and the other as a more elongated ellipse. The "string art" appearance of your construction suggests these could both be tilted parabolas, but that would probably be too much work to handle (at least without being paid).

But I'm not going to make another attempt without knowing what the rules really are.
 
Fair enough, and my apologies. I'm very grateful for your help, and sorry I haven't been able to explain the problem correctly.
I'd fully understand if you don't want to have another go, and the version you've already provided should be plenty good enough for my needs.

Let me try to explain it another way though:

Imagine we're looking down on a car from directly above, with North being up, and East being right.
It starts out at 0,0, already facing towards Point_A.
It drives forward in a straight line until it reaches Point_A.
At this point, the driver quickly turns the steering wheel by a certain amount, such that if the car continues onward with a fixed speed and a fixed steering lock (ie. change in angle per second), it will reach 1,1 and will be facing directly East at the exact moment that it does.
If I know how far East the car is of its original position, I want to be able to calculate how far North it is.

The more I think about it in those terms, the more I think it's actually physically impossible in virtually all cases - when the car's X=1, either its Y can be =1 OR it can be heading directly East, but not both.

So yes, thanks again for your help - I think your example is going to be the best solution possible.
Incidentally, the intended use is in a game, to calculate the probability of scoring a hit, based on various factors (time spent aiming, range, etc). As it's a major part of the game, it's important that I have a system that gives me a lot of flexibility to create exactly the shape curve I want, but keeping it very simple and intuitive (the top half of a logistic function looks about right but just varying "k" doesn't give enough control, and it's not intuitive enough - "50%" is easy to visualize, but "k=2" is not).
 
Last edited:
What you are describing (constant change in angle) is going to be an exact circle. We know that can't always be done, but a horizontal ellipse (in effect, a stretched circle) might be good enough for you. Or there may be some other curve whose curvature varies smoothly. (Curvature is more complicated than rate of change of gradient, but is a similar idea.)

But I take it the curve doesn't represent the actual physical motion of some object, but a cumulative probability function? And is it just you that has to be able to adjust the shape, or the user? Does the user see this shape, or only some effect of it? Does its behavior have to be intuitive to you, or to the user?
 
Here's another option based on a Bézier curve

Note that the Bézier curve is parametric, so there's no direct relationship from x to y. See item 5 on the Desmos link, it uses a "t" variable that goes from 0 to 1. The value of t determines values for x and y.

So the post#7 solution will be much easier to implement in code than this method.
 
@Dr.Peterson:
I'm not sure how much information will be shown to the user yet. I wasn't originally planning to, but now I think it'll be worth having a small graph showing the accuracy curve, with the current position marked on it.

It would be nice for stats to be meaningful though. For example, the player can choose how long to spend aiming their weapon before they fire, and the probability of them hitting the target will increase rapidly to begin with, and then the rate of increase will start to drop off, so knowing the location of that point would be useful to the player. But if the hit probability actually continues to increase at an almost-linear rate for some time beyond that point, then it's not so meaningful (and if the A.I. uses it to decide when to shoot, then that might lead to some poor decision-making as well).


@Cubist:
I've used bezier curves before, and never liked that the required "control point" is not actually on the line itself. I see that you're calculating the position of the control point automatically though, which is neat.
I see what you mean though - without an easy way to calculate "t" it's not going to be easy to use in practice (Desmos must be doing some complex maths behind the scenes).
 
Top