Question

In: Physics

using matlab can you show me an example of a monte carlo that has two things...

using matlab can you show me an example of a monte carlo that has two things randomizing?

Solutions

Expert Solution

ANSWER:

% Using Monte Carlo Simulation to estimate Pi in Matlab 

% This is a classic example of how Monte Carlo Simulation can be a powerful tool in regards to solving % complex and non-linear challenges.

function MC_estimatePi_withError %Estimate pi using Monte Carlo to simulate dart shooting clc; clear; format short g; % This code will execute the Monte Carlo simulation just once N = 1000; score = shootingDart(N); estimated_pi = 4*mean(score); std_error = std(4*score)/sqrt(N); disp(' Pi Pi estimate SEM') disp([pi, estimated_pi, std_error]); %now display a confidence interval z_confidence = norminv(.975); %2-sided 95% interval; %note: if you use .025 instead of .975, this %will be negative min_of_interval = estimated_pi - z_confidence*std_error; max_of_interval = estimated_pi + z_confidence*std_error; disp('95% Confidence Interval:'); fprintf('[%g, %g]',min_of_interval,max_of_interval); disp(' '); disp(' '); %This code executes the Monte Carlo simulation multiple times with % increasing values for N, showing convergence power = 1:6; N = 10.^power; estimated_pi = zeros( length(N), 1); std_error = zeros( length(N),1); for k = 1:length(N) current_N = N(k); score = shootingDart( current_N ); estimated_pi(k) = 4*mean(score); std_error(k) = std(4*score)/sqrt(current_N); end format shortg disp(' N Pi estimate SEM'); disp( [N', estimated_pi, std_error]); %You could also use fprintf to display your results disp(' '); disp(' '); disp(' '); disp('Same results using fprintf()'); fprintf('%10s %15s %10s\n','N','Pi Estimate','SEM'); fprintf('%10d %15g %10g\n', [N; estimated_pi'; std_error']); %This is a nested function. Note that we can only use nested functions in a %function file (i.e., we cannot create a nested function in a script file. function score = shootingDart(n) % generate random number I to estimate pie %%%%%%%%%%%%%%%%%%%%% % Vectorized Version% %%%%%%%%%%%%%%%%%%%%% x1 = rand(n,1); x2 = rand(n,1); sum_of_x_squares = x1.^2 + x2.^2; score = (sum_of_x_squares <= 1); % % non-vectorized version % score = zeros(n,1); % for i = 1:n % x1 = rand; % x2 = rand; % sum_of_x_squares = x1*x1 + x2*x2; % if sum_of_x_squares <= 1 % score(i) = 1; % end % end 

Related Solutions

Write a matlab program that determines the value of pi using the monte carlo technique. do...
Write a matlab program that determines the value of pi using the monte carlo technique. do this for a loop of multiple fixed points. (i.e 100-10000) Plot the computed value of pi and the difference from the true value as this number increases. Time the execution of your code for various numbers of points, and plot the precision vs the computational cost.
in Matlab, Use the Monte Carlo analysis to compute the area of a circle with radius...
in Matlab, Use the Monte Carlo analysis to compute the area of a circle with radius 1. print out your code, at least one figure on which the circle and ‘dart hits’ are shown, and numerical results for N=10, 100,1000. For each N, repeat the calculation at least 5times.
Can anyone walk me through this please!!-- Build a Monte Carlo simulation model to solve a...
Can anyone walk me through this please!!-- Build a Monte Carlo simulation model to solve a project problem. He does not provide that information, it just says a probabilistic model.
Can you show me a UML example for the game mastermind. Using C++
Can you show me a UML example for the game mastermind. Using C++
Please provide an example of a Monte Carlo simulation model (and explain). Your simulation example should...
Please provide an example of a Monte Carlo simulation model (and explain). Your simulation example should be able to: Tackle a wide variety of problems using simulation Understand the seven steps of conducting a simulation Explain the advantages and disadvantages of simulation Develop random number intervals and use them to generate outcomes Understand alternative computer simulation packages available Explain the different type of simulations Explain the basic concept of simulation
Describe the purpose of Monte Carlo simulations. How can these be used in designs?
Describe the purpose of Monte Carlo simulations. How can these be used in designs?
Explain Monte Carlo Sampling? Under what circumstances, can it be used? Elaborate on the application and...
Explain Monte Carlo Sampling? Under what circumstances, can it be used? Elaborate on the application and limitations related to this sampling?
Can you all show me an specialization and production possibilities model and example?
Can you all show me an specialization and production possibilities model and example?
Can you show me an example of a detailed family-systems based assessment?
Can you show me an example of a detailed family-systems based assessment?
After applying Monte Carlo Simulation for a potential investment opportunity, you observed that the mean of...
After applying Monte Carlo Simulation for a potential investment opportunity, you observed that the mean of the NPV distribution generated from the 10,000 iterations of this investment opportunity was $320 million and the Value at Risk (VAR) was $-550 million. Present a balanced recommendation based on these simulation results regarding whether or not the firm should make this investment.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT