Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

solving an equation 2

Status
Not open for further replies.

chriisb

Programmer
Mar 13, 2003
28
US
i would like to make a program that graphs an equation written by the user in terms of X. if they enter the equation into a textbox and it stores it as a string, i cant figure out how to get it to solve the string instead of just viewing it as characters. for instance, if they just type in "2*X" and i try val(string) it gives me a value of 2. is there any command that would help with this so i wouldnt have to type in a ton of code just for the equation?
thanks
 
Depending on how complex the function, including parenthesis, and exponentiation, and support for SQRT, SIN, COS, or other mathematical functions, that can be entered into the textbox, you may find that you going to have to develop a parser, which if I were doing it, would build in reverse polish notation stack from which evalutation could take place. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
im sorry im new to programming and i dont understand what a "parser" or "reverse polish notation stack" is.
 
A parser is code which would validate that the expression is a valid expression, insuring that all of the operators have the proper number of operands, that the parenthesis balance, in other words, that the expression is a well formed expression.

Reverse Polish Notation is a method of writing an expression so that no parenthesis are required, and that operator precedence is insured. Usually implemented as a stack, its easy to evaluate as you can pop an operator off the stack, and the next one or two entries, the operator determines the count), are the operands.

Suppose I have the expression: (3*(2+3))/5
/*3+235 would be the RPN equivalent Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
You may find that the Eval method of the Microsoft Script Control library does what you need. Try doing a keyword search in this forum for "Eval" and/or "Script Control". You should find several examples.
 
strongm - do you know off the top of your head (if not, I can certainly write a quick test) if the Eval method will handle embedded mathematical function calls? Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
thanks a ton i got the script control working
 
Of course, there are a few additional details to 'flesh out' the generation of the actual chart.

To even return a SET of values, you will need a few 'ground rules':

whether the function is limited to single variables (per your example);

(presuming the limit is to single variable), what character(s) represent that variable (JUST "X", or any Char between "S" and "Z", or ... );

how to enter the coefficients (e.g. "A", "B", "C" or "1.25", "3.14159", "7");

'validate' the equation. e.g. does it follow the 'rules' established (like unto the above)?

You will also need to have the use enter the rnge and step values for the plot.

And, even to generate the value list, you WILL need ot do at least some small bit of the parseing noted by ye CajunCenturion, as ? Eval(2 * X) will always return zero, so your values list will beed to replace the "X" with a muneric value (range-step).

As far as I know, you will need to place the index (range-step) and value into some 'recordset' and feed these to the MS. Chart applet (unless - of course you are going to generate your own plotting function).

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
ooops. my bad. i was looking at the process of embedding the eval statement in a procedure, where it is necessary to pass the hi/lo/step values, and dynamically returning the results to a calling funct. it is (OF COURSE) correct that you can 'define' X as a variable and set its' value within a loop to generate the results.

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top