Optimizing Mathematica Calculations: Faster Plotting with Delayed Expressions

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Plot
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
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?
 
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.