Bulbe Posted April 5, 2008 Share Posted April 5, 2008 Hey everyone, i've been doing a work in math and it should be about solving quadratics in php. Well, my dictonary says that quadratics mean all this stuff, not only ax2+bx+c. So my question is, do you think it would be possible to make this in php ? You would enter the quadratic and it would show its roots Best regard, Bulb Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 5, 2008 Share Posted April 5, 2008 Trivial exercise, surely. http://en.wikipedia.org/wiki/Quadratic_equation Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 6, 2008 Share Posted April 6, 2008 Yeah, very easy - just use the quadratic formula. Check the discriminant to work out if you're going to get any real answers, then plug the values of a,b and c into the formula. You could set it up to give you non-real answers too. Quote Link to comment Share on other sites More sharing options...
Bulbe Posted April 6, 2008 Author Share Posted April 6, 2008 yes, but imagine somone wants to solve e.g. 2x2+32-2x=3x+2x-32-x2 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 6, 2008 Share Posted April 6, 2008 It wouldn't be that difficult to have it rearranged and solved. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 6, 2008 Share Posted April 6, 2008 Was bored: <?php $eqn = '-2x^2+32-2x=3x+2x-32-x^2'; function rearrange($eqn){ $sides = explode('=',$eqn); $coefficients = array(); foreach($sides as $k => $v){ preg_match_all('%(\-|\+)?[0-9x^]+%s',$v,$terms); foreach($terms[0] as $v2){ //work out which term is being used if(strpos($v2, 'x^2') !== false){ $key = 0; }elseif(strpos($v2, 'x') !== false){ $key = 1; }else{ $key = 2; } //work out the coefficient preg_match('%^(\-|\+)?[0-9]+%s',$v2,$matches); if(empty($matches)){ if($v2[0] == '-'){ $num = -1; }else{ $num = 1; } }else{ $num = $matches[0]; } @$coefficients[$k][$key] += $num;//+= because terms may not be collected } } for($x=0;$x<=2;$x++){ //collect terms. Surpress error since coeffiecent may not be set @$rearranged[$x] = $coefficients[0][$x] - $coefficients[1][$x]; } return $rearranged; } function solve($coefficients){ list($a,$b,$c) = $coefficients; $discriminant = pow($b,2) - 4*$a*$c; if($discriminant > 0){ $roots[0] = (-$b + sqrt($discriminant))/(2*$a); $roots[1] = (-$b - sqrt($discriminant))/(2*$a); }elseif($discriminant == 0){ $roots[0] = -$b/(2*$a); }else{ $real = -$b/(2*$a); $imaginary = sqrt(-$discriminant)/(2*$a); $roots[0] = $real.' + '.$imaginary.'i'; $roots[1] = $real.' - '.$imaginary.'i'; } return $roots; } print_r(solve(rearrange($eqn))); ?> Will work as long as there's nothing unexpected in the input. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 7, 2008 Share Posted April 7, 2008 without PEAR php gets very confused on imaginary numbers so any solution should verify that the radical isn't negative pre attempting to calculate it as php gets confused trying to do sqrt(-1n) where n is any thing above 0. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 7, 2008 Share Posted April 7, 2008 Was that in response to my code? It handes the imaginary numbers fine. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 10, 2008 Share Posted April 10, 2008 Was that in response to my code? It handes the imaginary numbers fine. yes cause you verify the descrimenent I was saying in general php has no ability to handle complex answers (although it should) Quote Link to comment Share on other sites More sharing options...
Barand Posted April 10, 2008 Share Posted April 10, 2008 There's your next project. A PHP extension to handle complex numbers. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.