Showing posts with label solver. Show all posts
Showing posts with label solver. Show all posts

Sunday, January 25, 2009

solve second degree equations

In their simplest form, second degree equations look like this: a*x^2+b*x+c.

This code will solve a formula starting in that solution. Unfortunately, however, it will only solve equations which have whole numbers as solutions. If there are 2 solutions (possible with a second degree equation), it will display both of them. It solves the first solution by narrowing in ot it from above. As it gets closer to the solution, the amount subtracted from X decreases, so that, when it gets to the number, it stops completely. It starts on the second number from the first number, and slowly goes up until it hits it. If there is no solution, you will not get an answer for a very long time. When you do, it will say "no solution".


to seconddegree :a :b :c
make "x1 100000000000 make "done 1

while [:a*:x1*:x1+:b*:x1+:c>100000000] [make "x1 :x1-100000000 IF :x1>1000000000000 [show (list "no "solution)] while [:a*:x1*:x1+:b*:x1+:c>10000000]  [make "x1 :x1-10000000] while [:a*:x1*:x1+:b*:x1>1000000] [make "x1 :x1-1000000] while [:a*:x1*:x1+:b*:x1+:c>100000] [make "x1 :x1-100000] while [:a*:x1*:x1+:b*:x1+:c>10000] [make "x1 :x1-10000] while [:a*:x1*:x1+:b*:x1+:c>1000] [make "x1 :x1-1000] while [:a*:x1*:x1+:b*:x1>100] [make "x1 :x1-100] while [:a*:x1*:x1+:b*:x1+:c>10] [make "x1 :x1-10] while [:a*:x1*:x1+:b*:x1>1] [make "x1 :x1-1] while [:a*:x1*:x1+:b*:x1+:c>0] [make "x1 :x1-0.001]

make "x2 :x1-.01 until [:a*:x2*:x2+:b*:x2+:c>0] [make "x2 :x2-.01 IF :x2<-1000000000000 [show (list "the "answer "is :x1) STOP]]]

while [:x1>:x2] [show (list "the "answers "are :x1 "and :x2) Stop] show (list "no "solution)


end

Saturday, January 24, 2009

Simplified First-degree equation solver

In it's simplest form, a first degree equation looks like this: a*x+b=zero If you want to know what x equals, you would simply solve it. However, that would be impossible for more difficult equations, like 5th degree. That is what I'm working on now. Until then, however, this what I've gotten so far, only first degree. Notice, to get an answer with a possiblity as high as ten million, and accurate to the ten-thousandths place, it would take the computer practically forever, assuming it subtracted one ten-thousandth at a time. However, by making X "swing" back and forth around the number, it manages to zone in on it much faster. X goes down by a large amount until it passes the number, and then it goes up by a slightly smaller amount, and then back down again, until it finally hit the number. If, after getting to negative ten million, it couldn't find a number, it spits out "no solution". of course, unless X is too big, or too small, there will be a solution for every first degree equation. I only added that because I was thinking of the later equations, in which certain equations will have no solution.

to firstdegree :a :b
make "x 100000000000 
Until [:x=-10000000000] [while [:a*:x+:b>0] [make "x :x-100000000] while [:a*:x+:b<0]>0] [make "x :x-1000000] while [:a*:x+:b<0]>0] [make "x :x-10000] while [:a*:x+:b<0]>0] [make "x :x-100] while [:a*:x+:b<0]>0] [make "x :x-1] while [:a*:x+:b<0]>

end