# Mathematica For looping problem not getting answer

Tags:
1. May 24, 2017

### hbz88

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]
I1
I2 [/code/

Last edited by a moderator: May 24, 2017
2. May 24, 2017

### Staff: Mentor

Welcome to the PF.

Can you please describe in detail what the code is supposed to do?

3. May 24, 2017

### hbz88

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.

4. May 24, 2017

### 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

https://reference.wolfram.com/language/tutorial/LoopsAndControlStructures.html

Last edited: May 24, 2017