Plotting 2 functions & economizing on calc of intermediate result

In summary, the error message is caused by mismatches between the ranges specified for IntegerPart and Range. To get rid of the error, the programmer would need to make sure that all ranges used in the code have appropriate bounds.
  • #1
Swamp Thing
Insights Author
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!

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:
Physics news on Phys.org
  • #2
If K is a function of x then you can make K into a function that remembers its values as follows:

K[x_]:=K[x]= (* some expression involving x *)

Every time you evaluate K it will remember the result and it will simply use that result instead of recalculating it.
 
  • Informative
Likes Swamp Thing
  • #3
Brilliant!
Now the code is as simple as this:

Code:
fnA[x_]:=Total[Range[IntegerPart[x/20]]]
fnB[x_]:=.1*x

n=0 (*Evaluation counter for K *)
K[x_]:=K[x]= (n=n+1;0.2*x) (* value common to A, B *)

Plot[{ K[x]+fnA[x] , K[x]+fnB[x] } , {x,100,200}]

Style[Row[{"Evalutated 'K' ",n," times"}],20]

1609390845442.png

1609390952277.png


Thanks again!
 
  • Like
Likes Dale

1. How do I plot two functions on the same graph?

To plot two functions on the same graph, you can use a graphing calculator or graphing software on a computer. First, enter both functions into the calculator or software. Then, select the option to graph multiple functions. The resulting graph will display both functions on the same coordinate plane.

2. How can I economize on the calculation of intermediate results?

To economize on the calculation of intermediate results, you can use the properties of algebra to simplify equations before solving them. This can include combining like terms, factoring, or using the distributive property. Additionally, using a graphing calculator or software can also help save time by quickly displaying the results of multiple calculations.

3. Can I plot more than two functions on the same graph?

Yes, you can plot multiple functions on the same graph using a graphing calculator or software. Most graphing calculators allow you to graph up to 10 functions at once, while graphing software on a computer may allow for even more.

4. Is it necessary to plot both functions on the same graph?

No, it is not necessary to plot both functions on the same graph. You can plot each function on its own graph if desired. However, plotting both functions on the same graph can be useful for comparing the two functions and identifying any points of intersection.

5. How can I use the graph to find the solution to a system of equations?

To use the graph to find the solution to a system of equations, plot both equations on the same graph. The solution to the system of equations will be the point of intersection between the two graphs. You can use the cursor on a graphing calculator or the zoom and trace functions on graphing software to find the coordinates of the point of intersection.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
941
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
6K
Back
Top