How to create Integration and Derivative functions [TUTORIAL]

So I just discovered how to make Integration and Derivatives in roblox so i thought ill share it with yall, if anyone needs it :happy3:


Derivative

  • Create a function… of course
info

._.

function Derivative()
end
  • Add a number and a function argument
info

f’(x)

here x is our number argument and f is the function arg.

you can name the arguments anything but you have to assign the names everywhere

function Derivative(x: number, f: ())
end
  • Create a really small constant
info

The smaller the constant, the bigger the accuracy and :slight_smile:

function Derivative(x: number, f: ())
    local C = 1e-10
end
  • Calculate the slope of f from x to x+C
info

This is a bit confusing so please just CTRL+C, CTRL+V it if you dont understand ><

function Derivative(x: number, f: ())
    local C = 1e-10
    local slope = ( f(x+C) - f(x) )/C
end
  • And lastly, return the slope :slight_smile:
  • Results:
function Derivative(x: number, f: ())
    local C = 1e-10
    local slope = ( f(x+C) - f(x) )/C
    return slope
end

Integration

  • Create a function with 2 number and 1 function argument.
info

;-;

function Integral(x: number, y: number, f: ())
end
  • Create a small constant and a zero constant.
info ❗

If the constant is smaller than 1e-6 then it might blow up your pc crash your game.

function Integral(x: number, y: number, f: ())
    local C = 1e-5
    local a = 0
end
  • Make a for loop starting at x, ending at y and grows by C
info

shorty, for x,y,C do

function Integral(x: number, y: number, f: ())
    local C = 1e-5
    local a = 0
    for i = x,y,C do
    end
end
  • Insert f(i)*C into the loop (adding it to a)
info

f(i)*C is a thin line under the curve. All of those make up the integral.

function Integral(x: number, y: number, f: ())
    local C = 1e-5
    local a = 0
    for i = x,y,C do
    a += f(i)*C
    end
end
  • And lastly, return a :))
  • result:
function Integral(x: number, y: number, f: ())
    local C = 1e-5
    local a = 0
    for i = x,y,C do
    a += f(i)*C
    end
    return a
end

NOTE: i didn’t test this in every way i wanted to. It might have bugs or might not even work. I just learned Integration and Derivatives. Also… Integration is 5 decimals accurate

7 Likes

if this is a tutorial, then this belongs in #resources:community-tutorials

also I don’t need Integration and Derivative functions, also can you add them to your math module?

1 Like

Never really thought about using integration in roblox scripting, I put together a trapezoidal integration and a param (n) to approximate more accurately by diving the intervals into n subintervals

function trapezoidalIntegral(x, y, f, n)
    local h = (y - x) / n
    local sum = 0.5 * (f(x) + f(y))

    for i = 1, n - 1 do
        sum = sum + f(x + i * h)
    end
    return sum * h
end
2 Likes

Oh i didnt see theres a community tutorials category, ill change it!

Also i did add them into my module, but i didnt update it yet, im adding more things too :))

1 Like

Bookmarked because I might find this useful for calculating derivatives for almost all my models in the DataPredict library in the future.

Good resource.

1 Like