MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Drawing curves with equation.



Olivier Gilloire wrote:

> I'm a c++ programmer, and I would like to learn how to draw a curve
> corresponding to an equation, given in a string format like "x^2".
> 
> My problem is not how to draw the curve, but rather how to analyze the
> string and interpret it as a mathematic equation.
> 
> code samples are welcome !!

Olivier:

In[1]:=
expr = ToExpression["x^2"]

Out[1]=
 2
x

In[2]:=
Plot[expr, {x,-1,1}]

Mathematica has many string manipulation functions that you might use
before converting to an expression

In[3]:=
?*String*

>From In[3]:=
DisplayString        StringLength
InputString          StringMatchQ
InString             StringPosition
RepeatedString       StringQ
ShowStringCharacters StringReplace
String               StringReplacePart
StringBreak          StringReverse
StringByteCount      StringSkeleton
StringDrop           StringTake
StringForm           StringToStream
StringInsert         ToString
StringJoin           WriteString

We can avoid extra definitions:

Plot[ToExpression["x^2"],{x,-1,1}]

Plot[Evaluate[ToExpression["x^2"]],{x,-1,1}]

(the latter is more efficient - it does the conversion to expression
before numerical evaluation at sample points, the former converts for
each such calculation) -- 
Allan Hayes
Training and Consulting
Leicester, UK
hay@haystack.demon.co.uk
http://www.haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44 (0)116 271 8642




  • Prev by Date: Re: Simplifying algebraic expr: howto?
  • Next by Date: making MatrixForm[] default?
  • Prev by thread: Re: Drawing curves with equation.
  • Next by thread: RE: Drawing curves with equation.