function [estimatepi,c] = estimatepi9(tol)x=rand;y=rand;c=0;hits=rand;n= 0; hit=[];error=[];estimatepi=[];tol=0.0001; error = abs(pi-estimatepi)-tol; while error>tol n=n+1; x=rand; y=rand; hit=(x-0.5).^2+(y.^2)<=1; for c=1:n; hits=sum(hit); estimatepi=4*hits/n; endend end
MATLAB: I am new to Maltlab, i wanted to use a while loop to calculate pi using Montecarlo method. the code below is not giving me any answer can anyone help please.
matlab functionwhile loop
Related Question
- How to generate just one random number in every iteration of for loop
- Approximate the value of π using the following algorithm based on geometric probability. This algorithm is an example of the so called monte carlo method
- Basic monte carlo question: area of circle
- How to do a while loop to estimate pi using monte carlo method
- I’m having trouble calculating the probability to estimate pi. Any help is appreciated.
- How to write a loop which creates a random number and adds the previous values
Best Answer