ywu119
(michelle yrw)
1
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
fkampas
(Frank Kampas)
2
You need to put spaces between x y z and w inside Sin. xyzw is interpreted as a variable by that name.
ywu119
(michelle yrw)
3
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
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?