# Calculus Functions

Does anybody have any interest in some calculus functions I whipped up? Currently programming a calculator and figured id post some of the functions here if anyone had any interest.

@Monkeyman32123 Sure, I’m interested.

i have much interest although i have forgotten Calculus

I love maths, and would like using calculus in CODEA, nice you made those functions.

I must admit I’m curious as to what you mean by “calculus functions”. What specifically have you implemented?

Nothing complex, just things like finding definite untegrals and derivatives. I plan to post the code once i get it all nice and tidy and once i figure out whats wrong with my integral function. It does a trapezoidal sum and for some reason it doesnt work with certain numbers of segments, if you care to see, here it be

``````Integral={}

function Integral.calculate(func,x1,x2,mode,seg)
func = func or 'x'
x1 = x1 or 0
x2= x2 or 0
mode = mode or Settings.modes.CARTESIAN
local f = load("return (function(x) return "..func.." end)")()
local function cartesian()
local delta = (x2-x1)/(seg or 2000000)
local sum = 0
if delta~=0 then
for currx = x1,x2,delta do
sum=sum+f(currx)
end
sum=sum-f(x1)/2
sum=sum-f(x2)/2
end
sum = round(sum*delta,8)
return (sum or "No Solution Found")
end
local function polar()
local delta = (x2-x1)/(seg or 2000000)
local sum = 0
sum=sum+(f(x1)^2)
sum=sum+(f(x2)^2)
x1=x1+delta
if delta~=0 then
for currx = x1,x2,delta do
sum=sum+(f(currx)^2)
end
end
sum = round(sum*delta/2,8)
return (sum or "No Solution Found")
end
if mode == Settings.modes.CARTESIAN then
return cartesian()
elseif mode == Settings.modes.POLAR then
return polar()
elseif mode == Settings.modes.PARAMETRIC then

end
end

function draw()
print(9, Integral.calculate("x",0,4,nil,9))
print(6, Integral.calculate("x",0,4,nil,6))
end
``````

Note how it is correct with six subsections, but not with nine

Actually, just under 50% of numbers dont work properly

Only ever do for loops with integers:

``````    seg = seg or 2000000
local f = load("return (function(x) return "..func.." end)")()
local function cartesian()
local delta = (x2-x1)/seg
local sum = 0
if delta~=0 then
for currx = 0,seg do
sum=sum+f(x1+currx*delta)
end
sum=sum-f(x1)/2
sum=sum-f(x2)/2
end
sum = round(sum*delta,8)
return (sum or "No Solution Found")
end
``````

(Similar adjustment for the polar version.)

Any reason why you’re defining all your subfunction every time rather than just the one you need?

I’d love to see the symbolic integration and differentiation code. I’ve a few projects waiting for a decent maths parser.

I should just be defining the one i need, but currently the code is very sloppy, i havent yet refactored it. Thanks for the tip, by the way, i guessed it had something to do with not using integers, but by the time i went to check youd already responded