Message Boards Message Boards

0
|
4249 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Where I got it wrong with a weighted curve fit? Thanks

Posted 11 years ago
 In[1]:= weightedcount[n_] :=
  Module[{ x, y, z, w, weightedcount = 0},
   Do[
    x = RandomReal[{-5, 5}];
    y = RandomReal[{-5, 5}];
    z = RandomReal[{-5, 5}];
    w = RandomReal[{-5, 5}];
    If[ 0 <= x y z w <= 1,
     weightedcount = weightedcount + Exp[x]  Sin[x y z w];
    ],
   {n}];
  weightedcount/n
  ]
area = 10000

Out[2]= 10000

In[3]:= area*weightedcount[10000]

Out[3]= 1825.05

In[4]:=

In[5]:=

In[6]:=
weightedcount[n_] :=
Module[{x, y, z, w, weightedcount = 0, weightedcount2 = 0},
  Do[
   x = RandomReal[{-5, 5}];
    y = RandomReal[{-5, 5}];
    z = RandomReal[{-5, 5}];
    w = RandomReal[{-5, 5}];
    If[0 <= xyzw <= 1,
     Weightedcount = Weightedcount + Exp[x] Sin[x y z w];
     Weightedcount = Weightedcount + Exp[x]^2 [Sin[x y z w]]^2;
     ]?
    {n}];
  {weightedcount/n, weightedcount2/n}
  ]
weightedcount[10000]


Out[7]= {0, 0}
POSTED BY: michelle yrw
5 Replies
Posted 11 years ago
 In[1]:= weightedcount1[n_] := Module[{x, y, z, w, Weightedcount = 0},
    Do[x = RandomReal[{-5, 5}];
     y = RandomReal[{-5, 5}];
     z = RandomReal[{-5, 5}];
     w = RandomReal[{-5, 5}];
     If[0 <= x*y*z*w <= 1, Weightedcount = Weightedcount + Exp[x] Sin[x*y*z*w]],
     {n}];
    Weightedcount/n];
 area = 10000;
area*weightedcount1[10000]

Out[3]= 1511.36

In[4]:= weightedcount2[n_] := Module[{x, y, z, w, Weightedcount = 0, Weightedcount2 = 0},
  Do[x = RandomReal[{-5, 5}];
   y = RandomReal[{-5, 5}];
   z = RandomReal[{-5, 5}];
   w = RandomReal[{-5, 5}];
   If[0 <= x*y*z*w <= 1,
    Weightedcount = Weightedcount + Exp[x] Sin[x*y*z*w];
    Weightedcount2 = Weightedcount2 + Exp[x]^2 Sin[x*y*z*w]^2],
   {n}];
  {Weightedcount/n, Weightedcount2/n}];
weightedcount2[10000]

Out[5]= {0.1487, 4.76572}
POSTED BY: Bill Simpson
Posted 11 years ago
Mathematica is very demanding that every character be correct. It is more than the multiply sign. In your code above you have
 weightedcount = 0, weightedcount2 = 0
 
 and
 
 Weightedcount = Weightedcount + Exp[x] Sin[x y z w];
 
 Weightedcount = Weightedcount + Exp[x]^2 [Sin[x y z w]]^2
 
 and

{weightedcount/n, weightedcount2/n}
Notice that Weightedcount and weightedcount and weightedcount2 are completely different for Mathematica and it gives you results you do not expect.

In a previous post you have Exp Sin[ x y z w ] and Mathematica does not understand what you mean. 

Such small errors would be understood by a bright graduate student who would know what the problem should be. But Mathematica is not a bright graduate student and will give you incorrect answers or error messages you do not understand or even nothing at all.
In[1]:= f[x_,y_,z_,w_]:=Piecewise[{{Exp[x]Sin[x*y*z*q],x*y*z*w<=1}},0];
NIntegrate[f[x,y,z,w],{w,-5,5},{z,{-5,5},{y,-5,5},{x,-5,5},MaxPoints->10^7,Method->"MonteCarlo"]

NIntegrate::maxp: The integral failed to converge after 10000100 integrand evaluations. NIntegrate
obtained -1949.47 and 52.76 for the integral and error estimates.

Out[2]= -1949.27
POSTED BY: Bill Simpson
Posted 11 years ago
First an error. If you look at the last code box in my posting you see that two errors have appeared even though I just pasted the code from a notebook.

Sin[ x*y*z*w ] somehow became Sin[ x*y*z*q ] and {z,-5,5} became {z,{-5,5}. Usually I would dismiss things like this as operator error, but it seems extremely unlikely that I would have made two errors like that when doing a simple select, copy, paste. Unfortunately it is almost certainly impossible for me to track down and pinpoint the source of this kind of error.

Next, to try to answer your question about the number of points for 95% confidence in 0.01. My first impression would be this would be impossible to answer. I assumed from the size of the error estimate that the calculation would need to run for many years to get to an error of 0.01. But I tried the following.
 In[1]:= f[x_, y_, z_, w_] := Piecewise[{{Exp[x] Sin[x*y*z*w], x*y*z*w <= 1}}, 0];
 NIntegrate[f[x, y, z, w], {w, -5, 5}, {z, -5, 5}, {y, -5, 5}, {x, -5, 5}, MaxPoints -> 10^1, Method -> "MonteCarlo"]
 
 During evaluation of In[1]:= NIntegrate::maxp: The integral failed to converge after 100 integrand evaluations.
 NIntegrate obtained 33747.63448633355` and 18817.447915103512` for the integral and error estimates. >>
 
 Out[2]= 33747.6
 
 In[3]:= NIntegrate[f[x, y, z, w], {w, -5, 5}, {z, -5, 5}, {y, -5, 5}, {x, -5, 5},  MaxPoints -> 10^2, Method -> "MonteCarlo"]

During evaluation of In[3]:= NIntegrate::maxp: The integral failed to converge after 200 integrand evaluations.
NIntegrate obtained 3840.5470856443385` and 13915.684872403956` for the integral and error estimates. >>

Out[3]= 3840.55

In[4]:= NIntegrate[f[x, y, z, w], {w, -5, 5}, {z, -5, 5}, {y, -5, 5}, {x, -5, 5}, MaxPoints -> 10^3, Method -> "MonteCarlo"]

During evaluation of In[4]:= NIntegrate::maxp: The integral failed to converge after 1100 integrand evaluations.
NIntegrate obtained -892.994 and 4556.737738794749` for the integral and error estimates. >>

Out[4]= -892.994

In[5]:= NIntegrate[f[x, y, z, w], {w, -5, 5}, {z, -5, 5}, {y, -5, 5}, {x, -5, 5}, MaxPoints -> 10^4, Method -> "MonteCarlo"]

During evaluation of In[5]:= NIntegrate::maxp: The integral failed to converge after 10100 integrand evaluations.
NIntegrate obtained -2101.01 and 1601.1200343423657` for the integral and error estimates. >>

Out[5]= -2101.01

In[6]:= NIntegrate[f[x, y, z, w], {w, -5, 5}, {z, -5, 5}, {y, -5, 5}, {x, -5, 5}, MaxPoints -> 10^5, Method -> "MonteCarlo"]

During evaluation of In[6]:= NIntegrate::maxp: The integral failed to converge after 100100 integrand evaluations.
NIntegrate obtained -2001.8 and 523.7733991685055` for the integral and error estimates. >>

Out[6]= -2001.8

In[7]:= NIntegrate[f[x, y, z, w], {w, -5, 5}, {z, -5, 5}, {y, -5, 5}, {x, -5, 5}, MaxPoints -> 10^6, Method -> "MonteCarlo"]

During evaluation of In[7]:= NIntegrate::maxp: The integral failed to converge after 1000100 integrand evaluations.
NIntegrate obtained -2035.14 and 166.86159178863022` for the integral and error estimates. >>

Out[7]= -2035.14

In[8]:= NIntegrate[f[x, y, z, w], {w, -5, 5}, {z, -5, 5}, {y, -5, 5}, {x, -5, 5}, MaxPoints -> 10^7, Method -> "MonteCarlo"]

During evaluation of In[8]:= NIntegrate::maxp: The integral failed to converge after 10000100 integrand evaluations.
NIntegrate obtained -1898.9 and 52.75810129576391` for the integral and error estimates. >>

Out[8]= -1898.9

In[9]:= NIntegrate[f[x, y, z, w], {w, -5, 5}, {z, -5, 5}, {y, -5, 5}, {x, -5, 5}, MaxPoints -> 10^8, Method -> "MonteCarlo"]

Out[9]= -1916.95

After some initial variation it appeared that the error was decreasing by a factor of three when increasing the number of points by a factor of ten. I was about to calculate how many powers of ten would be needed to decrease the error to 0.01 when the last result appeared.

I do not know that the possible error in the Monte Carlo NIntegrate is precisely documented, but if you believe this calculation is sufficient then I think you are done and the calculation does not take as much time as I feared it would.

Please check all this very carefully for any more errors that have appeared after a single simple copy and paste.
POSTED BY: Bill Simpson
Posted 11 years ago
Yes? Thanks.
I should add the multiply sign.

Did you try the Monte Carlo Problem, in my later discussion? that is very challenging quesiton.
POSTED BY: michelle yrw
Posted 11 years ago
Question for the above mathematica

How many points n are needed to be 95% sure that an estimate is accurate to 0.01.
POSTED BY: michelle yrw
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract