Mathematica For looping problem not getting answer

AI Thread Summary
The discussion revolves around a coding issue where the user is trying to compute values for I1 and I2 using a for loop in a programming context. The user reports that both I1 and I2 remain at their initial value of zero after execution. Key points include the need for clarification on the intended functionality of the code, which aims to calculate these values by iterating from x=0 to Q - dx. Participants suggest adding print statements to monitor the changes in variables throughout the loop. A critical observation is that the variable x does not change within the loop, leading to the conclusion that the loop may not be functioning as intended. Suggestions include modifying the loop to increment x properly, potentially using x+=dx instead of the current structure. Additionally, a reference to Mathematica's control structures is provided to assist in correcting the loop logic.
hbz88
Messages
2
Reaction score
0
Hi, I need some helps. I try to run the code below but its keep give me the value of intial value.
Matlab:
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/

Thank you in advance
 
Last edited by a moderator:
Physics news on Phys.org
Welcome to the PF.

Can you please describe in detail what the code is supposed to do?
 
berkeman said:
Welcome to the PF.

Can you please describe in detail what the code is supposed to do?
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.
 
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:
Back
Top