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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mathematical Formulas.

Status
Not open for further replies.

starter2

Programmer
Nov 10, 2008
3
CA
Hello,

I am working on a semi major project and I am having difficulty with a component of it.

I need a user to be able to imput a mathematical formula such as but not limited to: "x^2 + 2x +6" and for TCL to convert it to a tcl forumula, so that a tcl interpreter can understand it.

Basically I am interested in making a graphical interface Tk operating under TCL for the Newton Method for resolving any equation equal to 0... after an N amount of iterations specified by the user. But I want the user to be able to input mathematical expressions in a non TCL way.

Any help?

Example of what I want is to give tcl a formula of this type:

ln(x^4 + 6) and after tell it to compute f(3) and for it to tell me the result of ln((3)^4 + 6)...

Thank you
 
You have to be able to enforce some syntax rules. Why not make them Tcl-ready to start with? So instead of ln(x^4 + 6), can you enforce that the formula be log($x^4 + 6)?

If so, you can set s ... to your formula, then set x ... to your value, then expr $s

_________________
Bob Rashkin
 
(bin) 40 % set x 4
4
(bin) 41 % set s "$x^2"
4^2
(bin) 42 % expr $s
6

isn't the answer 16?

Also,

(bin) 44 % set x 4
4
(bin) 45 % set s "log(x)"
log(x)
(bin) 46 % expr $s
invalid bareword "x"
in expression "log(x)";
should be "$x" or "{x}" or "x(...)" or ...
(bin) 47 % set s "log$x"
log4
(bin) 48 % expr $s
invalid bareword "log4"
in expression "log4";
should be "$log4" or "{log4}" or "log4(...)" or ...


Having trouble here...
 
OK. You need to observe the Tcl syntax (and I didn't). I was just giving a quick example. There is no "a^b". The Tcl syntax is "pow(a,b)". And then, all "x" in the formulae need to be "$x".

_________________
Bob Rashkin
 
This is kind of what I mean!!! the default language for a TI-83 calculator is different than tcl's. We have $x^2 and it has power($x,2). Any script or way to convert a whole function? I want a user to be able to input it in the TI-83 form and for tcl to convert it to it's own syntax...
 
What you plan to do is not as easy as explaining it in two words.
First you need to define all the rules for writing your expressions. Making reference to an existing language (TI-83) is a step forward, but you need to access a full definition of it as the first step. Also, and this is only an example, as TI-83 accepts dropping the multiplication sign, you need to decide whether to do the same, and be prepared to write many more lines of code if you do.
This is really not a simple task. You need to write what, in the programming language jargon, is called a parser. Personally I wouldn't even try to do it with Tk, Perl would be my preferred candidate.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top