11

I need help to solve this formula ((n * 2) + 10) / (n + 1) = 3, preferably in PHP. (The numbers 2, 10 and 3 should be variables that can be changed.)

I'm able to solve this equation on paper quite easily. However, when I try to implement this in PHP, I'm not sure where to start. I've done several Google queries and searches on here and nothing seems to help. I'm missing the proper approach to deal with this problem.

Any tips and pointers would be great, and if you provide the exact code, please explain how you got to this result.

10
  • Do you need to implement only this one, or all equation of the same kind ? Commented Aug 31, 2011 at 8:00
  • Are you allowed to reformulate the equation? Commented Aug 31, 2011 at 8:00
  • 2
    There are specialized languages for formula transformation/equation solving. PHP does not lend itself to it, as scientific uses are not its primary domain. You might find something in Python or Perl however (and then invoke those via exec). Commented Aug 31, 2011 at 8:02
  • @pinouchon only this one, however the numbers will be dynamic Commented Aug 31, 2011 at 8:03
  • @Deve yes I can reformulate the equation Commented Aug 31, 2011 at 8:03

5 Answers 5

15

You're wanting to solve an equation, not implement it. There's a difference. Implementing the equation would be as simple as typing it in. You'd probably want to make it an equality operator (==) though.

Equation solvers are complicated, complicated things. I wouldn't try to make one when there are such good ones ( http://en.wikipedia.org/wiki/Comparison_of_computer_algebra_systems ) lying around.

Sign up to request clarification or add additional context in comments.

3 Comments

"You're wanting to solve an equation, not implement it." Oh, how I wish I could up-vote this more than once...
Thanks for pointing to the correct term. I will edit the question :)
phpCAS does not mean what you think it means.
9

You can use http://pear.php.net/package/PHP_ParserGenerator/redirected to parse the math expressions into a syntax tree, then do the maths.

((n * 2) + 10) / (n + 1) = 3 would look like:

enter image description here

The idea is to bring on the right subtree (here ...) all the numbers, and on the left all the unknownws, just as you'd do on paper.

In the end you'll have:

 + / \ n -7 

which is 0. And there you have your solution, for any math expression (with one unknown variable).

I'll leave the algorithm to you.

2 Comments

Very interesting, I will have a look :)
And how do you move all the numbers to the right side? Could you give us an example please? Thanks!
4
<?php // ((x * n) + y)/(n + 1) = z) // => n=(y-z)/(z-x) function eq ($x=0,$y=0,$z=0) { if ($z!=$x) { $n=($y-$z)/($z-$x); } else { $n='NAN'; } return $n; } ?> 

(My algebra is old and flakey but I think this is right)

9 Comments

Your function works! Would you mind explaning what was your thought process when you were writing/thinking about it?
Your function is mathematically incorect. You're missing the case $y == $x && $z == $x. In such a case, the esemble R - { 1 } is solution of the equation.
Also, if i call eq(0, 0, 1);, your function yields -1, yet ((x * n) + y)/(n + 1) = z) gives ((0 * -1) + 0)/(-1 + 1) = 1) which is impossible
My get out clause is that my algebra is old and flakey and not something I use everyday. The main thing was to check for division by 0 (hence the check for Z<>X). My thinking behind the solution was to get the equation into the form of x*n=c (even if x is a fraction). Once it's in that form, the solution was a simple re-write of the equation into PHP
@pinouchon - obviously, more checks can be added for special cases. However, I'm a programmer and not a mathematician so my caveat is still valid - my algebra is old and flakey but I think this is right
|
3

how about using brute-force??!?! might be slow and not exact:

$step = 0.00001; $err = 0.1; //error margin $start = 0; $response = 3; for($i = $start;$i <= 3;$i += $step){ if((($i * 2) + 10) / ($i + 1) >= $response - $err){ echo "the answer is $i"; } } 

You could improove this answer.. on every loop you could calculate the distance between the current answer and the desired answer, and adjust the parameters acording to that..

This reminds me my old A.I. class =)

Good Luck

1 Comment

That's a solution, but I'm looking for the proper way to solve it. Thanks for your answer though!
0

Here's how to solve that equation in C# with the Symbolism computer algebra library:

var n = new Symbol("n"); (((n * 2) + 10) / (n + 1) == 3) .IsolateVariable(n) .Disp(); 

The following is displayed on the console when that code is executed:

n == 7 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.