Computer Science, asked by sahilabhi, 7 months ago

Write a program to implement Monte Carlo Simulation in matlab

Answers

Answered by aadhyarajkumar
2

Answer:

Monte Carlo simulation is a technique used to study how a model responds to randomly generated inputs. It typically involves a three-step process:

Randomly generate “N” inputs (sometimes called scenarios).

Run a simulation for each of the “N” inputs. Simulations are run on a computerized model of the system being analyzed.

Aggregate and assess the outputs from the simulations. Common measures include the mean value of an output, the distribution of output values, and the minimum or maximum output value.

Answered by cu17BCS2904
1

Answer:

clear, clc, close all

% generate a set of random numbers x(0, 1), y(0, 1) and z(0, 1)

x = rand(1, 100);

y = rand(1, 100);

z = rand(1, 100);

% visualizing

[X, Y] = meshgrid(0:0.001:1, 0:0.001:1);

Z = sqrt(1 - X.^2 - Y.^2);

Z(imag(Z) ~= 0) = NaN;

figure(3)

surf(X, Y, Z)

shading interp

hold on

axis([0 1 0 1 0 1])

colormap([1,0,0])

view(45,45)

alpha(.1)

% set(gca, 'FontName', 'Times New Roman', 'FontSize', 10)

title('1/8 unit sphere vol Cartesian coordinates')

text(1,-1 , .5, {['The volume of the sphere could be represented as ' ...

   'eight times the ratio of the points inside the sphere section vs. all points.']}, ...

   'EdgeColor', 'k', 'FontName', "Algerian", 'FontSize', 20, 'FontWeight', 'bold')

for k = 1:length

   (x)

   

   if x(k)^2 + y(k)^2 + z(k)^2 <= 1

       

       % the point is inside the sphere, show it in red

       plot3(x(k), y(k), z(k), '.r')

       

   else

       

       % the point is outside the sphere, show it in blue

       plot3(x(k), y(k), z(k), '.b')

       

   end

     

drawnow update

   

end

% find all x for which x.^2 + y.^2 + z.^2 <= 1

w = x(x.^2 + y.^2 + z.^2 <= 1);

% estimate the unit sphere volume as a probability ratio

% the real value is approx. 4.1888

volest = 8*length(w)/length(x);

err = (volest - 4.1888)/4.1888*100;

disp(['The estimated volume is ' num2str(volest)])

disp(['The error of the estimation is ' num2str(err) ' %'])

Similar questions