- #1
- 908
- 572
- TL;DR Summary
- It's complicated :)
I am trying to plot two functions a(t) and b(t) that both use a common intermediate result K. In my actual code K would be a slow-ish calculation. To reuse the K across a and b, I am putting them into a single module that provides an array {a, b} to the Plot[] function. (BTW, the Evaluate[] is needed because without it, both plots come out the same color).
So far so good. But a(t) involves discretizing t to get N, and then summing over N terms, as in Total[Range[IntegerPart[t/20]]].
Now, although the plot comes out OK in this example, it displays a little error message : "Range specification in Range[IntegerPart[t/20]] does not have appropriate bounds". I am worried that this error will break my actual plot, which would be more complicated than this minimal demo, so it would be nice to get rid of the error.
And BTW, if I plot a and b separately and do a Show[Pa, Pb] then it works fine with no error -- at the cost of computing K twice as many times. Same if I Plot[{fn_a[t] , fn_ b[t],{t,0,10 }] in the usual way.
Thanks!
So far so good. But a(t) involves discretizing t to get N, and then summing over N terms, as in Total[Range[IntegerPart[t/20]]].
Now, although the plot comes out OK in this example, it displays a little error message : "Range specification in Range[IntegerPart[t/20]] does not have appropriate bounds". I am worried that this error will break my actual plot, which would be more complicated than this minimal demo, so it would be nice to get rid of the error.
And BTW, if I plot a and b separately and do a Show[Pa, Pb] then it works fine with no error -- at the cost of computing K twice as many times. Same if I Plot[{fn_a[t] , fn_ b[t],{t,0,10 }] in the usual way.
Thanks!
Code:
Plot[ Evaluate[
Module[{K,a,b},
(* "K" is an intermediate result needed for "a" and "b" *)
K=t; (* we don't want to calc this more than once per t *)
(* "a" is a sum over N terms; N depends on "t" *)
a= K * Total[Range[IntegerPart[t/20]]];
(* "b" is just a simple function of "t" *)
b= K * t/10+5;
(* We want to plot a and b as a fn of t *)
{a,b}
]
] ,
{t,100,200},Exclusions->None
]
Last edited: