For looping problem not getting answer

  • Context: Mathematica 
  • Thread starter Thread starter hbz88
  • Start date Start date
  • Tags Tags
    For loops Mathematica
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to a for loop in a programming context, specifically focusing on the calculation of two variables, I1 and I2, which are intended to represent integrals based on the probability density function (PDF) of a normal distribution. The problem arises when the participant finds that the final values of I1 and I2 remain unchanged from their initial values.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The participant describes an issue where the values of I1 and I2 do not change during the execution of the for loop.
  • Some participants suggest adding print statements to monitor the changes of variables within the loop.
  • There is a suggestion that the variable x may not be updating correctly, with a proposal to check if dx should be used instead of x or if x should be incremented by dx.
  • A comparison is made to Java's for loop structure, implying that the current loop structure may not be functioning as intended.
  • A reference to Mathematica's documentation on loops is provided for further clarification.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the cause of the issue. There are differing views on how the for loop is structured and whether the variable updates are being handled correctly.

Contextual Notes

There is uncertainty regarding the correct implementation of the for loop and the intended behavior of the variables involved. The discussion does not resolve these uncertainties.

Who May Find This Useful

Individuals interested in programming loops, particularly in Mathematica, and those troubleshooting similar coding issues may find this discussion relevant.

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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 29 ·
Replies
29
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K