472,967 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,967 software developers and data experts.

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!
Jul 17 '05 #1
4 5968
Sven Dzepina wrote:
Hello people =)

Has somebody a nice script, which can solve equations ?
yes, I do now :-)
It would be super, if someone has an idea where I can get such a script /
code in php.
Use mine
Thanks.


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.
Jul 17 '05 #2
Big Thanks Pedro!
It's a great script =)

An Equation Script which can handle more variables would be better ;)
"Pedro" <he****@hotpop.com> schrieb im Newsbeitrag
news:d2********************************@4ax.com...
Sven Dzepina wrote:
Hello people =)

Has somebody a nice script, which can solve equations ?


yes, I do now :-)
It would be super, if someone has an idea where I can get such a script /
code in php.


Use mine
Thanks.


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.

Jul 17 '05 #3
Sven Dzepina wrote:
Big Thanks Pedro!
It's a great script =)
LOL, thanks

An Equation Script which can handle more variables would be better ;)


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 :-)

Jul 17 '05 #4
Hi Pedro,

you must be a very intelligent human =)

Gretting.

"Pedro" <he****@hotpop.com> schrieb im Newsbeitrag
news:bm************@ID-203069.news.uni-berlin.de...
Sven Dzepina wrote:
Big Thanks Pedro!
It's a great script =)


LOL, thanks

An Equation Script which can handle more variables would be better ;)


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 :-)

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Russell Blau | last post by:
This is not really a Python question, but it does relate to a Python program I'm working on, so this seems like as good a place as any to ask for suggestions ... I'm working on a GUI application...
20
by: Brian Kazian | last post by:
Here's my problem, and hopefully someone can help me figure out if there is a good way to do this. I am writing a program that allows the user to enter an equation in a text field using...
9
by: Stud Muffin | last post by:
Hey Basically, I'm trying to take objects created in microsoft word using equation editor (for creating clean looking math/physics equations) and putting them into some sort of webpage format....
5
w33nie
by: w33nie | last post by:
My table is pretty well complete, but I would prefer it if the value for Points could be turned into a mathematical equation, and this equation would use the data from the other fields in the table...
6
by: Trev17 | last post by:
Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question: the roots of the quadratic equation ax^2 + bx + c = 0, a...
6
by: trashman.horlicks | last post by:
Hi, A few months ago, I saw an equation parser, written in c#, and using regular expressions (I think!), but now I cannot recall where I saw it- if anyone saw anything like this, could they please...
2
by: Chuck | last post by:
Is the any way to convert an equation in Excel to something Access will understand other than manually rewriting the code? Basic equation has several *MOD(A,B) and several IF(AND(C,D)... simply...
10
by: Constantine AI | last post by:
Hi i am having a little problem with an equation function that was created from all your help previously. The function works fine itself but with a small glitch within it. Here is the function...
2
by: phoenix1990 | last post by:
so i have an entry frame where i want to input an equation, and i need to turn the string into an actual equation in terms of x. so that i can plot it on a canvas. i already know how to make the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.