Message Boards Message Boards

0
|
4127 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Can you check why I couldn't get the answer I want, what is wrong?

Posted 11 years ago
I have a question and I use the Mathematica to run it,

 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[ x y z w <= 1,
     weightedcount = weightedcount + Exp[x] Sin[xyzw];
    ],
   {n}];
  weightedcount/n
  ]
area[10000]

area*weightedcount[100000]

But the answer gives me
7.8517 area Sin[xyzw]

Where did I get it wrong?

Thanks
POSTED BY: michelle yrw
3 Replies
You need to put spaces between x y z and w inside Sin. xyzw is interpreted as a variable by that name.
POSTED BY: Frank Kampas
Posted 11 years ago
I did, but why it take so long to run it. It is still running, very weired. Can anyone explain?

then I wrote the following to check it up, but still it takes so long to run, even now, still couldn't get any result, why. Can anyone review it?

Thanks

f[x_, y_, z_, w_] := If[x y z w <= 1, Exp Sin[x y z w ], 0]
NIntegrate[f[x, y, z, w], {w, -5, 5}, {z, -5, 5}, {y, -5, 5}, {x, -5, 5}]

Many thanks
POSTED BY: michelle yrw
Posted 11 years ago
I'm worried that you have used weightedcount as the name of a function and the name of a variable inside that function.

Slight edit
 In[1]:= weightedcount[n_] := Module[{x, y, z, w, wc = 0},
    Do[{x, y, z, w} = RandomReal[{-5, 5}, 4];
     If[x*y*z*w <= 1, wc = wc + Exp[x] Sin[x*y*z*w]],
     {n}
     ];
    wc/n];
 Timing[weightedcount[100000]]
 
 Out[2]= {0.608404, -0.244891}
It now completes 100000 iterations in 0.6 seconds, and that includes the startup time for the kernel which is probably a substantial part of that.

How long is "so long to run" in your case?
POSTED BY: Bill Simpson
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