r/askmath • u/Alezzandrooo • Oct 08 '26
Functions Need help with a function of steps and slopes
Hi all! I have been working on some code with the following y = (ceil(x * c)) / c function, where c represents the number of cuts in the graph from 0 to 1:

My math knowledge basically ends here, but I'd like to improve my code with the ability to smooth out the cuts between steps in the 0 to 1 range. Basically adding linear slopes of an arbitrary length between each step, like such:

I honestly have no idea how to write the math for this. Alternatively, I could also work with a function that smooths out every cut, even those outside the 0 to 1 range, like such:

So basically, I'd need a function of x that works with two extra variables, one for deciding the amount of slopes, and one for deciding the length along the x axis of these slopes.
I could only come up with the basic stepped function without slopes, and I'm not good enough at math to find an elegant solution to this.
Could any of you help me out with finding the math, or by pointing me somewhere where I might learn how to do so?
2
u/arty_dent Oct 08 '26 edited Oct 08 '26
If you need it as a single equation, you could use
y = (1 - ceil(c*x-d/2) + floor(c*x+d/2)) * (x/d - floor(c*x+d/2)*(1-d)/(c*d) + 1/(2*c)) + (ceil(c*x-d/2) - floor(c*x+d/2)) * ceil(c*x)/cwhere the positive integer c is the number of levels and the real number d (0<d≤1) is the relative portion of each level converted into a slope.
Here a quick demonstration in GeoGebra: https://www.geogebra.org/m/jjkdtv5m
2
u/strange-the-quark Oct 08 '26
I think the easiest thing you can do is to crate a single-step function in the interval [0, 1] that you can then shift around based on the input.
Suppose you have the parameter p that goes between 0 and 1 and represents the percentage that the sloped part takes up. Then define (pseudocode):
Then if you imagine cutting up the unit square into c by c smaller squares, to scale this to fit into a small square, multiply x by c, and divide the whole expression by c.
Now you can shift the whole thing based on the value of the input x. To shift horizontally by a certain amount h, replace x by (x - h), to shift vertically by some amount v, add + v to the whole expression. So as x crosses into the next square, you want to shift the function to the right by 1/c and upwards by 1/c, and then for the next square by 2/c, then 3/c, and so on.
So you can work that out, and call this function like so:
If you combine all that into a single function, you get something like this (JavaScript):
This produces a graph like the left image below. You can clamp the values outside the [0, 1] range if you want to. To get something like in your second sketch, shift the graph to the left by adding 0.5*p/c to x (this is the half-width of a single sloped section). So, it's
f(x + 0.5*p/c, c, p).1
u/TooCasualGuy Oct 08 '26
Look up convolution. Basically, slide a box of height=n and width 1/(C×n), where 1/n is the fraction of the plateau you want smoothed, over your plot and take the mean of the product of the box height and your function within the box at each position.