-2

I need some help. I try to run the code below, but at the end I1 = 0, I2 = 0 (the same as their initial values). I'm not sure which part of my code is wrong.

Q = 1415; μ = 1000; σ = 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[μ, σ], x] - μ)/σ];
  fxplusdx = 
    PDF[NormalDistribution[0, 1], (InverseCDF[NormalDistribution[μ, σ], x + dx] - μ)/σ];
  I1 = I1 + 0.5*(x*fx + (x + dx)*fxplusdx)*dx;
  I2 = I2 + 0.5*(fx + fxplusdx)*dx]
I1
I2
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
fzazul
  • 1
  • 2
    It's hard to know where to start. "double" is not a mathematica command. Your For syntax is wrong (you use x=0 but then try to increment another variable dx, which is itself uninitialized). – bill s May 25 '17 at 01:11
  • Hi, welcome to Mma.SE! To make the most of the site start by taking the [tour] now. Please read the documentation and write your code in actual Wolfram Mathematica Language. After you have done your due diligence, if you still have problems then [edit] your question and share your code. – rhermans May 25 '17 at 13:07
  • Your question will be put on-hold as it's off-topic, i.e it arises from a simple mistake of syntax error and is unlikely to help any future visitors as it's easily found in the documentation. Don't be discouraged by that cleaning-up policy. Your future good questions are welcome. Learn about common pitfalls here. – rhermans May 25 '17 at 13:09

1 Answers1

6

First of all there is nothing like double in Mathematica, you don't need to define the type of variable here.

I don't know what problem are you trying to solve, since you don't describe it, but I at least can point out absolute wrong parts of your code.

There is such a thing in Mathematica, called function, so you can parametrize, instead of hard-coding:

foo[Q_, μ_, σ_] := Module[{xxx},...]

With the body the code should look like:

foo[Q_, μ_, σ_] := Module[
  {dx, x, fx, fxplusdx, I1 = 0, I2 = 0},

  For[x = 0, x <= Q-dx, dx += 0.01,
   fx = PDF[
     NormalDistribution[0, 
      1], (InverseCDF[NormalDistribution[μ, σ], 
         x] - μ)/σ];

   fxplusdx = 
    PDF[NormalDistribution[0, 
      1], (InverseCDF[NormalDistribution[μ, σ], 
         x + dx] - μ)/σ];

   I1 = I1 + 0.5*(x*fx + (x + dx)*fxplusdx)*dx;

   I2 = I2 + 0.5*(fx + fxplusdx)*dx
   ];

  {I1, I2}
  ]

Besides not knowing what do you want to achieve here, I see following problems:

  1. dx has to have initial value, if you want to use it in For[]
  2. You have to be sure that you want to do ~150.000 iterations.
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
SuTron
  • 1,708
  • 1
  • 11
  • 21