Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Algebra Equation Solver HELP NEEDED!
- RoboNeo9
-
Scratcher
96 posts
Algebra Equation Solver HELP NEEDED!
I'm having trouble with something that works like a calculator to solve algebra equations. I am pretty far through already and it has many features, but I want it to solve any linear equation given to it (that uses one variable once) for example: 3x/3=2 15x/5-2=3 (12x+7)/3=19 you can find the beta here.
Can someone please help me with this? I already have an idea of what to do (put in the project), but it doesn't work for some reason can anyone please check it out?
Can someone please help me with this? I already have an idea of what to do (put in the project), but it doesn't work for some reason can anyone please check it out?
- andre_rifaut
-
Scratcher
100+ posts
Algebra Equation Solver HELP NEEDED!
I had a look, at your program.
When trying to understand problems, you should test some easy input, and see at some important stages of you code what are the values of your (internal) variables.
I tried the following:
1) for en empty string (i.e. I put nothing in the answer), your program never end
2) for x-0=0 the answer is NaN (instead of 0)
3) for 0-x=0 the answer is NaN (instead of 0)
4) for x=0 your program never end
5) same for 0=x or x=1 or 1=x or x=2 or 2=x … your program never end
6) for 2-x=2 the answer is “infinity” (instead of 0)
7) for x-2=2 the answer is NaN (instead of 0)
8) for 2x-2=0 the answer is -1 (instead of 1)
9) for 2-2x=0 the answer is NaN
…
So there are very simple equations in this list, you should see if you split correctly the string given by the user. Then you should see if you detect correctly all parameters of the equations, then you shouls see if you make the right computations. Three important steps. You should test each one separately.
At a more fundamental level, you could change your design as follows:
1) allow the user to define functions, by writing for instance f(x) = … (any formula)
2) allow the user to as f(1) or f(3) or … and you compute the value for him.
3) actually, if you can do 1) and 2) , you could plot the function
coming back to your algebra solver:
1) Recall that a linear equation is solved when two points of the line are given. So, when given a function, you can yourself compute f(x1) and f(x2) for two random x1 and x2, then compute “the line” (i.e. solve the equation). Then you compute 10 other random points and see if their values computed with f(x) match with the value computed by your solution. If yes there is a perfect match, you can easily solve the equation from the two points. If no perfect match, the equation is not linear, so, do the next step.
2) You know that any quadratic is fixed with three point. Then compute three points, for instance f(0), f(1), f(-1).
So, you can easily find a,b,c, such that a*x*x + b*x + c = f(x)
For instance f(0) = a*0*0 + b*0 + c ==> c=f(0)
f(1) = a*1*1 + b*1 + f(0) ==> a+b=f(1)-f(0) ==> b=f(1)-f(0)-a
f(-1) = a*(-1)*(-1) + b*(-1) + c ==> f(-1) = a + (f(1)-f(0)-a)*(-1) + f(0) ==> a = … hence you can compute a, and then b from a.
Now having computed a,b and c, you verify with 10 random points that a*x*x + b*x + c is the same as when computing the original f(x). If yes, the equation is quadratic and with the a,b and c, you can use the traditional formula to find the two “roots” of f(x).
There is also a formula for a*x*x*x + b*x*x + c*x + d. (Cardano formula). So you could extend the preceding technique with four points to find a, b, c and d, and then to use that formula.
For solving other polynomial equations, you should use other methods.
I do not understand what you mean by “factoring”. Could you give an example of the string given by the user and the solution your program has to provide.
When trying to understand problems, you should test some easy input, and see at some important stages of you code what are the values of your (internal) variables.
I tried the following:
1) for en empty string (i.e. I put nothing in the answer), your program never end
2) for x-0=0 the answer is NaN (instead of 0)
3) for 0-x=0 the answer is NaN (instead of 0)
4) for x=0 your program never end
5) same for 0=x or x=1 or 1=x or x=2 or 2=x … your program never end
6) for 2-x=2 the answer is “infinity” (instead of 0)
7) for x-2=2 the answer is NaN (instead of 0)
8) for 2x-2=0 the answer is -1 (instead of 1)
9) for 2-2x=0 the answer is NaN
…
So there are very simple equations in this list, you should see if you split correctly the string given by the user. Then you should see if you detect correctly all parameters of the equations, then you shouls see if you make the right computations. Three important steps. You should test each one separately.
At a more fundamental level, you could change your design as follows:
1) allow the user to define functions, by writing for instance f(x) = … (any formula)
2) allow the user to as f(1) or f(3) or … and you compute the value for him.
3) actually, if you can do 1) and 2) , you could plot the function
coming back to your algebra solver:
1) Recall that a linear equation is solved when two points of the line are given. So, when given a function, you can yourself compute f(x1) and f(x2) for two random x1 and x2, then compute “the line” (i.e. solve the equation). Then you compute 10 other random points and see if their values computed with f(x) match with the value computed by your solution. If yes there is a perfect match, you can easily solve the equation from the two points. If no perfect match, the equation is not linear, so, do the next step.
2) You know that any quadratic is fixed with three point. Then compute three points, for instance f(0), f(1), f(-1).
So, you can easily find a,b,c, such that a*x*x + b*x + c = f(x)
For instance f(0) = a*0*0 + b*0 + c ==> c=f(0)
f(1) = a*1*1 + b*1 + f(0) ==> a+b=f(1)-f(0) ==> b=f(1)-f(0)-a
f(-1) = a*(-1)*(-1) + b*(-1) + c ==> f(-1) = a + (f(1)-f(0)-a)*(-1) + f(0) ==> a = … hence you can compute a, and then b from a.
Now having computed a,b and c, you verify with 10 random points that a*x*x + b*x + c is the same as when computing the original f(x). If yes, the equation is quadratic and with the a,b and c, you can use the traditional formula to find the two “roots” of f(x).
There is also a formula for a*x*x*x + b*x*x + c*x + d. (Cardano formula). So you could extend the preceding technique with four points to find a, b, c and d, and then to use that formula.
For solving other polynomial equations, you should use other methods.
I do not understand what you mean by “factoring”. Could you give an example of the string given by the user and the solution your program has to provide.
- Hardmath123
-
Scratcher
1000+ posts
Algebra Equation Solver HELP NEEDED!
There is another neat trick: if you have an equation of the form f(n) = n (which is easy if f(n) is a polymonial) then you plot y = f(x) and y = x. To find where they intersect (and thus where f(x) = y = x) you find f^k(n) for some arbitrary n and large k, and hopefully it will converge to one of the solutions to your polynomial. See this link.
A couple years ago I wrote a sample implementation: try this HTML out.
(Yes, the JS will make you cry, but it works.)
A couple years ago I wrote a sample implementation: try this HTML out.
<h1>Computing fixed points of a function by iteration:</h1> <br>f(n) = <input type="text" id="fn" value="6+60/n"> <br>Iterations: <input type="text" id="it" value="2"> <br>Starting value: <input type="text" id="val" value="5"> <br><input type="button" value="Iterate!" onclick="iterate()"> <br><canvas id="canvas" height="300" width="300"></canvas> <script type="text/javascript"> //Set context var ctx = document.getElementById("canvas").getContext("2d"); //Set up graphing plane quardants ctx.clearRect(0,0,300,300); ctx.strokeRect(0,0,300,300); ctx.fillStyle="rgba(0,0,30,0.5)"; ctx.fillRect(0,0,300,150); ctx.fillRect(0,0,150,300); function iterate() { //Draw plane ctx.clearRect(0,0,300,300); ctx.strokeRect(0,0,300,300); ctx.fillStyle="rgba(0,0,30,0.5)"; ctx.fillRect(0,0,300,150); ctx.fillRect(0,0,150,300); var i; var n; var ox; var x; var v = 0; //Draw x=y ctx.fillStyle="rgba(0,0,30,1)"; for(i=-150; i<=150; i++) { ctx.fillRect(i+150,300-(i+150),1,1); } //Plot function ctx.beginPath(); ctx.strokeStyle="rgba(0,0,0,1)" for(n=-150; n<=150; n++) { eval("i = "+document.getElementById("fn").value); ctx.lineTo((n+150),(300-(i+150))); } ctx.stroke(); //Draw the iterations ctx.beginPath() ox = document.getElementById("val").value*1; ctx.moveTo(ox+150,150) ctx.strokeStyle="rgba(256,0,0,1)" for(i=0;i<=document.getElementById("it").value; i++) { eval("v = "+document.getElementById("fn").value.replace(/n/g, "ox")) ctx.lineTo((ox+150),300-(v+150)) ctx.lineTo((v+150),300-(v+150)) ox = v } ctx.stroke() alert("One of the fixed points of your function is "+ox) } </script>
Last edited by Hardmath123 (May 10, 2013 08:08:47)
- RoboNeo9
-
Scratcher
96 posts
Algebra Equation Solver HELP NEEDED!
There is another neat trick: if you have an equation of the form f(n) = n (which is easy if f(n) is a polymonial) then you plot y = f(x) and y = x. To find where they intersect (and thus where f(x) = y = x) you find f^k(n) for some arbitrary n and large k, and hopefully it will converge to one of the solutions to your polynomial. See this link.Thank You!
A couple years ago I wrote a sample implementation: try this HTML out.(Yes, the JS will make you cry, but it works.)<h1>Computing fixed points of a function by iteration:</h1> <br>f(n) = <input type="text" id="fn" value="6+60/n"> <br>Iterations: <input type="text" id="it" value="2"> <br>Starting value: <input type="text" id="val" value="5"> <br><input type="button" value="Iterate!" onclick="iterate()"> <br><canvas id="canvas" height="300" width="300"></canvas> <script type="text/javascript"> //Set context var ctx = document.getElementById("canvas").getContext("2d"); //Set up graphing plane quardants ctx.clearRect(0,0,300,300); ctx.strokeRect(0,0,300,300); ctx.fillStyle="rgba(0,0,30,0.5)"; ctx.fillRect(0,0,300,150); ctx.fillRect(0,0,150,300); function iterate() { //Draw plane ctx.clearRect(0,0,300,300); ctx.strokeRect(0,0,300,300); ctx.fillStyle="rgba(0,0,30,0.5)"; ctx.fillRect(0,0,300,150); ctx.fillRect(0,0,150,300); var i; var n; var ox; var x; var v = 0; //Draw x=y ctx.fillStyle="rgba(0,0,30,1)"; for(i=-150; i<=150; i++) { ctx.fillRect(i+150,300-(i+150),1,1); } //Plot function ctx.beginPath(); ctx.strokeStyle="rgba(0,0,0,1)" for(n=-150; n<=150; n++) { eval("i = "+document.getElementById("fn").value); ctx.lineTo((n+150),(300-(i+150))); } ctx.stroke(); //Draw the iterations ctx.beginPath() ox = document.getElementById("val").value*1; ctx.moveTo(ox+150,150) ctx.strokeStyle="rgba(256,0,0,1)" for(i=0;i<=document.getElementById("it").value; i++) { eval("v = "+document.getElementById("fn").value.replace(/n/g, "ox")) ctx.lineTo((ox+150),300-(v+150)) ctx.lineTo((v+150),300-(v+150)) ox = v } ctx.stroke() alert("One of the fixed points of your function is "+ox) } </script>
- RoboNeo9
-
Scratcher
96 posts
Algebra Equation Solver HELP NEEDED!
I had a look, at your program.I look at that! I like your function idea. The reason the simple problems above don't work is because of the way it is programmed. It is made for more specific equations than just any linear (read notes).
When trying to understand problems, you should test some easy input, and see at some important stages of you code what are the values of your (internal) variables.
I tried the following:
1) for en empty string (i.e. I put nothing in the answer), your program never end
2) for x-0=0 the answer is NaN (instead of 0)
3) for 0-x=0 the answer is NaN (instead of 0)
4) for x=0 your program never end
5) same for 0=x or x=1 or 1=x or x=2 or 2=x … your program never end
6) for 2-x=2 the answer is “infinity” (instead of 0)
7) for x-2=2 the answer is NaN (instead of 0)
8) for 2x-2=0 the answer is -1 (instead of 1)
9) for 2-2x=0 the answer is NaN
…
So there are very simple equations in this list, you should see if you split correctly the string given by the user. Then you should see if you detect correctly all parameters of the equations, then you shouls see if you make the right computations. Three important steps. You should test each one separately.
At a more fundamental level, you could change your design as follows:
1) allow the user to define functions, by writing for instance f(x) = … (any formula)
2) allow the user to as f(1) or f(3) or … and you compute the value for him.
3) actually, if you can do 1) and 2) , you could plot the function
coming back to your algebra solver:
1) Recall that a linear equation is solved when two points of the line are given. So, when given a function, you can yourself compute f(x1) and f(x2) for two random x1 and x2, then compute “the line” (i.e. solve the equation). Then you compute 10 other random points and see if their values computed with f(x) match with the value computed by your solution. If yes there is a perfect match, you can easily solve the equation from the two points. If no perfect match, the equation is not linear, so, do the next step.
2) You know that any quadratic is fixed with three point. Then compute three points, for instance f(0), f(1), f(-1).
So, you can easily find a,b,c, such that a*x*x + b*x + c = f(x)
For instance f(0) = a*0*0 + b*0 + c ==> c=f(0)
f(1) = a*1*1 + b*1 + f(0) ==> a+b=f(1)-f(0) ==> b=f(1)-f(0)-a
f(-1) = a*(-1)*(-1) + b*(-1) + c ==> f(-1) = a + (f(1)-f(0)-a)*(-1) + f(0) ==> a = … hence you can compute a, and then b from a.
Now having computed a,b and c, you verify with 10 random points that a*x*x + b*x + c is the same as when computing the original f(x). If yes, the equation is quadratic and with the a,b and c, you can use the traditional formula to find the two “roots” of f(x).
There is also a formula for a*x*x*x + b*x*x + c*x + d. (Cardano formula). So you could extend the preceding technique with four points to find a, b, c and d, and then to use that formula.
For solving other polynomial equations, you should use other methods.
I do not understand what you mean by “factoring”. Could you give an example of the string given by the user and the solution your program has to provide.
- andre_rifaut
-
Scratcher
100+ posts
Algebra Equation Solver HELP NEEDED!
Indeed, your symbolic algebra calculator is very appropriate for general problems.
I had a second thought with your idea of algebra application and “the three stages” you have in your code.
I remembered that it was very easy to make an interpreter for any language written in the Polish reverse notation (see web wikipedia).
Indeed, the polish reverse notation allows you to escape from th problems of “parsing” the input sentence. In some way, this would be a kind of intermediate representation of “compiled algebraic expressions” that can be very easily interpreted.
A polynomial function
f(x1,x2) =def= sqrt(x1*x1 + x2*x2) / 2
could be written (spaces are importants):
x1 x2 f 2 apply x1 x1 * x2 x2 * + sqrt 2 / =
In this way, you separate the “paring” problem from the “interpretation” problem
From that you can make a general function evaluator.
Or you can make a general function that “normalize” the polynomials expressions for symbolic computations on polynomials.
You then can extend your polish notation to a computer programming language for specific tasks.
I do not know if a scratcher has already made a Polish reverse notation interpreter.
I had a second thought with your idea of algebra application and “the three stages” you have in your code.
I remembered that it was very easy to make an interpreter for any language written in the Polish reverse notation (see web wikipedia).
Indeed, the polish reverse notation allows you to escape from th problems of “parsing” the input sentence. In some way, this would be a kind of intermediate representation of “compiled algebraic expressions” that can be very easily interpreted.
A polynomial function
f(x1,x2) =def= sqrt(x1*x1 + x2*x2) / 2
could be written (spaces are importants):
x1 x2 f 2 apply x1 x1 * x2 x2 * + sqrt 2 / =
In this way, you separate the “paring” problem from the “interpretation” problem
From that you can make a general function evaluator.
Or you can make a general function that “normalize” the polynomials expressions for symbolic computations on polynomials.
You then can extend your polish notation to a computer programming language for specific tasks.
I do not know if a scratcher has already made a Polish reverse notation interpreter.
- andre_rifaut
-
Scratcher
100+ posts
Algebra Equation Solver HELP NEEDED!
See http://scratch.mit.edu/projects/580724/ and remixes.
However the originality of your project is to propose symbolic computations on functions and polynomials, …
However the originality of your project is to propose symbolic computations on functions and polynomials, …
- YMIBANWAH
-
Scratcher
100+ posts
Algebra Equation Solver HELP NEEDED!
If you're after an algebra you should add quadratics, they're extremely easy since all you have to do is separate the equation and define a, b, and c. From there all you need is to use the formula.
- RoboNeo9
-
Scratcher
96 posts
Algebra Equation Solver HELP NEEDED!
If you're after an algebra you should add quadratics, they're extremely easy since all you have to do is separate the equation and define a, b, and c. From there all you need is to use the formula.It is already added. You just type the equation to solve it.
- Discussion Forums
- » Help with Scripts
-
» Algebra Equation Solver HELP NEEDED!