Create a formula that creates a curve between two points

23.8k Views Asked by At

We have two points, $A$ and $B$. The difference in $x$ is 1 unit, and the difference in $y$ is arbitrary. For each point we also know the gradient. First, I want to draw a smooth line that connects both points and smoothly transitions between gradients. Then, I want a function that returns a $y$-value for any value of $x$ between $A$ and $B$, where $0 < x < 1$.

2

There are 2 best solutions below

2
On BEST ANSWER

Assume that our conditions are: $$ f(0) = 0,~~ f(1) = b,~~ f'(0) = p,~~ f'(1) = q $$ and that we use a cubic polynomial $f(x) = a_3x^3 + a_2x^2 + a_1x + a_0$. Solving this system of equations yields: $$ f(x) = (p + q - 2b)x^3 + (3b - 2p - q)x^2 + px $$


For example, if our first point is $(0, 0)$ with a gradient of $p = 3$ and our second point is $(1, 4)$ with a gradient of $q = -2$, then our curve is: $$ f(x) = -7x^3 + 8x^2 + 3x $$ Indeed, the plot looks like:

enter image description here

0
On

You have 4 conditions: $f(x_a)=y_a$, $f(x_b)=y_b$, $f'(x_a)=m_a$ and $f'(x_b)=m_b$. As such you need a cubic equation - $f(x)=ax^3+bx^2+cx+d$.

This is known as a cubic spline. There are other types of splines which may be more suitable for your application but will involve harder mathematics.

You should be able to find lots with a simple google search.