Re: Drawing curves with equation.
- To: mathgroup@smc.vnet.net
- Subject: [mg11405] Re: Drawing curves with equation.
- From: Allan Hayes <hay@haystack.demon.co.uk>
- Date: Sun, 8 Mar 1998 20:13:25 -0500
- References: <6dqqie$rk6@smc.vnet.net>
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