How could I store a mathematical function?

Hi everyone,
I’m currently working on a train chassis and I’ve came across yet another problem:
In real life, the traction motors decrease the force they apply the faster the train gets, and I wanted to replicate that.
Alright, so I looked at some documents for a certain train and have created a mathematical function for that.
Since that function will vary from train to train, I want to put it into the separate settings script (a module script) which I already have so I don’t have to edit the main system every time I’m building a new train.
Now that’s where the problem comes in.
If you just put the function into the script like this:

Traction_Curve		= 575*0.9904^Velocity
EB_Curve			= -575*0.9897^Velocity

it tries to calculate that, of course. However in this case I do not want that, since I want that function passed on later. How would I do that?

I thought about putting the function into a string and then using tonumber(), however that would cut off some parts of the functions as far as I know. So I’m at a dead end now, I don’t have any ideas.

Regards, FloribertHD

You can store functions using functions?

--in a module script
Local functionModule ={} --put your functions in this table

 function functionModule.calculateTractionCurve(CurrentVelocity)
return 575*0.9904^CurrentVelocity
end

return functionModule

Then in a controller script:

local functionModule = require(script.functionModule)

--somewhere
local traction =  functionModule.calculateTractionCurve(velocitystuff)
2 Likes

I wouldn’ve thought of that, thanks!
I’ve added that to the scripts now, and it works. It isn’t the most fanciest solution, but it works, and that’s what counts. The players won’t see the scripts anyways. :stuck_out_tongue:
Thank you so much for your help!

Having a module for one function only (and it’s a really short one), is pretty useless in my opinion.
You can store it as a local variable instead.

2 Likes

I’ve learnt this from what A chassis has done to calculate their torque curves and it can get pretty complex with math functions inside of math functions inside of math functions.

If it’s static and the formula doesn’t change like in this scenario then it should work fine. Otherwise, you can use something like this string calculator to convert string math expressions into numbers. Looking into it, it seems to even accept functions as well.

@Xacima thanks for adding on and mentioning this, was rushing to post. It’s just an example I’ll edit it in table form.

2 Likes

If your function needs to change, or you need to generate and store functions with different values for different trains, you could set up a function that defines a function for you using specified coefficients:

local function exponential(a, b)
	return function(x)
		return a * b ^ x
	end
end

local function polynomial(...) -- example
	local c = {...}
	return function(x)
		local y = 0
		for deg, coef in ipairs(c) do
			y = y + coef * x ^ (#c - deg)
		end
		return y
	end
end

local exp = exponential(1, 2.71828) -- same as e^x

local poly = polynomial(1, 2, 3) --  x^2 + 2x + 3 (as example)

local Traction_Curve = exponential(575, 0.9904)
local EB_Curve = exponential(-575, 0.9897)

print(Traction_Curve(20))
2 Likes

Again - thanks for the help! In the end I’ll have to calculate the functions externally anyways, so I will stick with the first solution, it being the simplest. Having another module just so I can transfer two mathematical functions doesn’t seem worth it to me. The last solution is also interesting, I might implement it in some sort later.