Write a MATLAB script named PP1P1m that calculates and displ

Write a MATLAB script named PP1_P1.m that calculates and displays an estimate of using a while loop. Run the script and show the output to verify that pi is properly calculated and displayed.

Solution

% matlab code that calculates and displays an estimate of using a while loop.

% total terms
n = 100000;
count = 0;    
while true
    % random variable x and y withoin [0,1]
    x=rand;       
    y=rand;   
  
    %check if the x and y coordinates are within a unit circle
    if (x^2+y^2 <=1)
        count = count +1;
    end
    i = i + 1;
    if i == n
        break
    end
end

approxPI = count;
approxPI = 4 * (approxPI / n);
disp(\"Approximate value of pi: \");
disp(approxPI);
disp(\"Actual value of pi:\");
disp(pi);

%{
output:

Approximate value of pi:                                                            
3.1388                                                                             
Actual value of pi:                                                                 
3.1416

%}

Write a MATLAB script named PP1_P1.m that calculates and displays an estimate of using a while loop. Run the script and show the output to verify that pi is pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site