Mathematica calculates for ages

  • Mathematica
  • Thread starter nikolafmf
  • Start date
  • Tags
    Mathematica
In summary, the code is taking a long time to run because it is doing the calculations symbolically. If you want it to run quickly, you should change the last statement so that it calculates the values numerically.
  • #1
nikolafmf
114
0
Hello,

I told Mathematica to do a few calculations as follows:

a = 0;
b = 1;
n = 4;
For[i = 0, i < n, i++,
p = 2*x*b - 2*i*a;
a = b;
b = p]
c = CoefficientList[p, x];
l = Length[c];
m = 0;
For[i = 1, i < l + 1, i++,
r = Abs[Part[c, i]]/Abs[Part[c, l]];
If[r > m, m = r, m]]
lowerbound = -m - 1;
upperbound = m + 1;
iter = 5;
f = Function[k, p /. x -> k];
For[z = lowerbound, z < upperbound, z++,
For[i = 0, i < iter, i++,
t = z - f[z]/(D[f[k], k] /. k -> z);
z = t]
Print[N[t]]]

I think this should be executed in a second. But no, Mathematica would run for minutes and won't stop. Can somebody execute this code too, to check if the problem is with my computer or with the code? If the problem is with the code, what kind of problem is it?
 
Physics news on Phys.org
  • #2
Mathematica is doing what you are telling it to do. Try modifying the last statement to read:

For[z = lowerbound, z < upperbound, z++,
For[i = 0, i < iter, i++,
t = z - f[z]/(D[f[k], k] /. k -> z);
Print; Print[z]; Print[t]; z = t]
Print[N[t]]]

Then run it and look at the output. Is this really what you want the code to do? You are modifying z inside the i loop, so each time through the z loop it adds 1 to z, until z > upperbound. But z is no longer an integer after the first time through the i loop. Since Mathematica is symbolic code, the fractions z and t are expanding until they are the ratios of huge integers. This is why the code is taking so long to run.
 
  • Like
Likes 1 person
  • #3
Ok, I have modified that part as follows:

For[z = lowerbound, z < upperbound, z++,
g = z;
For[i = 0, i < iter, i++,
t = g - f[g]/(D[f[k], k] /. k -> g);
g = t]
Print[N[t]]]

That seems better, since it calculates 5 or 10 iterations in a reasonable time. But still, if I tell Mathematica to calculate 15 iterations in i loop, it takes too much time...
 
  • #4
It's taking so much time because you are still doing the calculation symbolically. I suspect that you don't need to do that - doing it numerically will probably be sufficient. If you change the last statement so that it reads:

For[z = lowerbound, z < upperbound, z++,
g = z;
For[i = 0, i < iter, i++,
t = g - f[g]/(D[f[k], k] /. k -> g);
g = N[t]]
Print[N[t]]]

then it will do 15 iterations basically instantaneously. It's important to pay attention to what Mathematica is actually doing.
 
  • Like
Likes 1 person
  • #5
It works (although I don't understand the difference).
 
  • #6
nikolafmf said:
It works (although I don't understand the difference).

Try printing out g for each step in the case where g = t vs the case where g = N[t]. In the first case g will be ratios of two integers which are getting larger and larger (like 123456789987654321/987654321123456789, for example). The computer is doing the calculations, keeping all of the digits of those integers. In the second case, the computer is doing the division of those two large integers and only keeping about 10-20 digits of the numerical result, so g is a single floating point numbers (like 1.23456789, for example).
 

What is Mathematica?

Mathematica is a software program developed by Wolfram Research that is used for mathematical and scientific computing. It is commonly used by scientists, engineers, and mathematicians for data analysis, modeling, and visualization.

What makes Mathematica stand out from other software programs?

Mathematica is known for its powerful capabilities in symbolic and numerical computation, as well as its ability to handle complex mathematical equations and algorithms. It also offers a user-friendly interface and extensive documentation, making it accessible for users of all levels of expertise.

How does Mathematica handle large datasets?

Mathematica has efficient algorithms and data structures that allow it to handle large datasets without compromising on speed or accuracy. It also offers various functions and tools for data manipulation and visualization.

Can Mathematica be used for data analysis and machine learning?

Yes, Mathematica has a wide range of built-in functions and packages for data analysis, pattern recognition, and machine learning. It also supports integration with other programming languages such as Python and R, making it a versatile tool for data scientists.

Is Mathematica suitable for both research and education purposes?

Yes, Mathematica can be used for both research and education purposes. Its powerful computational capabilities make it a valuable tool for scientific research, while its user-friendly interface and comprehensive documentation make it a great tool for teaching and learning mathematics and other sciences.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
267
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
227
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
140
Replies
3
Views
1K
Back
Top