2
$\begingroup$

line1 = Table[{x, z + 1}, {x, 0, 2}]; Manipulate[ RegionPlot[x^2 + y^2 + z < 1, {x, 0, 2}, {y, 0, 2}, Epilog -> {Red, Line[line1]}], {{z, 0.5}, 0, 1}]enter image description herewhen I add an new variable using manipulate, it will be wrong. what should I do?enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ hard to copy code from an image so it would be better to also paste the source code used. $\endgroup$
    – Nasser
    Jun 1 at 2:23
  • $\begingroup$ Thank you. I have added it.line1 = Table[{x, z + 1}, {x, 0, 2}]; Manipulate[ RegionPlot[x^2 + y^2 + z < 1, {x, 0, 2}, {y, 0, 2}, Epilog -> {Red, Line[line1]}], {{z, 0.5}, 0, 1}] $\endgroup$
    – hu_rrr
    Jun 1 at 2:29

1 Answer 1

4
$\begingroup$

Next time please paste the code used. Since it is small, I retyped it now.

The problem is that you have global z and control variable also called z. These should not be mixed. Here is one workaround. Other workaround are possible. And always use TrackedSymbols

enter image description here

line1=Table[{x,z+1},{x,0,2}]
Manipulate[
   RegionPlot[x^2+y^2+z0<1,{x,0,2},{y,0,2},Epilog->{Red,Line[line1/.z->z0]}],
   {{z0,0},0,1},
   TrackedSymbols:>{z0}
]

I do not like mixing globals with Manipulate. Better is to use a Module inside Manipulate. Something like this and now you do not have to worry about different contexts

Manipulate[
 Module[{x, y, line1},
  line1 = Table[{x, z + 1}, {x, 0, 2}];
  RegionPlot[x^2 + y^2 + z < 1, {x, 0, 2}, {y, 0, 2}, 
   Epilog -> {Red, Line[line1]}]
  ],
 {{z, 0, "z"}, 0, 1, .1, Appearance -> "Labeled"},
 TrackedSymbols :> {z}
 ]

Update

When I change the x of line1 to start from 0.5 and end to 2, why only the line from 0.5 to 1.5 is displayed?

Because in your table the default increment is 1. Change it to the following: (ps. no need to use z0 anymore since we moved the code to inside the Manipulate.

Compare:

line1 = Table[{x, y}, {x, 0, 2}]

Mathematica graphics

with

 line1 = Table[{x, y}, {x, 1/2, 2}]

Mathematica graphics

So if you want to go to 2 in the second case need to tell it to use increment of 1/2 and not 1.

 line1 = Table[{x, y}, {x, 1/2, 2, 1/2}]

Mathematica graphics

enter image description here

Manipulate[
 Module[{x, y, line1},
  line1 = Table[{x, z + 1}, {x, 0.5, 2, .1}];
  RegionPlot[x^2 + y^2 + z < 1, {x, 0, 2}, {y, 0, 2}, 
   Epilog -> {Red, Line[line1]}]
  ],
 {{z, 0, "z"}, 0, 1, .1, Appearance -> "Labeled"},
 TrackedSymbols :> {z}
 ]
$\endgroup$
4
  • $\begingroup$ Got it. Thank you very much. $\endgroup$
    – hu_rrr
    Jun 1 at 2:44
  • $\begingroup$ I have a new problem. When I change the x of line1 to start from 0.5 and end to 2, why only the line from 0.5 to 1.5 is displayed? Manipulate[ Module[{x, y, z, line1}, line1 = Table[{x, z + 1}, {x, 0.5, 2}]; RegionPlot[x^2 + y^2 + z0 < 1, {x, 0, 2}, {y, 0, 2}, Epilog -> {Red, Line[line1 /. z -> z0]}]], {{z0, 0, "z"}, 0, 1, .1, Appearance -> "Labeled"}, TrackedSymbols :> {z0}] $\endgroup$
    – hu_rrr
    Jun 1 at 3:25
  • $\begingroup$ @hu_rrr see update. hopefully this does what you want. $\endgroup$
    – Nasser
    Jun 1 at 4:19
  • $\begingroup$ Yes, it's really helpful! $\endgroup$
    – hu_rrr
    Jun 1 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.