Question

Write a matlab program that determine the value of pi using Monte Carlo technique, using a...

Write a matlab program that determine the value of pi using Monte Carlo technique, using a while loop for a fixed number of precision (i.e 3 significant figures, 4 significant figures). The level of precision should not reference the actual value of pi . (can use avg approximiaton or std deviation method).

Homework Answers

Answer #1

% MC estimate of pi
clear all;
clc;
%%initialize random points
n=10000;
x=rand([1 n]);
y=rand ([1 n]);
%%compute using vectorization
radii = sqrt(x.^2+y.^2);
hits = sum(radii<=1);
misses = n-hits;
pi_mc = 4*(hits/n);
fprintf('\nUsing Vectorization:: HITS = %d, MISSES = %d, PI = %f\n',hits,misses,pi_mc);
%%compute using a loop
c=0;
s=0;
i=1;
while i<=n
s=s+1;
if x(i)^2 +y(i)^2 <=1 % inside circle
c=c+1;
end
i+=1;
end
p=c/s;
pi_=p*4;
fprintf('\nUsing Loop:: HITS = %d, MISSES = %d, PI = %f\n',c,s-c,pi_);

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a MATLAB program to determine the factorial value of an input integer between 1 and...
Write a MATLAB program to determine the factorial value of an input integer between 1 and 30. You may NOT use the built-in factorial function. You must implement it using a for loop. Display the result back to the user with 2 decimal places (yes, 2 decimal places). The result should follow the following format:   The factorial value is *.xx
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
"Allowed to use any platform" > Octave desktop app, Octave Online, MATLAB App, or MATLAB Online....
"Allowed to use any platform" > Octave desktop app, Octave Online, MATLAB App, or MATLAB Online. Create a script or a function to solve the following problem. The irrational number π can be (indirectly) approximated by an infinite series, that is π4=1-13+15-17+… The script or function should feature the following: the user can input the number of terms of the infinite series n , compute the sum of n terms of the infinite series, compute the absolute difference (error) of...
Using a for Loop visual c++ Summary In this lab the completed program should print the...
Using a for Loop visual c++ Summary In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and...
USING MATLAB Write a program called QuadProg that determines the roots of a quadratic equation. Use...
USING MATLAB Write a program called QuadProg that determines the roots of a quadratic equation. Use functions to implement each action. Zip you directory of *.m files into a single file and turn this in on BBLearn under Class Activities. QuadProg write a function called getInputs to get a single input value. write a function called findDiscriminant write a function called findDenominator write a function called findRoot which will be used for both roots write a function called displayResults displayResults...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Solve the following problem using the MATLAB environment Write a function [approx_root, num_its] = bisection(f,a,b,tol) that...
Solve the following problem using the MATLAB environment Write a function [approx_root, num_its] = bisection(f,a,b,tol) that implements the bisection method. You function should take as input 4 arguments with the last argument being optional, i.e, if the user does not provide the accuracy tol use a default of 1.0e-6 (use varargin to attain this). Your function should output the approximate root, approx_root and the number of iterations it took to attain the root, num_its. However, if the user calls the...
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following...
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following conditional statement. If ( n is even) { n = n / 2; } else { n = 3 * n + 1; } In this case, n is to be input by the user (assume they input a non-negative value), the conditional is performed, and the resulting n is to be output. Again, use the system calls for input, output, and exiting the...
•Write the claim as a math statement, its opposite, and then determine H0and H1and the tail...
•Write the claim as a math statement, its opposite, and then determine H0and H1and the tail type. •Test the claim using the traditional (critical value) method: •Sketch the appropriate distribution showing the tail(s) and their area, and give the critical value(s) determined from the appropriate distribution table. •Show the test statistic formula with the relevant numbers in place, and determine the test statistic value. Show where this value is located on the sketch of the distribution. •State the proper conclusion...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT