Friday, February 6, 2009

solve 2nd degree equations

This is will output second degree equations using the quadratic formula. It is perfect except for two things: first, it outputs without parentheses around the parts before the fraction, so you must remember that you are dividing everything on the top by the bottom, not just the last part. Second, it does not reduce the square root. For example, the square root of 40 can be reduced to 2 times the square root of ten. However, I couldn't think of an effective way to do that. Still, it gives the right answer, and that's what counts in the end:

to solve.second :a :b :c
while [(:b*:b-4*:a*:c)>0] [if [sqrt(:b*:b-4*:a*:c)= int (:b*:b-4*:a*:c)] [make "q sqrt(:b*:b-4*:a*:c) show (word "x "= "(-:b+:q)/(2*:a) " and " (-:b-:q)/(2*:a)) stop]]
show (word " ) show (list "the "answers "are) show (word  (-:b) "+ "sqrt "(:b*:b-4*:a*:c) "/ "(2*:a)) show (list "and) show (word ""(-:b) "- "sqrt "(:b*:b-4*:a*:c) "/ "(2*:a))
end

Solve 1st degree equations

Although this does the same thing as the solver before, it does it the direct way. Put your equation in the form ax+b=0 and insert a and b into the variables, and it spits out X:

to solve.first :a :b
show (word "x "= "(-:b/:a))  setx (-:b/:a)
end

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

Monday, January 12, 2009

Graphing a second-degree equation

This is for drawing two-variable, second-degree equations on a graph in their simplest form: y=a*(x^2)+bx+c

Where :a is what is multiplied by x squared, :b is multiplied by x, and :c is added to the other two. :X is the x-cordinate. Wait a few seconds after you start the program, as it has to run for a while before the first condition is satisfied. This is to keep the turtle from drawing all over the screen, as it flies up and then appears at the bottom again and again.

to graph2 :a :b :c
make "x -450
while [:a*:x*:x+:b*:x+:c>450] [make "x :x+.01]  penup make "x :x+01 setx :x sety :a*xcor*xcor+:b*xcor+:c pendown while [ycor<450]>450] [stop] make "x :x+01 setx :x sety :a*xcor*xcor+:b*xcor+:c]
end

graphing a first degree equation

This is for drawing graphs of the most simplified form of any two-variable equation, y=ax+b.

Where :a is what you multiply by X, :b is what you add to that, and :x is where the turtle is on the x-axis:

to graph :a :b
make "x -450
while [:a*:x+:b>450] [make "x :x+.01]  penup make "x :x+01 setx :x sety :a*xcor+:b pendown
while [xcor<450]>450] [stop] make "x :x+01 setxy :x :a*:x+:b] 
end

Thursday, January 1, 2009

Pool table (1 ball)

This is code for a ball bouncing without gravity, but with walls all around it. Currently, I have not figured out an efficient way to get the ball to bounce off any wall. It has to be just straight lines that are preprogrammed; no cool zig zag bounces. Also, this equation only accounts for one ball. I plan on adding more balls later, but only have one right now.



to pooltable :V :A :CS

Make "h 0 Make "l 0 MAKE "y 0 Make "p 0 seth 0 rt :a
while [:V>.001] [MAKE "l xcor MAKE "h ycor fd :V IFelse ycor>:h [Make "y ycor-:h] [Make "y :h-ycor] Ifelse xcor>:l [Make "z xcor-:l] [Make "z :l-xcor]
while [ycor>300] [IF Heading>180 [lt 180-(arctan (:y) (:z))*2] IF Heading<180 [rt 180-(arctan (:y) (:z))*2] fd :V*:CS Make "V :V*:CS]
while [ycor<-300] [IF Heading>180 [rt 180-(arctan :y :z)*2] IF Heading<180 [lt 180-(arctan :y :z)*2] FD :V*:CS Make "V :V*:CS]
while [xcor>450] [If Heading>90 [rt 180-(arctan :z :y)*2] If heading<90 [lt 180-(arctan :z :y)*2] fd :V*:CS Make "V :V*:CS]
while [xcor<-450] [ IF heading<270>270 [rt 180-((arctan :z :y)*2) fd 1] fd :V*:CS Make "V :V*:CS ]]

end

Where :v is the starting velocity, :a is the starting angle, and :CS is coefficient of elasticity at which it will bounce off walls. I recomend putting the coefficient of elasticity at about .9 or .6 at the lowest. Keep velocity very low. Preferably at only 1. Pick your own angle.