Message Boards Message Boards

0
|
20296 Views
|
26 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Place an equation on a Plot?

Posted 7 years ago

I want to place the equation on the plot. I make macros so it is easier to create plots. In this case y=x^3, with domain between [-5,5], and range between [-25, 25] The below script allows the user to enter the function and domain and range, and have the graph be generated automatically. This is just a small portion of the whole script.

The question is how to add a text version of the equation to the graph, and still use this template structure. I was able to do it with version 6, but not with version 7.

xL = -5
xR = 5

yT = 25

yB = -25

t = x^3

pp = Plot[Evaluate[t], {x, xL, xR}]

This is the complete script

clear[GraphSize]
clear[Tg]
clear[Te]
clear[Wg]
clear[We]
clear[Shifte]
clear[Sidee]
clear[xL]
clear[xR]
clear[yT]
clear[yB]
clear[t]
Null
Null
Null
Null
Null
Null
Null
Null
Null
Null
Null
Null
Null

GraphSize = 185
Tg = 6
Te = 6
Wg = Bold
We = Bold
Shifte = 0
Sidee = -1

Tope = 1

xL = -5`
xR = 5`
yT = 25
yB = -25

t = x^3

pp = Plot[Evaluate[t], {x, xL, xR}, 
   BaseStyle -> {FontSize -> Tg, FontWeight -> Wg}, 
   ImageSize -> {GraphSize, Automatic}, AspectRatio -> 1/1,  
   Ticks -> {{xL, xL + (xR - xL)/10, xL + 2 (xR - xL)/10, 
      xL + 3 (xR - xL)/10, xL + 4 (xR - xL)/10, xL + 5 (xR - xL)/10, 
      xL + 6 (xR - xL)/10, xL + 6 (xR - xL)/10, xL + 7 (xR - xL)/10, 
      xL + 8 (xR - xL)/10, xL + 9 (xR - xL)/10, xR}, {yB, 
      yB + (yT - yB)/10, yB + 2 (yT - yB)/10, yB + 3 (yT - yB)/10, 
      yB + 4 (yT - yB)/10, yB (yT - yB)/10, yB + 6 (yT - yB)/10, 
      yB + 7 (yT - yB)/10, yB + 8 (yT - yB)/10, yB (yT - yB)/10, yT}},
    GridLines -> {{xL, xL + (xR - xL)/10, xL + 2 (xR - xL)/10, 
      xL + 3 (xR - xL)/10, xL + 4 (xR - xL)/10, xL + 5 (xR - xL)/10, 
      xL + 6 (xR - xL)/10, xL + 6 (xR - xL)/10, xL + 7 (xR - xL)/10, 
      xL + 8 (xR - xL)/10, xL + 9 (xR - xL)/10, xR}, {yB - yB/100, 
      yB + (yT - yB)/10, yB + 2 (yT - yB)/10, yB + 3 (yT - yB)/10, 
      yB + 4 (yT - yB)/10, yB + 5 (yT - yB)/10, yB + 6 (yT - yB)/10, 
      yB + 7 (yT - yB)/10, yB + 8 (yT - yB)/10, yB + 9 (yT - yB)/10, 
      yT}},  GridLinesStyle -> Directive[Gray, Thin], 
   AxesStyle -> Arrowheads[{-0.05, 0.05}],  AxesLabel -> {"x", "y"}, 
   AxesOrigin -> {0, 0}, PlotRange -> {yB, yT}, 
   PlotStyle -> RGBColor[1, 0, 1]] /. 
  Line[x_] -> {Arrowheads[{-.05, .05}], Arrow[x]}
Export["F:\\OneEquation.eps", pp, "EPSTIFF"]
POSTED BY: Peter Munro
26 Replies

This does what you want:

t = HoldForm[x^3]
t1 = HoldForm[(2 x)^3]
t2 = HoldForm[3 (2 x)^3]
parenteqn = y == t
eqn1 = y == t1
eqn2 = y == t2

But you must release the hold in order to plot them:

Plot[{ReleaseHold[t], ReleaseHold[t1], ReleaseHold[t2]}, {x, -2, 2}, PlotLegend -> {parenteqn, eqn1, eqn2}, 
LegendPosition -> {1, -.4}, LegendShadow -> None]

In Version 11 I use:

Plot[{ReleaseHold[t], ReleaseHold[t1], ReleaseHold[t2]}, {x, -2, 2}, 
 PlotLegends -> {parenteqn, eqn1, eqn2}]

To get:

enter image description here

POSTED BY: Neil Singer
Posted 7 years ago
Needs["PlotLegends`"]
clear[t]
clear[t1]
clear[t2]
clear[parenteqn]
clear[eqn1]
clear[eqn2]
clear[y]

t = x^3
t1 = (2 x)^3
t2 = 3 (2 x)^3
parenteqn = y == t
eqn1 = y == t1
eqn2 = y == t2

Plot[{t, t1, t2}, {x, -2, 2}, 
 PlotLegend -> {parenteqn, HoldForm[y = b] /. b -> t1, 
   HoldForm[y = b] /. b -> t2}, LegendPosition -> {1, -.4}, 
 LegendShadow -> None]

Gives the same result

Attachments:
POSTED BY: Peter Munro

You can change the content of HoldForm from outside using replacement rules:

HoldForm[y = b] /. b -> t1
POSTED BY: Gianluca Gorni

Yes that is true, but how does Holdform work with variables like t, t1, t2, or parenteqn, eqn1, eqn2?

The idea is to put the equation that was graphed on the graph also in text form as a check and for the students benefit.

POSTED BY: Gianluca Gorni

If you want that form then do what Gianluca posted:

HoldForm[y = (2 x)^3]

This will give you exactly that form in the legend. In your case:

Plot[{t, t1, t2}, {x, -2, 2}, PlotLegend -> {parenteqn, HoldForm[y = (2 x)^3], HoldForm[y = 3 (2 x)^3]}, 
LegendPosition -> {1, -.4}, LegendShadow -> None]
POSTED BY: Neil Singer
Posted 7 years ago

Yes that is true, but how does Holdform work with variables like t, t1, t2, or parenteqn, eqn1, eqn2?

The idea is to put the equation that was graphed on the graph also in text form as a check and for the students benefit.

POSTED BY: Peter Munro

Peter,

Gianluca is correct. you keep using = instead of ==. if you want an expression like y = x^3 to print on the plot you MUST type it as y ==x^3 (Note there are two equal signs here!!!)

You need one small change to your posted code:

t = x^3
t1 = (2 x)^3
t2 = 3 (2 x)^3
parenteqn = y == t
eqn1 = y == t1
eqn2 = y == t2

Your plot command will now work as expected (in Version 7 -- I can't run it with my newer version).

Just so you understand, the expression:

parenteqn = y == t

means "define parenteqn to be the equation y == x^3" -- the "t" gets substituted. This will now put "y=x^3" where you want it in the legend.

POSTED BY: Neil Singer
Posted 7 years ago

That is exactly what I thought, but it did not work before, but partially worked now. This is what I got. The double = signs worked this time, but mathematica simplified the equation and did not show the shifts.

Needs["PlotLegends`"]
clear[t]
clear[t1]
clear[t2]
clear[parenteqn]
clear[eqn1]
clear[eqn2]
clear[y]

t = x^3
t1 = (2 x)^3
t2 = 3 (2 x)^3
parenteqn = y == t
eqn1 = y == t1
eqn2 = y == t2

Plot[{t, t1, t2}, {x, -2, 2}, PlotLegend -> {parenteqn, eqn1, eqn2}, 
LegendPosition -> {1, -.4}, LegendShadow -> None]
Attachments:
POSTED BY: Peter Munro

If you insist on having = instead of ==, you can do it with HoldForm:

Plot[Callout[t, Style[HoldForm[y = t^3], Red, Medium]], {x, xL, xR}]
POSTED BY: Gianluca Gorni

I think the misunderstanding is that an expression like t = x^3 is not an equation -- it is an assignment -- t is assigned the value of x^3. If you want the equation you must enter y == x^3 or if you already defined t, y == t. To put your expression on the plot in an equation form you need to do something like this:

eqn = y == t

Which assigns the equation y == t (which evaluates to y == x^3) to the variable, eqn.

Then plot it with

Plot[Callout[t, Style[eqn, Red, Medium]], {x, xL, xR}]

to get

enter image description here

POSTED BY: Neil Singer
Posted 7 years ago

This is what I want, but Callout does not work with version 7, so I opted for PlotLabel for now. Later I will want to know how to use something like Legended command for graphing translations. In that case, I would want to have a have a legend to show the progression of translations from y=sinx to y=2sinx to y=2sin3x to y=2sin3(x+pi/4)] to y=2sin[3(x+pi/4)]+1 This is for the eTutoring.com project. see [http://www.etutoring.com/Free_Videos.php I need youtube subscribers, and/or donations. I am not making videos for a while while my right arm recovers from an injury. I have also added an image of the more detailed plot

xL = -5 

xR = 5

yT = 25

yB = -25

t = x^3

eqn = y == t

pp = Plot[t, {x, xL, xR}, PlotLabel -> eqn]
Attachments:
POSTED BY: Peter Munro
Posted 7 years ago

I have been using PlotLegend, but have had some difficulty adding the y= part. Do you know how to do that? Using the method for one equation did not work for multiple equations. I want to have a legend for translations.

clear[t]
clear[t1]
clear[t2]
clear[parenteqn]
clear[eqn1]
clear[eqn2]
clear[y]

t = x^3
t1 = (2 x)^3
t2 = 3 (2 x)^3
parenteqn = t
eqn1 = t1
eqn2 = t2

Plot[{t, t1, t2}, {x, -2, 2}, PlotLegend -> {parenteqn, eqn1, eqn2}, 
 LegendPosition -> {1, -.4}, LegendShadow -> None]
Attachments:
POSTED BY: Peter Munro

I am a bit confused, The Callout suggested by Gianluca seems to be what you requested -- just change the variable slightly:

Plot[Callout[t, t], {x, xL, xR}]

or, with formatting

Plot[Callout[t, Style[t, Red, Medium]], {x, xL, xR}]

This gives the plot:

enter image description here

You can experiment with different formats, etc. (I would suggest that you try a variety before reposting).

POSTED BY: Neil Singer
Posted 7 years ago

Now we are getting somewhere, but the question is how to put the equation on the graph. The equation would be either y=x^3 or y=x^3.

POSTED BY: Peter Munro
Posted 7 years ago

Try this one

Plot[t^3 && -25 <= t^3 <= 25, {t, -5, 5}]

POSTED BY: Omar Alsuhaimi
Posted 7 years ago

Your reply does not put the equation on the graph

POSTED BY: Peter Munro
Plot[t^3 && -25 <= t^3 <= 25, {t, -5, 5}
 , Epilog -> Text[y == x^3, {3.2, 15}]
 ]
POSTED BY: Jos Klaps
Posted 7 years ago

How does your answer relate to

xL = -5 xR = 5

yT = 25

yB = -25

t = x^3

pp = Plot[Evaluate[t], {x, xL, xR}]

POSTED BY: Peter Munro

Dear, @Peter Munro, you did not format code properly again as we asked you. It should be syntax-highlighted and framed, for example:

pp = Plot[Evaluate[t], {x, xL, xR}]

Please learn to format code properly on this forum: http://wolfr.am/READ-1ST

POSTED BY: Moderation Team

Here are a few ways:

Plot[t^3, {t, -5, 5}, Epilog -> Text[t^3, {3, 40}]]
Plot[Callout[t^3, t^3], {t, -5, 5}]
Plot[Legended[t^3, t^3], {t, -5, 5}]
Plot[t^3, {t, -5, 5}, PlotLabel -> t^3]
POSTED BY: Gianluca Gorni
Posted 7 years ago

The function is y=x^3, not t^3.

POSTED BY: Peter Munro
Posted 7 years ago

The equation is y=x^3

Plot[t^3, {t, -5, 5}, Epilog -> Text[t^3, {3, 40}]]

Does put t^3 on the graph, but does not put y=x^3 on the graph, and does not use the variable and table format.

POSTED BY: Peter Munro

Please avoid making multiple comments of the same meaning and please do invest some work into this yourself. You can easily modify the code for what you need:

Plot[t^3, {t, -5, 5}, Epilog -> Text[y == x^3, {3, 40}]]

Please also read forum rules and format code correctly: http://wolfr.am/READ-1ST

POSTED BY: Sam Carrettie
Posted 7 years ago

This is the original script

xL = -5 xR = 5

yT = 25

yB = -25

t = x^3

pp = Plot[Evaluate[t], {x, xL, xR}]

Your answer does not use variables. I know how to put the equation on the graph without using the variables, and I know how to do it using version 6.

POSTED BY: Peter Munro

@Peter Munro. please learn to format code properly on this forum: http://wolfr.am/READ-1ST

You keep posting non-formatted code.

POSTED BY: Moderation Team
Posted 7 years ago

Hi

Al code I post can be copied and pasted into version 7 and run

POSTED BY: Peter Munro
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract