By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,831 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,831 IT Pros & Developers. It's quick & easy.

evaluate an algebraic expression

P: n/a
How do i evaulate a mathematical expression that is entered in a text
field.
Ex:
Text Fields:

Xo=23
X1= 250

Expression: y = Xoe^(x1+Xo)-cos(X0+X1)+23Xo
How do i manage that?

tx
Aug 31 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On Aug 31, 10:29*am, serave <ramirez.sebast...@gmail.comwrote:
How do i evaulate a mathematical expression that is entered in a text
field.
Ex:
Text Fields:

Xo=23
X1= 250

Expression: y = Xoe^(x1+Xo)-cos(X0+X1)+23Xo

How do i manage that?
You have X0, x1, Xo, and X1 on the right side of the equation but
define only Xo and X1. Thus the equation can not be solved as a
numerical value for y unless you define one other value with some
numerical value. Perhaps there is a typo in the equation? If you mean
to simplify the equation as written, that is a problem in algebra.

If you define another value or correct a possible typo, either the
math functions of php or Javascript can be used to solve the
equation(the value of y for perhaps a value of x1 ?) because the
simple math functions, exponentials, and cos are all contained in both
languages.
Aug 31 '08 #2

P: n/a
On 31 aug, 17:29, serave <ramirez.sebast...@gmail.comwrote:
Xo=23
X1= 250

Expression: y = Xoe^(x1+Xo)-cos(X0+X1)+23Xo
To do this in PHP, you first parse the expression to convert it from a
string to a structure. You have to make a parser to do this. Next, you
compute the result.

It is far from trivial to do this in PHP. A better approach may be to
use something like bc (http://www.gnu.org/software/bc/) or another
program to evaluate the expression for you.
Aug 31 '08 #3

P: n/a
Do you know if there is something already wrote, maybe in javascript
or something caise i will take lots of lines to do it again. And there
might be somebody that has already wrote that?

Aug 31 '08 #4

P: n/a
On Aug 31, 1:03*pm, cwdjrxyz <spamtr...@cwdjr.infowrote:
On Aug 31, 10:29*am, serave <ramirez.sebast...@gmail.comwrote:
How do i evaulate a mathematical expression that is entered in a text
field.
Ex:
Text Fields:
Xo=23
X1= 250
Expression: y = Xoe^(x1+Xo)-cos(X0+X1)+23Xo
How do i manage that?

You have X0, x1, Xo, and X1 on the right side of the equation but
define only Xo and X1. Thus the equation can not be solved as a
numerical value for y unless you define one other value with some
numerical value. Perhaps there is a typo in the equation? If you mean
to simplify the equation as written, that is a problem in algebra.

If you define another value or correct a possible typo, either the
math functions of php or Javascript can be used to solve the
equation(the value of y for perhaps a value of x1 ?) *because the
simple math functions, exponentials, and cos are all contained in both
languages.
You will find how to use the math functions in Javascript and PHP in
most more complete books published on these subjects in the last
several years. Reading several of the examples given in most of these
books will likely help.

To show how math functions are used on a real web page, I have
examples using both Javascript and PHP.

For the Javascript example, view the source code of
http://www.cwdjr.net/math/cylinderAbsorption.html .

For the PHP code for http://www.cwdjr.net/calendar2/perpetual_calendar.php
view the text file:
http://www.cwdjr.net/calendar2/perpetual_calendar.txt .
Aug 31 '08 #5

P: n/a
My problem is not the equation, or simplifing it. i just need to
evaluate what evere equation is given by the user. How will no write
it as php or javsacsript will demant it. I need to transform the
equation to a language like php or javascript or whatevere i can use
web. I'm looking for some info like that. Maybe a predifine function
that does that job.
Aug 31 '08 #6

P: n/a
serave wrote:
Do you know if there is something already wrote, maybe in javascript
or something caise i will take lots of lines to do it again. And there
might be somebody that has already wrote that?

It won't help much to have something already written in javascript -
unless you want to try to convert it to PHP yourself.

But even if you do, I don't know of anything which wouldn't be
proprietary. And as Sjoerd said, this is not trivial. It can be done,
but you have a lot of work ahead of you.

I did something like this many years ago in C. I used a structure with
three parameters - the two values and the operation to be performed.
The operation was simple - but if a value was computed, I pointed to the
structure whose results was the value (via a UNION in the structure) to
be used.

However, I was not dealing with variables, etc. All I need to do was
give the results for an expression. It was orders of magnitude simpler
than what you want to do. But it was still difficult to get everything
sorted out and computed properly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 31 '08 #7

P: n/a
On 31 ago, 15:32, Jerry Stuckle <jstuck...@attglobal.netwrote:
serave wrote:
Do you know if there is something already wrote, maybe in javascript
or something caise i will take lots of lines to do it again. And there
might be somebody that has already wrote that?

It won't help much to have something already written in javascript -
unless you want to try to convert it to PHP yourself.

But even if you do, I don't know of anything which wouldn't be
proprietary. And as Sjoerd said, this is not trivial. *It can be done,
but you have a lot of work ahead of you.

I did something like this many years ago in C. *I used a structure with
three parameters - the two values and the operation to be performed.
The operation was simple - but if a value was computed, I pointed to the
structure whose results was the value (via a UNION in the structure) to
be used.

However, I was not dealing with variables, etc. *All I need to do was
give the results for an expression. It was orders of magnitude simpler
than what you want to do. *But it was still difficult to get everything
sorted out and computed properly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
tx for you information. But actually i'm going to work with php, but
if i can get something that evaluates(give the result) the function
writen by an end user, i can put it in variable and from then on i
will start my job in php
Aug 31 '08 #8

P: n/a
serave wrote:
On 31 ago, 15:32, Jerry Stuckle <jstuck...@attglobal.netwrote:
>serave wrote:
>>Do you know if there is something already wrote, maybe in javascript
or something caise i will take lots of lines to do it again. And there
might be somebody that has already wrote that?
It won't help much to have something already written in javascript -
unless you want to try to convert it to PHP yourself.

But even if you do, I don't know of anything which wouldn't be
proprietary. And as Sjoerd said, this is not trivial. It can be done,
but you have a lot of work ahead of you.

I did something like this many years ago in C. I used a structure with
three parameters - the two values and the operation to be performed.
The operation was simple - but if a value was computed, I pointed to the
structure whose results was the value (via a UNION in the structure) to
be used.

However, I was not dealing with variables, etc. All I need to do was
give the results for an expression. It was orders of magnitude simpler
than what you want to do. But it was still difficult to get everything
sorted out and computed properly.

tx for you information. But actually i'm going to work with php, but
if i can get something that evaluates(give the result) the function
writen by an end user, i can put it in variable and from then on i
will start my job in php
Sjoerd's is probably the best suggestion. But even so, you're looking
at lots of work.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 31 '08 #9

P: n/a
On Aug 31, 3:27*pm, serave <ramirez.sebast...@gmail.comwrote:
My problem is not the equation, or simplifing it. i just need to
evaluate what evere equation is given by the user. How will no write
it as php or javsacsript will demant it. I need to transform the
equation to a language like php or javascript or whatevere i can use
web. I'm looking for some info like that. Maybe a predifine function
that does that job.
Thanks for the additional information. If I understand you correctly
now, you want a program into which you can type a mathematical
equation much as it would appear in a math text. Then you want the
program to take that input, convert it into some code to solve the
equation and perhaps tabulate the results in tables, plots etc.

If the above is so, there long have been programs designed to do this.
They are used by people in math, engineering, the sciences etc. Such
programs are extremely complex, and if this is what you have in mind I
would suggest that you not waste your time on such a project. I am not
sure that any one person, no matter how skilled, could come up with
such a program that would be anywhere close to the quality of what is
available. Some such programs can be put up on a server so that input
can be from the web. However such programs are very expensive, for
good reason. Even so, input has to follow strict rules for the program
to work properly. The program must have very complex error detection
features, for there is no telling what someone may type in. Such a
program would most likely be written mostly in C++ or something of
that sort.
Aug 31 '08 #10

P: n/a
On 31 ago, 17:44, cwdjrxyz <spamtr...@cwdjr.infowrote:
On Aug 31, 3:27*pm, serave <ramirez.sebast...@gmail.comwrote:
My problem is not the equation, or simplifing it. i just need to
evaluate what evere equation is given by the user. How will no write
it as php or javsacsript will demant it. I need to transform the
equation to a language like php or javascript or whatevere i can use
web. I'm looking for some info like that. Maybe a predifine function
that does that job.

Thanks for the additional information. If I understand you correctly
now, you want a program into which you can type a mathematical
equation much as it would appear in a math text. Then you want the
program to take that input, convert it into some code to solve the
equation and perhaps tabulate the results in tables, plots etc.

If the above is so, there long have been programs designed to do this.
They are used by people in math, engineering, the sciences etc. Such
programs are extremely complex, and if this is what you have in mind I
would suggest that you not waste your time on such a project. I am not
sure that any one person, no matter how skilled, could come up with
such a program that would be anywhere close to the quality of what is
available. Some such programs can be put up on a server so that input
can be from the web. However such programs are very expensive, for
good reason. Even so, input has to follow strict rules for the program
to work properly. The program must have very complex error detection
features, for there is no telling what someone may type in. Such a
program would most likely be written mostly in C++ or something of
that sort.
tx a lot what you are saying is exactly what im looking for,but
hopefully free, or just a library to get the cuntion i just need it to
dosome additional work. i need to program some nnumerical methods, but
the entries must be given from and end user so i need the equation
parser
Aug 31 '08 #11

P: n/a
serave wrote:
On 31 ago, 17:44, cwdjrxyz <spamtr...@cwdjr.infowrote:
>On Aug 31, 3:27 pm, serave <ramirez.sebast...@gmail.comwrote:
>>My problem is not the equation, or simplifing it. i just need to
evaluate what evere equation is given by the user. How will no write
it as php or javsacsript will demant it. I need to transform the
equation to a language like php or javascript or whatevere i can use
web. I'm looking for some info like that. Maybe a predifine function
that does that job.
Thanks for the additional information. If I understand you correctly
now, you want a program into which you can type a mathematical
equation much as it would appear in a math text. Then you want the
program to take that input, convert it into some code to solve the
equation and perhaps tabulate the results in tables, plots etc.

If the above is so, there long have been programs designed to do this.
They are used by people in math, engineering, the sciences etc. Such
programs are extremely complex, and if this is what you have in mind I
would suggest that you not waste your time on such a project. I am not
sure that any one person, no matter how skilled, could come up with
such a program that would be anywhere close to the quality of what is
available. Some such programs can be put up on a server so that input
can be from the web. However such programs are very expensive, for
good reason. Even so, input has to follow strict rules for the program
to work properly. The program must have very complex error detection
features, for there is no telling what someone may type in. Such a
program would most likely be written mostly in C++ or something of
that sort.

tx a lot what you are saying is exactly what im looking for,but
hopefully free, or just a library to get the cuntion i just need it to
dosome additional work. i need to program some nnumerical methods, but
the entries must be given from and end user so i need the equation
parser
The point is - you are not going to find something like that for free.
They'll probably cost you tens of thousands of dollars.

As we've been trying to tell you. Such a script is not trivial. It
takes a lot of work to develop. And people don't give all of that work
away for free. They need to live, also.

It's also too specialized for the open source community. You'll find
very little interest there.

Probably the best free you'll find will be what Sjoerd recommended.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 1 '08 #12

P: n/a
serave wrote:
On 31 ago, 15:32, Jerry Stuckle <jstuck...@attglobal.netwrote:
>serave wrote:
>>Do you know if there is something already wrote, maybe in javascript
or something caise i will take lots of lines to do it again. And there
might be somebody that has already wrote that?
It won't help much to have something already written in javascript -
unless you want to try to convert it to PHP yourself.

But even if you do, I don't know of anything which wouldn't be
proprietary. And as Sjoerd said, this is not trivial. It can be done,
but you have a lot of work ahead of you.

I did something like this many years ago in C. I used a structure with
three parameters - the two values and the operation to be performed.
The operation was simple - but if a value was computed, I pointed to the
structure whose results was the value (via a UNION in the structure) to
be used.

However, I was not dealing with variables, etc. All I need to do was
give the results for an expression. It was orders of magnitude simpler
than what you want to do. But it was still difficult to get everything
sorted out and computed properly.

tx for you information. But actually i'm going to work with php, but
if i can get something that evaluates(give the result) the function
writen by an end user, i can put it in variable and from then on i
will start my job in php
Just as a precautionary note, if you actually try to write some sort
of equation parser, you may be tempted to use eval(). *DON'T*. User
input mixed with eval() is perhaps the most dangerous exploit you can
expose.

If you do try to use Sjoerd's suggestion, do not forget to look up
PHP's command line sanitizing functions. For example, when passing
arguments, use escapeshellarg(). It's used for escaping *individual*
arguments.

Be extremely careful when exposing user input to the shell.

--
Curtis
Sep 1 '08 #13

P: n/a
On Aug 31, 5:29*pm, serave <ramirez.sebast...@gmail.comwrote:
How do i evaulate a mathematical expression that is entered in a text
field.
What exactly do you want? Why do you want to evaluate mathematical
expressions? Do you simply want the numerical value of y?
Sep 1 '08 #14

P: n/a
On 1 sep, 02:50, Sjoerd <sjoer...@gmail.comwrote:
On Aug 31, 5:29*pm, serave <ramirez.sebast...@gmail.comwrote:
How do i evaulate a mathematical expression that is entered in a text
field.

What exactly do you want? Why do you want to evaluate mathematical
expressions? Do you simply want the numerical value of y?
Exactly, thats what i need. The be explicit. the user enter the values
of the x`s and the program should return the y value, given teh math
equiation entered by the user.
Sep 14 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.