# Calculus Concepts Scripted

Hello, I managed to quickly put together an integral calculator. It’s useless, and it only calculates the value of the the given definite integral you put in, but that’s okay! The value is given in the output console, so I didn’t manage to make like a super cool UI that works with it.

I am very new to scripting, so I’m going to give all the credit to @Scarious for his summation formula script. This script is absolutely essential because without sums you cannot integrate.

Here’s his project: [MATH] Getting the ln(x) given x

I have not looked for any limitations of this script, because I already know its of subpar quality. Feel free to use it and improve it. I just made this to learn a few things and touch up on some math as well.

IMPROVED VERSIONS BELOW IN COMMENT SECTION

Changed title of thread to Calculus Concepts Scripted from Integral Calculator

``````function sum (s,n,body,...)
local self = {}

self.s = s
self.n = n
self.body = body
self.args = {...}
self.p = 0

function self:update()
local p = 0
for i = self.s,self.n,1 do
p = p + self.body(i,unpack(self.args))
end
self.p = p
end
self:update()

function self:setargs(...)
self.args = {...}
end

function self:setbody(newbody)
self.body = newbody
end

return self
end

function riemannsum (x)
local r= 1000000 -- rectangles to be used. the more that are used the more accurate the value will be.
local summation = sum(1,r,function(n) local x0 = 1/2 local x1 = 5/2 local dx = (x1 - x0)/r local x = x0 + n*dx local fx = 1/(1+x^2) return (fx) end) -- f(x) = fx. Lower Bound = x0, Upper bound = x1
summation:update()
return summation.p

end

x0 = 1/2 -- lower bound
x1 = 5/2 -- upper bound
r = 1000000 -- rectangles to be used. the more that are used the more accurate the value will be.
dx = (x1 - x0)/r -- dx
integral = (riemannsum())*dx --- ∫f(x)dx
print(integral)

-- outputs 0.7266416786129303

``````

This is a coded version of this:

Where…

a = x0
b = x1
n = r
i = n
f(x_i) = f(x) = fx
x_i = x = x

according to the code.

4 Likes

I’m currently take Calculus, and the fact that anyone had the patience to make this astounds me.

Well done.

Update:

This works for trig, hyperbolic functions, exponential, and non-trivial integrals like sin(x^2) and x^x, if you’re wondering. Also for some reason it prints out a random 0.166667… value and I’m not sure why. Just ignore it if you’re using it to see what value you get.

Ooooo old code. For a summation it’s even easier if you use a for loop!

``````local function integrate(f: (number) -> number, lowerBound: number, upperBound: number, rectangles: number): number
local dx = (upperBound - lowerBound) / rectangles
local sum = 0

for i = 1, rectangles do
local x = lowerBound + (i - 0.5) * dx
sum = sum + f(x)
end

return sum * dx
end

local lower = 1/2
local upper = 5/2
local rectangles = 1000000

print(integrate(math.sqrt, lower, upper, rectangles)) -- 2.3995291230781213
print(integrate(math.sin, lower, upper, rectangles)) -- 1.6787261774376632
print(integrate(math.cos, lower, upper, rectangles)) -- 0.11904660549975103
``````

Amazing, thanks for making an improved version. This code I can understand much better as a beginner. I found it interesting that 0.5 made the value more accurate.

Although, I’m not really sure what this does because it works perfectly fine without it.

Here is a double integral over RECTAGULAR regions (constants only). I’m pretty sure you can expand this into a triple integral as well.

``````function integration(a,b,c,d,rectangles)
local dx = (b - a)/rectangles
local dy = (d - c)/rectangles
local sum = 0

for i = 1, rectangles, 1 do
local x = a + (i-0.5)*dx
local y = c + (i-0.5)*dy
local fxy = y/(1+y^2) -- function goes here
sum = sum + fxy
end

return sum*dx*dy -- You can switch this by Fubini's Theorem.
end

--[[

∫∫fxydA

where dA = dy*dx or dA = dx*dy (by Fubini's Theorem). Default: dx*dy

∫∫fxdA is integrated over Region = {(x,y) | a≤x≤b, c≤y≤d} (Rectangular Region (constants only))

]]

local abound =1 -- dx bounds
local bbound =3

local cbound =0 -- dy bounds.
local dbound =1

local rectangles = 10000000

DoubleIntegral = (integration(abound, bbound, cbound, dbound, rectangles) )
print(DoubleIntegral * 10000000)
``````

Its a simple type definition; (number) → number is just defining that variable “f” is a function that takes in a number, and returns a number.