Mathematica Optimizing Mathematica Calculations: Faster Plotting with Delayed Expressions

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Plot
AI Thread Summary
Using delayed expressions in Mathematica can significantly slow down plotting functions, as demonstrated by the user’s experience with plotting f[x] using the substitution method. The recommended approach is to directly plot using Plot[f[y], {y, -1, 1}], but the user encounters an error stating "y is not a valid variable." This issue may stem from prior definitions in the notebook that conflict with the variable y. Suggestions include reviewing the code for variable conflicts and considering the use of alternative methods for optimization. Overall, addressing variable definitions and using direct plotting commands can enhance performance in Mathematica.
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 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
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
2
Views
3K
Back
Top