Matlab vs Mathematica for computing derivatives

In summary, the conversation is discussing a calculation involving taking the derivative of a term that is millions of terms long. The speaker is using a supercomputer with Mathematica to perform the calculation, but it is taking too long. They inquire whether Matlab has a faster algorithm for computing derivatives analytically. More information is needed, such as the structure of the terms and the number of items in each term, in order to provide a more specific answer.
  • #1
xdrgnh
417
0
I'm trying to do a calculation which involves taking the derivative of a term that is millions of terms long. I'm using a supercomputer right now with Mathematica to do it but it's taking to long. Does Matlab have a faster algorithm for computing derivatives analytically?
 
Physics news on Phys.org
  • #2
"taking the derivative of a term that is millions of terms long" doesn't provide much information. It isn't clear whether each of your terms have a few or a few thousand items making up the term. And it isn't even clear whether you are using the more or less formal definition of "term", which is strictly a sum of individual items, as opposed to "factor" which is strictly a product of individual items.

If you could paste the first 50 lines or so of your millions of terms then someone might be able to give more information. Or a description that "each of my terms is approximately this structure" might help. Even a LeafCount[yourexpression] might give you and others some information about how complicated this is. Sometimes the vast majority of the items can be almost instantly differentiated, but you have a few or a few thousand which might each require hours or many hours. A million small integers is feasible to handle with Mathematica. A million really complicated things to differentiate may not.
 
  • #3
(((4 P - 2 p Cos[q - Q]) (-((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + ((2 p - 2 P Cos[q - Q]) ((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
2 Cos[q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + (
2 (-((4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 ((4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 P Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + (
2 Cos[q -
Q] (-((4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)) +
1/(2 + 2 Sin[q - Q]^2) (2 p -
2 P Cos[
q - Q]) (((4 P - 2 p Cos[q - Q]) ((
64 Cos[q - Q]^3 Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3 - (
8 Cos[q - Q]^3)/(2 + 2 Sin[q - Q]^2)^2 + (
24 Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
2 Cos[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + ((2 p -
2 P Cos[q - Q]) (-((
64 Cos[q - Q]^3 Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3) + (
8 Cos[q - Q]^3)/(2 + 2 Sin[q - Q]^2)^2 - (
24 Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
2 Cos[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
2 Cos[q -
Q] (-((32 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]) Sin[
q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3) + (
4 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 + (
16 p Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
4 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 - (2 p Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
2 ((32 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3 - (
4 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 - (
16 p Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
4 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 + (2 p Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 (-((32 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3) + (
4 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 + (
16 P Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
4 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 - (2 P Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
2 Cos[q - Q] ((
32 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3 - (
4 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 - (
16 P Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
4 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 + (2 P Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[
q - Q] (-((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 +
2 Sin[q - Q]^2)^2 + (
2 p Sin[q -
Q] (-((8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[
q - Q] ((8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 +
2 Sin[q - Q]^2)^2 + (
2 P Sin[q - Q] ((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
8 Cos[q - Q]^2 Sin[
q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
8 Cos[q - Q] Sin[
q - Q] (-((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 + (
16 Cos[q - Q] Sin[
q - Q] ((
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 - (
8 Cos[q - Q]^2 Sin[
q - Q] (-((
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 - (
2 Sin[q -
Q] (-((4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)) +
1/(2 + 2 Sin[q - Q]^2) (4 P -
2 p Cos[q - Q]) (((2 p - 2 P Cos[q - Q]) ((
64 Cos[q - Q]^3 Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3 - (
8 Cos[q - Q]^3)/(2 + 2 Sin[q - Q]^2)^2 + (
24 Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
2 Cos[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + ((4 P -
2 p Cos[q - Q]) (-((
64 Cos[q - Q]^3 Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3) + (
8 Cos[q - Q]^3)/(2 + 2 Sin[q - Q]^2)^2 - (
24 Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
2 Cos[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
2 (-((32 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3) + (
4 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 + (
16 p Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
4 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 - (2 p Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
2 Cos[q - Q] ((
32 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3 - (
4 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 - (
16 p Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
4 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 + (2 p Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
2 Cos[q -
Q] (-((32 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]) Sin[
q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3) + (
4 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 + (
16 P Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
4 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 - (2 P Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 ((32 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3 - (
4 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 - (
16 P Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
4 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 + (2 P Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[
q - Q] (-((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 +
2 Sin[q - Q]^2)^2 - (
2 p Sin[q -
Q] (-((8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[
q - Q] ((8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 +
2 Sin[q - Q]^2)^2 - (
2 P Sin[q - Q] ((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
8 Cos[q - Q]^2 Sin[
q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 - (
2 Sin[q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
8 Cos[q - Q] Sin[
q - Q] (-((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 - (
16 Cos[q - Q] Sin[
q - Q] ((
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 + (
8 Cos[q - Q]^2 Sin[
q - Q] (-((
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q -
Q] (-((4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)) - (-((
32 Cos[q - Q] Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^3) - (
32 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^3) ((
4 Cos[q - Q] (p^2 + 2 P^2 - 2 p P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p P Sin[q - Q])/(2 + 2 Sin[q - Q]^2) +
9.8 Sin[Q]) -
 
  • #4
Ah. That was extremely helpful.

You have lots of denominators and lots of transcendental functions. I can see why doing a derivative of millions of those could take a long time.

You didn't say whether it was just taking a very long time and you were not sure whether it was making progress or how long it might take OR whether you had already completed this and just needed it to do it repeatedly and have it finish in minutes.

If you don't know how much progress it is making then you might try to apply this example:

Code:
In[1]:= n = 0;
expr = 1 + q + q^3;
Print["Length is ", Length[expr]];
Map[(Print[n++]; D[#, q]) &, expr]

During evaluation of In[5]:= Length is 3

During evaluation of In[5]:= 0

During evaluation of In[5]:= 1

During evaluation of In[5]:= 2

Out[4]= 1 + 3 q^2

That should show you how much progress is being made. If it is printing out digits rapidly then you know it is busy working on it. If it the numbers print at a fairly uniform rate then you might even be able to estimate the total calculation time. If there is an extremely long pause after printing one number then that might let you know that one of your "terms" is much much harder.

The printing process is going to slow down the calculation considerably, so your result without the Map and Print should be somewhat faster. But this could at least give you some estimate.
 
  • #5


Both Matlab and Mathematica are powerful tools for scientific computing and have robust capabilities for computing derivatives. However, the choice between the two ultimately depends on the specific problem at hand and the user's familiarity with each software.

Matlab is known for its efficient numerical computations and has a strong focus on linear algebra and matrix operations. It also has several built-in functions for computing derivatives, such as the "diff" function, which can handle large datasets efficiently. However, Matlab may not be as versatile as Mathematica when it comes to symbolic computations and may not have the same level of precision.

On the other hand, Mathematica excels in symbolic computations and has a vast library of built-in functions for mathematical operations, including derivatives. It also has advanced algorithms for handling complex mathematical expressions, making it a popular choice for theoretical calculations. However, these symbolic computations can be computationally intensive and may take longer to run, especially for large datasets.

In terms of the specific problem of computing derivatives for a term with millions of terms, both Matlab and Mathematica can handle it, but the efficiency may depend on the specific expressions and algorithms used. It may be worth exploring the use of parallel computing techniques in both software to speed up the process.

In conclusion, both Matlab and Mathematica have their strengths and limitations in computing derivatives, and the choice between the two ultimately depends on the specific needs and preferences of the user. It may be beneficial to try both and see which one performs better for the specific problem at hand.
 

What are the main differences between Matlab and Mathematica for computing derivatives?

Matlab and Mathematica are two popular software programs used for computing derivatives. While both programs have similar capabilities, there are some key differences between them:

  • Language: Matlab uses its own programming language while Mathematica uses the Wolfram Language. This means that the syntax and commands used for computing derivatives will be different in each program.
  • User interface: Matlab has a more traditional user interface with menus and toolbars, while Mathematica has a notebook interface that allows for more interactive and dynamic calculations.
  • Availability: Matlab is a proprietary software that requires a license to use, while Mathematica offers a free version called Wolfram|Alpha for basic computations and a paid version for more advanced tasks.
  • Functionality: While both programs can compute derivatives, Mathematica has a wider range of mathematical functions and capabilities, making it more suitable for complex mathematical calculations.
  • Community support: Matlab has a larger user base and a strong community for support and troubleshooting, while Mathematica has a smaller but dedicated community.

Which program is better for computing derivatives: Matlab or Mathematica?

There is no clear answer to this question as both programs have their own strengths and weaknesses. It ultimately depends on the specific needs and preferences of the user. Some may find Matlab's simpler interface and larger user base more appealing, while others may prefer Mathematica's advanced capabilities and dynamic interface.

Can both Matlab and Mathematica handle symbolic and numerical derivatives?

Yes, both programs are capable of computing both symbolic and numerical derivatives. However, the syntax and commands used for each type of derivative may differ between the programs. Matlab has a built-in function called diff for computing both types of derivatives, while Mathematica uses the D function for symbolic derivatives and the N function for numerical derivatives.

Which program is more efficient for computing derivatives?

This question is difficult to answer definitively as it depends on the specific calculation being performed. In general, Mathematica has a reputation for being more efficient and faster for complex mathematical computations, while Matlab may be more efficient for simpler calculations. It is best to test both programs for the specific task at hand to determine which is more efficient.

Can I use both Matlab and Mathematica for computing derivatives?

Yes, you can use both programs for computing derivatives. In fact, some researchers and scientists prefer to use both programs for different types of calculations. For example, Matlab may be more efficient for numerical derivatives, while Mathematica may be more suitable for symbolic derivatives. It is also possible to import data and results from one program to the other for further analysis and visualization.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
684
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
21
Views
5K
Back
Top