Equation 
July 17th, 2005, 12:32 AM
| | | Equation
Hello people =)
Has somebody a nice script, which can solve equations ?
It would be super, if someone has an idea where I can get such a script /
code in php.
Thanks.
Gretting! | 
July 17th, 2005, 12:32 AM
| | | Re: Equation
Sven Dzepina wrote:[color=blue]
>Hello people =)
>
>Has somebody a nice script, which can solve equations ?[/color]
yes, I do now :-)
[color=blue]
>It would be super, if someone has an idea where I can get such a script /
>code in php.[/color]
Use mine
[color=blue]
>Thanks.[/color]
you're welcome :-)))
<?php // solve equation
function solve($eq) {
$eq = str_replace(' ', '', $eq);
// validate expression
$terms = explode('=', $eq);
if (count($terms) != 2) {
return('syntax error: you must have one (and only one) "=" in ['
. $eq . '].');
}
// put rhs in lhs
$eq = $terms[0] . '-(' . $terms[1] . ')';
if (substr_count($eq, '(') != substr_count($eq, ')')) {
return('syntax error: parenthesis mismatch in [' . $eq . '].');
}
if (preg_match('/\([^)]*\(/', $eq)) {
return('syntax error: only one level of parenthesis allowed in ['
. $eq . '].');
}
if (preg_match('/[^x0-9+*\/().=-]/', $eq)) {
return('syntax error: the only valid characters are x, 0-9, +, '
. '*, /, (, ), ., and - in [' . $eq . ']');
}
if (preg_match('/[^x0-9\)][-+\/*]/', $eq)) {
return('syntax error: you can only operate on numbers in ['
. $eq . ']');
}
if (preg_match('/[-+\/*][^x0-9\(]/', $eq)) {
return('syntax error: you can only operate on numbers in ['
. $eq . ']');
}
$eq = str_replace('x', '$x', $eq);
$y0 = $y1 = 0;
$x = 0; eval('$y0 = ' . $eq . ';');
$x = 1; eval('$y1 = ' . $eq . ';');
$slope = $y1 - $y0;
$intersect = $y0;
return (- ($intersect / $slope));
}
$eq = '2 * (x + 4) = 10';
echo $eq, ' ==> x = ', solve($eq), '<br />';
$eq = '(2 * x) + 4 = 10';
echo $eq, ' ==> x = ', solve($eq), '<br />';
$eq = '2 * x + 4 = 10';
echo $eq, ' ==> x = ', solve($eq), '<br />';
$eq = '4 + x / 10 = 2';
echo $eq, ' ==> x = ', solve($eq), '<br />';
?>
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded. | 
July 17th, 2005, 12:32 AM
| | | Re: Equation
Big Thanks Pedro!
It's a great script =)
An Equation Script which can handle more variables would be better ;)
"Pedro" <hexkid@hotpop.com> schrieb im Newsbeitrag
news:d2foov8ts7hjs4as316ki91ts6erme065m@4ax.com...[color=blue]
> Sven Dzepina wrote:[color=green]
> >Hello people =)
> >
> >Has somebody a nice script, which can solve equations ?[/color]
>
> yes, I do now :-)
>[color=green]
> >It would be super, if someone has an idea where I can get such a script /
> >code in php.[/color]
>
> Use mine
>[color=green]
> >Thanks.[/color]
>
> you're welcome :-)))
>
>
>
>
> <?php // solve equation
> function solve($eq) {
> $eq = str_replace(' ', '', $eq);
>
> // validate expression
> $terms = explode('=', $eq);
> if (count($terms) != 2) {
> return('syntax error: you must have one (and only one) "=" in ['
> . $eq . '].');
> }
>
> // put rhs in lhs
> $eq = $terms[0] . '-(' . $terms[1] . ')';
>
> if (substr_count($eq, '(') != substr_count($eq, ')')) {
> return('syntax error: parenthesis mismatch in [' . $eq . '].');
> }
> if (preg_match('/\([^)]*\(/', $eq)) {
> return('syntax error: only one level of parenthesis allowed in ['
> . $eq . '].');
> }
> if (preg_match('/[^x0-9+*\/().=-]/', $eq)) {
> return('syntax error: the only valid characters are x, 0-9, +, '
> . '*, /, (, ), ., and - in [' . $eq . ']');
> }
> if (preg_match('/[^x0-9\)][-+\/*]/', $eq)) {
> return('syntax error: you can only operate on numbers in ['
> . $eq . ']');
> }
> if (preg_match('/[-+\/*][^x0-9\(]/', $eq)) {
> return('syntax error: you can only operate on numbers in ['
> . $eq . ']');
> }
>
> $eq = str_replace('x', '$x', $eq);
> $y0 = $y1 = 0;
> $x = 0; eval('$y0 = ' . $eq . ';');
> $x = 1; eval('$y1 = ' . $eq . ';');
> $slope = $y1 - $y0;
> $intersect = $y0;
> return (- ($intersect / $slope));
> }
>
> $eq = '2 * (x + 4) = 10';
> echo $eq, ' ==> x = ', solve($eq), '<br />';
>
> $eq = '(2 * x) + 4 = 10';
> echo $eq, ' ==> x = ', solve($eq), '<br />';
>
> $eq = '2 * x + 4 = 10';
> echo $eq, ' ==> x = ', solve($eq), '<br />';
>
> $eq = '4 + x / 10 = 2';
> echo $eq, ' ==> x = ', solve($eq), '<br />';
> ?>
>
>
> --
> I have a spam filter working.
> To mail me include "urkxvq" (with or without the quotes)
> in the subject line, or your mail will be ruthlessly discarded.[/color] | 
July 17th, 2005, 12:32 AM
| | | Re: Equation
Sven Dzepina wrote:[color=blue]
> Big Thanks Pedro!
> It's a great script =)[/color]
LOL, thanks
[color=blue]
> An Equation Script which can handle more variables would be better ;)[/color]
Yes, I'm pretty sure it would.
If you can guarantee the equations are normalized
3x - 6 = 0
instead of
4(x-1) = x+2
that makes making the solver script terribly easy for
a) ax+b=0
b) ax^2+bx+c=0
c) ax+by+c=0 (and) dx+ey+f=0
d) ...
Input only the constants ... not the whole equation;
for the equation above you'd input only: a=3, and b=-6
and then solve (after checking for division by zero only):
return -($b/$a);
or, for equation in type 2:
return array((-$b+sqrt($a*$a-4*$a*$c))/(2*$a),
(-$b-sqrt($a*$a-4*$a*$c))/(2*$a));
etc. ... ...
Happy Coding :-) | 
July 17th, 2005, 12:33 AM
| | | Re: Equation
Hi Pedro,
you must be a very intelligent human =)
Gretting.
"Pedro" <hexkid@hotpop.com> schrieb im Newsbeitrag
news:bmhvt2$n6tdt$1@ID-203069.news.uni-berlin.de...[color=blue]
> Sven Dzepina wrote:[color=green]
> > Big Thanks Pedro!
> > It's a great script =)[/color]
>
> LOL, thanks
>
>[color=green]
> > An Equation Script which can handle more variables would be better ;)[/color]
>
> Yes, I'm pretty sure it would.
> If you can guarantee the equations are normalized
>
> 3x - 6 = 0
>
> instead of
>
> 4(x-1) = x+2
>
> that makes making the solver script terribly easy for
>
> a) ax+b=0
> b) ax^2+bx+c=0
> c) ax+by+c=0 (and) dx+ey+f=0
> d) ...
>
> Input only the constants ... not the whole equation;
> for the equation above you'd input only: a=3, and b=-6
>
> and then solve (after checking for division by zero only):
> return -($b/$a);
>
>
> or, for equation in type 2:
> return array((-$b+sqrt($a*$a-4*$a*$c))/(2*$a),
> (-$b-sqrt($a*$a-4*$a*$c))/(2*$a));
>
> etc. ... ...
>
>
>
> Happy Coding :-)
>[/color] | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|