This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LDDestroier's profile picture

Solve for X function

Started by LDDestroier, 10 May 2017 - 04:42 PM
LDDestroier #1
Posted 10 May 2017 - 06:42 PM
This function solves any algebraic equation where you need to solve for a single variable (x)

pastebin get bs4QNDyS solve

Syntax:

solve("2*x-4","4*x+3","x")
--First argument is the left part of the equation
--Second argument is the right part.
--Third is the name of the variable to do math with.
--This function supports adding functions like math.sqrt or math.sin

strsolve("2x(3-x) = 8x - math.sqrt(8)")
--First argument is a full equation
--Second argument can be a variable name, but defaults to "x"
--This too can have functions like math.sqrt

Give me some credit, or PM me, if you want to use this for your programs. Or don't. I won't find out…or eat you
This function might not be 100% accurate if you get irrational answers (off by a billionth or something), or is unsolvable. It does its best to detect that, though.
apemanzilla #2
Posted 11 May 2017 - 02:32 PM
Hmm….
Edited on 11 May 2017 - 12:38 PM
Emma #3
Posted 12 May 2017 - 04:35 AM
I broke it :)/>
Spoiler

Edit; very nice work tho, for not crafted functions specially designed to thwart your algorithm it works pretty well :)/>
Basically it doesn't like inflection points very much, at all
Edited on 12 May 2017 - 02:40 AM
Emma #4
Posted 13 May 2017 - 04:22 AM
Hmm….

@EldidiStroyrr also this happens bc the loadstring turns into -2^2=-4 which lua sees as -(2^2)=-4
Edited on 13 May 2017 - 02:22 AM
LDDestroier #5
Posted 27 May 2017 - 01:14 AM
Shite. I wonder how I could fix it. How does my CASIO calculator do it…?