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

Mathematica For looping problem not getting answer

  1. May 24, 2017 #1
    Hi, I need some helps. I try to run the code below but its keep give me the value of intial value.
    Code (Matlab M):

    Q = 1415; \[Mu] = 1000; \[Sigma] = 500;
    double dx;
    double x;
    double fx;
    double fxplusdx;
    double I1;
    double I2;
    I1 = 0;
    I2 = 0;
    For[x = 0, x <= Q - dx, dx += 0.01,
     fx = PDF[NormalDistribution[0, 1], (
       InverseCDF[NormalDistribution[\[Mu], \[Sigma]],
         x] - \[Mu])/\[Sigma]];
     fxplusdx =
      PDF[NormalDistribution[0, 1], (
       InverseCDF[NormalDistribution[\[Mu], \[Sigma]],
         x + dx] - \[Mu])/\[Sigma]];
     I1 = I1 + 0.5*(x*fx + (x + dx)*fxplusdx)*dx;
     I2 = I2 + 0.5*(fx + fxplusdx)*dx]
    I2 [/code/

    Thank you in advance
    Last edited by a moderator: May 24, 2017
  2. jcsd
  3. May 24, 2017 #2


    User Avatar

    Staff: Mentor

    Welcome to the PF.

    Can you please describe in detail what the code is supposed to do?
  4. May 24, 2017 #3
    I want to find the final value of I1 and I2 using the for loop (from x=0 up to x=Q - dx).
    fx and fxplus dx is a PDF of normal distribution. When I run, I get final value I1, and I2 equal to 0.There's no changes from the initial value.
  5. May 24, 2017 #4


    Staff: Mentor

    I would place print statements in the loop for each variable referenced and run it to see how they changed.

    Print x, dx, fx, fxplus ...i1, i2

    It also looks like x is always zero. I don't see anywhere where it's being changed in your loop. Did you mean to use dx instead of x or did you mean to say x+=dx?

    In Java the for loop looks like this

    For( initial value; loop test; increment value)

    So in your case I'm wondering if you meant something like this

    for(x=0; x<Q; x+=dx)

    I also found this Mathematica reference of loops

    Last edited: May 24, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted