-
-
Notifications
You must be signed in to change notification settings - Fork 359
adding matlab implementation to monte carlo integration #696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding matlab implementation to monte carlo integration #696
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good code. Runs very quickly, does what it says.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just a few comments.
@@ -0,0 +1,20 @@ | |||
monte_carlo(10000000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should go at the bottom of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that it looks weird, but matlab instructions (not in a function) in an .m file must come before all function definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Welp, that's what I get for trying to run this in Octave and not knowing that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha yeah, matlab is weird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed this is resolved then.
pi_estimate = 4*sum(incircle_array)/n; | ||
|
||
fprintf("The pi estimate is: %f\n", pi_estimate); | ||
fprintf("Percent error is: %f%%", 100 * abs(pi_estimate - pi) / pi); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, separate the printing from the computation: have monte_carlo
compute the estimate for pi, and print the result after the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was following the julia implementation: it also just prints and does not return the value. I can still make the change but I believe what I have is consistent with what's already in the archive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just took a look at a few of the other code samples, and most have the printing outside the function except for Julia, so I think the Julia code should be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I will make the change now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I'll update Julia today. Thanks for the mention of inconsistency!
pi_estimate = monte_carlo(10000000); | ||
|
||
fprintf("The pi estimate is: %f\n", pi_estimate); | ||
fprintf("Percent error is: %f%%", 100 * abs(pi_estimate - pi) / pi); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes added!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one thing, can you add a newline after Percent error is
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've downloaded MATLAB through work and, like Peter, confirmed that this works. Just one last request.
pi_estimate = monte_carlo(10000000); | ||
|
||
fprintf("The pi estimate is: %f\n", pi_estimate); | ||
fprintf("Percent error is: %f%%", 100 * abs(pi_estimate - pi) / pi); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one thing, can you add a newline after Percent error is
?
I can, but is that really necessary? Any other print that happens after running the file in a program will automatically be run on a new line since it's a new command. |
Yes, but I don't think it looks good:
What if the printed line was really long? Your prompt would be on the right-hand side of the screen. |
Oh I see. I was using a jupyter terminal and that didn't happen. I'll get that done now. |
Change added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice that you can use Jupyter and aren't stuck with their ancient REPL. Approved!
No description provided.