For looping problem not getting answer

  • Context: Mathematica 
  • Thread starter Thread starter hbz88
  • Start date Start date
  • Tags Tags
    For loops Mathematica
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
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:
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: