Optimizing Mathematica Calculations: Faster Plotting with Delayed Expressions

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Plot
Click For Summary

Discussion Overview

The discussion revolves around optimizing calculations in Mathematica, specifically focusing on the performance issues encountered when plotting functions defined with delayed expressions. Participants explore methods to improve plotting speed without resorting to manual evaluations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that plotting using the form Plot[f[x]/.x->y, {y,-1,1}] is significantly slower than evaluating f[x] first and then plotting.
  • Another participant suggests checking the Mathematica forum for similar issues and questions the number of elements in the x array, implying that performance expectations may be too high for Mathematica.
  • A participant questions the necessity of using the substitution method in plotting and proposes a simpler command: Plot[f[y], {y,-1,1}.
  • Another participant encounters an error message stating "y is not a valid variable" when attempting to use the suggested simpler plot command.
  • There is a discussion about different definitions of the function f, with one participant mentioning an alternative definition style that combines delayed and immediate assignment.
  • Participants express a willingness to troubleshoot further by reviewing the notebook in question.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to plotting in Mathematica, with some advocating for simpler commands while others encounter errors that complicate the discussion. No consensus is reached on the optimal method or the cause of the error.

Contextual Notes

Participants mention performance limitations of Mathematica and the potential for different definitions of functions to affect behavior, but these aspects remain unresolved.

member 428835
Hi PF!

I have a series of calculations I'm executing in Mathematica, and all expressions use the delayed := equality. At the end I am trying to plot a function, and for this example let's say it's
Code:
f[x_]:=x
When I plot, I am forced to plot in the form
Code:
Plot[f[x]/.x->y , {y,-1,1}
This method takes FOREVER! However, if I first evaluate f[x], copy-paste it into the plot, this happens much quicker (the evaluation takes some time and obviously the plot is instantaneous, but both are so much faster than the y-plot technique).

Anyone know how I can speed this up without doing the manual inputs?
 
Physics news on Phys.org
Did you check on the Mathematica forum? I’m sure others have experienced this same problem.

How many numbers in x array are we talking about? How slow is it really?

Mathematica isn’t the fastest tool on the block so you may be expecting too much from it performance wise.

I know many folks use Matlab for computations over Mathematica and use Mathematica when symbolic math is needed.

Heres some tips for speeding things up:

http://blog.wolfram.com/2011/12/07/10-tips-for-writing-fast-mathematica-code/
 
Last edited:
  • Like
Likes   Reactions: member 428835
joshmccraney said:
When I plot, I am forced to plot in the form
Code:
Plot[f[x]/.x->y , {y,-1,1}
Why are you doing that? The plot command should simply be
Code:
Plot[f[y], {y,-1,1}]
 
DrClaude said:
Why are you doing that? The plot command should simply be
Code:
Plot[f[y], {y,-1,1}]
When I try this I receive the error message "y is not a valid variable".

Perhaps related, but in the past I've seen other code where f is defined slightly different. For example, if ##f = x^2## I've seen
Code:
f[x_]:=f[x_]=x^2
Any idea why this is done?

Edit: I just read jedishrfu's link, and it explains the above question on the := to the = example. Thanks!
 
Last edited by a moderator:
joshmccraney said:
When I try this I receive the error message "y is not a valid variable".
Are you using y before in the Notebook? Because you shouldn't get such an error.
 
DrClaude said:
Are you using y before in the Notebook? Because you shouldn't get such an error.
Nope, I'm not. Want me to send you the notebook and you can check it out? It's pretty short.
 
joshmccraney said:
Nope, I'm not. Want me to send you the notebook and you can check it out? It's pretty short.
Yes, I can check it out.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K