For looping problem not getting answer

In summary: It uses a different syntax for the for loop thoughIn summary, the code aims to find the final values of I1 and I2 using a for loop. It uses the PDF of a normal distribution to calculate fx and fxplusdx. However, there may be an error in the code as x is never changed in the loop and the final values of I1 and I2 remain at 0. It is suggested to use print statements to track the values of variables and check if the intended increment for x is being used. Additionally, a different syntax for the for loop may be needed as suggested by Mathematica's reference.
  • #1
hbz88
2
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
  • #2
Welcome to the PF.

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

What is a for loop?

A for loop is a programming construct used to repeat a set of instructions for a specified number of times or until a certain condition is met. It typically consists of an initialization, a condition, an iteration statement, and a loop body.

Why is my for loop not working?

There could be several reasons why your for loop is not working. Some common causes include incorrect syntax, logic errors, and incorrect initialization or condition. It is important to carefully check your code and troubleshoot any errors before assuming the problem lies with the for loop itself.

How do I break out of a for loop?

You can use the break statement within a for loop to prematurely terminate the loop based on a specific condition. This is useful when you want to stop the loop before it has completed all iterations.

Can I use a for loop to iterate over arrays?

Yes, for loops are commonly used to iterate over arrays. You can use the loop index to access each element in the array and perform operations on it. Alternatively, you can also use the for...of loop syntax to iterate over the elements of an array directly.

What is the difference between a for loop and a while loop?

A for loop is typically used when you know the exact number of iterations you want to perform, while a while loop is used when you want to repeat a set of instructions until a specific condition is met. Additionally, a for loop has a built-in iteration control, while a while loop requires you to manually update the loop condition within the loop body.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
230
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
275
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Classical Physics
Replies
0
Views
161
  • Special and General Relativity
Replies
15
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Back
Top