# Create a function that takes two parameters and returns one output?

Posted 1 year ago
560 Views
|
2 Replies
|
1 Total Likes
|
 Hello Wolfram Community,I have a function that takes two parameters and is essentially supposed to return one of two things: "line Crossed" or "no line Crossed". However with this current function I am getting Null as my output. Why is this and how can I achieve my desired output. Here is the code: needleCross[length_, distance_] := { Block[ {midPoint = RandomReal[distance], rotationAngle = RandomReal[{-Pi/2, Pi/2}]}, xCoordLinePoints = {midPoint - (Cos[rotationAngle]*length/2), midPoint + (Cos[rotationAngle]*length/2)}]; If[xCoordLinePoints[[1]] <= 0 || xCoordLinePoints[[2]] >= distance, Print["Line Crossed"], Print["No Line Crossed"]]} any advice would help!Thanks
2 Replies
Sort By:
Posted 1 year ago
 Welcome to Wolfram Community! Please make sure you know the rules: https://wolfr.am/READ-1ST The rules explain how to format your code properly. If you do not format code, it may become corrupted and useless to other members. Please EDIT your posts and make sure code blocks start on a new paragraph and look framed and colored like this. int = Integrate[1/(x^3 - 1), x]; Map[Framed, int, Infinity] 
 Hi,when I run it it does print out the result and gives the output Null.If you only want the output you might want to use: needleCross[length_, distance_] := {Block[{midPoint = RandomReal[distance], rotationAngle = RandomReal[{-Pi/2, Pi/2}]}, xCoordLinePoints = {midPoint - (Cos[rotationAngle]*length/2), midPoint + (Cos[rotationAngle]*length/2)}]; If[xCoordLinePoints[[1]] <= 0 || xCoordLinePoints[[2]] >= distance, "Line Crossed", "No Line Crossed"]}[[1]]; So now you can simulate Buffon's needle experiment to estimate Pi. We know that $\pi \approx \frac{2 \cdot length}{distance \cdot P}$. P is the probability to hit a line. So lets get an estimate for P.If your function is correct then M = 1000000; P = N[(Count[Table[needleCross[1, 1.2], {M}], "Line Crossed"]/M), 10] should give you an estimate of the probability that a needle crosses a line. Then an estimate of Pi should be 2./(1.2*P) which in my case gives 3.13967, 3.1468, 3.13999, 3.13775, 3.14344 for a couple of different runs.If we assume someone really keen on the experiment would run it 400 times on 8000 needles. Then we would get: estimatesP = Monitor[Table[M = 8000; P = N[(Count[Table[needleCross[1, 1], {M}], "Line Crossed"]/M), 10], {i, 1, 400}], i]; piestimates = 2./(1*#) & /@ estimatesP; dist = SmoothKernelDistribution[piestimates]; Plot[ PDF[dist, x], {x, 3.03, 3.28}, Epilog -> Line[{{Mean[dist], 0}, {Mean[dist], 20}}], Filling -> Bottom, PlotTheme -> "Marketing", LabelStyle -> Directive[Bold, Medium], ImageSize -> Large] The expected value is: Mean[SmoothKernelDistribution[piestimates]] or equivalently Mean[piestimates] which equals 3.14029.If we assume that the values are Gaussian distributed we can also represent that: Plot[PDF[EstimatedDistribution[piestimates, NormalDistribution[\[Mu], \[Sigma]]], x], {x, 3.03, 3.28}, Epilog -> Line[{{Mean[dist], 0}, {Mean[dist], 20}}], Filling -> Bottom, PlotTheme -> "Marketing", LabelStyle -> Directive[Bold, Medium], ImageSize -> Large] Cheers,Marco