generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How to find equation zeros?

Previous Thread :: Next Thread 
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
19 Jul 2015 08:19 PM
Is it possible to find the zeros of an equation with one unknown variable (places where the equation crosses the x-axis on a graph) using Roblox Lua?

I've found that you can do this with a loop, but that method is very inefficient, inaccurate, and only works about 50% of the time. Are there any other, more definite ways to do this?
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
19 Jul 2015 09:03 PM
for i = -100, 100 do
print(5*i/10==0)
end




--Like that?
Report Abuse
morash is not online. morash
Joined: 22 May 2010
Total Posts: 5834
19 Jul 2015 09:19 PM
Given the equation:

ax^n + bx^(n-1) ... + c

The possible rational zeroes would in a set of numbers consisting of the positive and negative factors of c divided by the positive and negative factors of a. You can get these with this code:

local factorsA = {}
local factorsC = {}
local possibleNums = {}

for i = 1,a do --providing that a is a variable
if math.floor(a/i) == a/i then
table.insert(factorsA, i)
table.insert(factorsA, -i)
end
end

for i = 1,c do --provided that c is a variable
if math.floor(c/i) == c/i then
table.insert(factorsC, i)
table.insert(factorsC,-i)
end
end

for i1 = 1, #factorsA do
for i2 = 1, #factorsC do
table.insert(possibleNums, factorsA[i1]/factorsB[i2]
end
end

At this point (if that doesn't error), you should have a set of rational numbers with the rational zeroes contained somewhere in the table "possibleNums". You could loop through this and apply the numbers into the original equation, then check if it returns zero.
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 09:37 AM
So, here's what I've got for my equation, it's a bit more complex than [ax^n + bx^(n-1)... +c]. I also only need to find the zeros between -90 and 90.

-16(d/(300sin(90-a)))^2 + (300sin(a))(d/(300sin(90-a))) + c
Where d and c are known constants that are replaced before finding zeros. I'll give you an example with these replaced in case you need that.

-16(2600/(300sin(90-a)))^2 + (300sin(a))(2600/(300sin(90-a))) + 10

I've checked this equation on my TI-84 graphing calculator and it should always have at least one zero between -90 and 90.

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 10:18 AM
Bump

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 11:17 AM
Bump [2]

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
20 Jul 2015 11:21 AM
use the quadratic formula and just hard code the addition/subtraction

-b +/- squrt(b^2-4ac)/
2a

put in the variables using string patterns - i suggest dividing it up via the addition symbols and then distinguishing the numbers and the letters
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 11:25 AM
I can't use the quadratic formula because the dependent variable (a) is part of both the "x" section and the "b" section.

"x" section = (d/(300sin(90-a)))
"b" section = (300sin(a))

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
20 Jul 2015 11:40 AM
um what math isn't my strong suite but I dont think what you're saying makes any sense
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
20 Jul 2015 11:46 AM
converted this from java to lua

local d = (b^2 - 4 * a * c) --d is for discriminant
local re = -b / (2 * a)

if discriminant >=0 then --if the roots are real
print(math.sqrt(d) / (2 * a) + re);
print(-math.sqrt(d) / (2 * a) + re);
else
print(re .. " + " .. (math.sqrt(-d) / (2 * a)) .. "i");
print(re .. " - " .. (math.sqrt(-d) / (2 * a)) .. "i");
end
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 11:48 AM
What the quadratic formula does is it isolates the x on one side, therefore finding the equation's zeros. But in my equation, I use the variable "a" instead of "x" and "a" cannot be isolated with the quadratic formula. If you used the quadratic formula on my equation, you'd get:

2600/(300sin(90-a)) = (-(300sin(a)) +- sqrt((300sin(a))^2 - (-64)(c))) / (-32)

See how "a" is on both sides?

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
20 Jul 2015 11:52 AM
oh then just subtract a lmao


2600/(300sin(90-a)) = (-(300sin(a)) +- sqrt((300sin(a))^2 - (-64)(c))) / (-32)
to

0 = (-(300sin(a)) +- sqrt((300sin(a))^2 - (-64)(c))) / (-32) - (2600/(300sin(90-a)))

BOOM algebra 1
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 11:55 AM
How do I solve for a now? In that new equation that you made, a is still not isolated. You can't just do some simple math to isolate it from that point.

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
20 Jul 2015 11:56 AM
oh I forgot that a is x.
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
20 Jul 2015 11:58 AM
Oh I see. you're using guess and check. My bad.
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 11:59 AM
A method other than guess and check would be nice.

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 01:27 PM
Bump [3]

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
20 Jul 2015 01:36 PM
pm a math wiz
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 01:40 PM
I would PM AxisAngle, but who knows if he'll respond.

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
20 Jul 2015 01:43 PM
Set it equal to zero and solve for a.

I honestly don't know how you could come up with that equation but couldn't think of doing that.
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 01:58 PM
If it were that easy I would have done it already.

Do you think you could simplify that equation down to a = ... for me?

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
mylifeis2cooldlol is not online. mylifeis2cooldlol
Joined: 08 Mar 2011
Total Posts: 874
20 Jul 2015 03:06 PM
I will give you a straight up answer but then I will follow it with a proof.

Given d and c:
Define
x = -16*d*d/(300*300)
y = d
z = c + x

One answer:
a = math.atan((-y + math.sqrt((y*y) - (4*x*z)))/(2*x))
Second answer:
a = math.atan((-y - math.sqrt((y*y) - (4*x*z)))/(2*x))


Now to show you how to do it (sorry, you wanted an answer I am going to give you an answer):

Take our given equation (converted into radian sorry):
-16(d/(300sin(math.pi/2 - a)))^2 + (d300sin(a))/(300sin(90-a)) + c = 0

Now use the identity: sin(A-B) = sinAcosB - sinBcosA and note that sin(math.pi/2) = 1 and cos(math.pi/2) = 0:

-16(d/(300cos(a)))^2 + (d300sin(a))/(300cos(a)) + c = 0

Now recall that sec(x) = 1/cos(x) and tan(x) = sin(x)/cos(x):

-16(sec(a)(d/300))^2 + dtan(a) + c = 0

Now recall the identity of sec^2(x) = 1 + tan^2(x):

-16((d/300)^2)(1+tan^2(a)) + dtan(a) + c = 0

Doing some distribution we obtain:

(-16*d*d/(300*300))tan^2(a) + dtan(a) + c - (-16*d*d/(300*300)) = 0

Now let x = tan(a) and we obtain:

(-16*d*d/(300*300))x^2 + d*x + c - (-16*d*d/(300*300)) = 0

Now for the sake of sight, lets define two variables, p and q:
p = (-16* d * d/(300*300))
q = c - p

Replacing these in our equation we obtain:

px^2 + dx + q = 0

Seeing that this is a quadratic equation, we can solve for x:

x = (-d +- math.sqrt(d^2 - 4pq))/2p

Recall that x = tan(a), replacing we obtain:

tan(a) = (-d +- math.sqrt(d^2 - 4pq))/2p

Finally recall that tan^(-1)(tan(x)) = x:

a = tan^(-1)(-d +- math.sqrt(d^2 - 4pq))/2p)


Q.E.D
Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
20 Jul 2015 03:50 PM
Wow! I'll try that out and see if it works next time I have time!

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
mylifeis2cooldlol is not online. mylifeis2cooldlol
Joined: 08 Mar 2011
Total Posts: 874
20 Jul 2015 05:00 PM
(Sorry for reopening, but I wish to state this message about my formula)

The formula only works if the discriminate is non-negitive.

This means that

d^2 + (64/300^2)(d^2)(c - (16d^2)/(300^2)) needs to be positive or zero.

Or better yet to check for this evaluate for the following inequality (meaning the discriminate is positive if this inequality holds):

300^4 + (64)*(300^2)*c - (4^5)*(d^2) >= 0

If this is satisfied (it is true), there will be at least 1 root (zero). However, if this is not satisfied then there will not be any real roots (and definitely outside of the range -90 to 90).

As a side note: You can actually still get a root (zero) when the inequality is not true, but you will need to use an complex number. However, this is outside of the scope (hopefully) of your need for the formula.

Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
20 Jul 2015 05:35 PM
well duh the quadratic formula only works if the discriminant is positive aswell.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image