Mathematica Mathematica calculates for ages

  • Thread starter Thread starter nikolafmf
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
The discussion revolves around a Mathematica code snippet that is intended to perform calculations efficiently but is running excessively long. The initial code uses symbolic computation, which leads to the generation of large integers that slow down execution. A user suggests modifying the code to print intermediate values, revealing that the variable 'z' becomes non-integer during iterations, compounding the problem. Subsequent modifications to the code improve performance by switching to numerical calculations instead of symbolic ones. The final recommendation emphasizes the importance of using numerical results to maintain efficiency, as it significantly reduces computation time while still achieving the desired outcomes.
nikolafmf
Messages
112
Reaction score
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
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
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...
 
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
It works (although I don't understand the difference).
 
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).
 

Similar threads

Replies
1
Views
2K
Replies
1
Views
533
Replies
1
Views
2K
Replies
1
Views
2K
Replies
19
Views
2K
Replies
1
Views
2K
Replies
5
Views
3K
Back
Top