Plotting 2 functions & economizing on calc of intermediate result

  • Context: Mathematica 
  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Functions Plotting
Click For Summary
SUMMARY

This discussion focuses on optimizing the plotting of two functions, a(t) and b(t), in Mathematica by reusing a common intermediate result K. The user implements a Module to calculate K only once per t, which enhances efficiency. However, an error message regarding range specification arises during the plotting process, prompting concerns about its impact on more complex plots. The solution involves defining K as a function that retains its computed values, thereby eliminating redundant calculations and simplifying the code.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of function plotting in Mathematica
  • Knowledge of Modules and scoping in Mathematica
  • Basic concepts of optimization in programming
NEXT STEPS
  • Learn about Mathematica's Module function for variable scoping
  • Explore the use of caching techniques in Mathematica functions
  • Investigate error handling in Mathematica plotting functions
  • Study advanced plotting techniques in Mathematica, including Exclusions and Show
USEFUL FOR

Mathematica users, data scientists, and programmers looking to optimize function plotting and reduce computational overhead in their visualizations.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
798
TL;DR
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
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   Reactions: Swamp Thing
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   Reactions: Dale

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
9K
Replies
0
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
12K