2
$\begingroup$

I have a line of code K1=1; Plot[ Table[K1 xt x - K1 x^2,{xt,0.5,2.5,0.2}],{x,0,1}]. It generates multiple plot for on the same figure but whenever x > xt, value becomes negative which I don't need in the plot. Is there a way to do something like this Plot[Table[K1 xt x - K1 x^2,{xt,0.5,2.5,0.2}],{x,0,xt}]. Whenever I run the 2nd command it returns a error saying Limiting value xt is not a machine sized real number(I think it basically need a constant to be there).

$\endgroup$
5
  • $\begingroup$ It’ll be helpful to include definition for K1. $\endgroup$ – CA Trevillian Mar 7 at 6:41
  • $\begingroup$ @CATrevillian K1=1 $\endgroup$ – A Q Mar 7 at 6:42
  • $\begingroup$ Why not just change the PlotRange to remove the negative y-axis? $\endgroup$ – CA Trevillian Mar 7 at 6:46
  • $\begingroup$ @CATrevillian Is there a better approach than adjusting the PlotRange? $\endgroup$ – A Q Mar 7 at 6:49
  • 1
    $\begingroup$ Probably what kglr just posted :) but it ultimately depends on what you want to do with the plot/data/lines afterwards. $\endgroup$ – CA Trevillian Mar 7 at 6:51
6
$\begingroup$

You can use ConditionalExpression or Piecewise or RegionFunction as follows:

Plot[Evaluate @ Table[ConditionalExpression[K1 xt x - K1 x^2, x <= xt],
    {xt, 0.5, 2.5, 0.2}], {x, 0, 1}]

enter image description here

Plot[Evaluate @ Table[Piecewise[{{K1 xt x - K1 x^2, x <= xt}}, Undefined], 
  {xt, 0.5, 2.5, 0.2}], {x, 0, 1}]
 same picture
Plot[Evaluate @ Table[K1 xt x - K1 x^2, {xt, 0.5, 2.5, 0.2}], 
  {x, 0, 1}, RegionFunction -> (#2 >= 0 &)]

enter image description here

$\endgroup$
2
  • $\begingroup$ You know what I tried with piecewise but my x-axis was getting covered with the curve, I didn't know about the Undefined thing. Also, do you know the reason behind using Evaluate to get the coloured curves and without it, all the curves are of the same color. $\endgroup$ – A Q Mar 7 at 7:02
  • $\begingroup$ @AQ, re the need for Evaluate please see this answer by Mr. Wizard $\endgroup$ – kglr Mar 7 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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