Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica calculates for ages

Tags:
  1. May 4, 2014 #1
    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?
     
  2. jcsd
  3. May 4, 2014 #2

    phyzguy

    User Avatar
    Science Advisor

    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.
     
  4. May 4, 2014 #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...
     
  5. May 4, 2014 #4

    phyzguy

    User Avatar
    Science Advisor

    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.
     
  6. May 5, 2014 #5
    It works (although I don't understand the difference).
     
  7. May 5, 2014 #6

    phyzguy

    User Avatar
    Science Advisor

    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).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook