Optimizing Mathematica Calculations: Faster Plotting with Delayed Expressions

  • Mathematica
  • Thread starter member 428835
  • Start date
  • Tags
    Plot
It might be easier for me to spot the issue if I can see the notebook.Yes, I can check it out. It might be easier for me to spot the issue if I can see the notebook.In summary, the conversation discusses a problem with the speed of plotting a function in Mathematica using the delayed := equality. The suggested solution is to first evaluate the function and then copy-paste it into the plot, which is much faster. The conversation also mentions tips for speeding up computations in Mathematica and a discussion on the use of := versus = in function definitions. It ends with a request to see the notebook for further troubleshooting.
  • #1
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
  • #2
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
  • #3
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}]
 
  • #4
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:
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 

FAQ: Optimizing Mathematica Calculations: Faster Plotting with Delayed Expressions

How can I speed up my plotting calculations in Mathematica?

One way to speed up plotting calculations in Mathematica is by using delayed expressions. This allows the calculations to be deferred until they are needed, reducing the overall processing time.

What are delayed expressions in Mathematica?

Delayed expressions are a feature in Mathematica that allows certain calculations to be postponed until they are explicitly evaluated. This can improve performance by reducing the amount of unnecessary processing.

How do I use delayed expressions in my Mathematica code?

To use delayed expressions in your code, you can wrap an expression in the function Delayed[expr] or use the shorthand notation expr//.Delayed. This will delay the evaluation of the expression until it is explicitly called.

What types of calculations are best suited for delayed expressions in Mathematica?

Delayed expressions are most useful for calculations that involve large datasets or complex functions. This includes tasks such as plotting, numerical integration, and solving differential equations.

Can I combine delayed expressions with other optimization techniques in Mathematica?

Yes, you can combine delayed expressions with other optimization techniques in Mathematica, such as parallelization and compilation. This can further improve the performance of your calculations and reduce the overall processing time.

Similar threads

Replies
1
Views
1K
Replies
1
Views
808
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
917
Replies
3
Views
1K
Replies
1
Views
1K
Back
Top