Docsity

Monte Carlo Method Assignment for CS 257: Computing the Volume of a Surface using MATLAB -, Assignments of Computer Science

Toggle sidebar
bg1
Homework 7
CS 257
Assigned Date: 4/11/2005
Due Date: 4/19/2005
Purpose
The purpose of this assignment is to familiarize you with the Monte Carlo Method of Integration,
as well as 3-D plotting in MATLAB.
This homework has two parts. Note that you need to hand in three MATLAB functions. You
also should print out the figure generated and hand it in as a written part of the homework.
Make sure that your figure includes the appropriate settings described below and that you do
not rotate/translate your final graph in any way.
Assignment
In this assignment you are asked to use the Monte Carlo method to compute the volume of the
interior of a surface given as follows:
(1 −x2)(4 −x2) + y2+z2= 0 (1)
In order to complete the assignment you need to construct a 3-dimensional box surrounding
the surface, and generate nrandom points in the box. Next, you approximate the volume of
the interior by computing the fraction of points which lie inside the surface and multiplying
this fraction by the volume of the entire box. Once you have computed this volume, you are
asked to provide a plot of your work.
Part 1: Code
You need to write and turn in the following 3 functions:
1. function v = volume(n, xmin, xmax, ymin, ymax, zmin, zmax)
%INPUT: n represents the number of random points to be generated
% xmin, xmax, ymin, ymax, zmin, zmax represent the limits of
% the 3D box that encapsulates the surface
%OUTPUT: approximate volume of the interior of the surface calculated
% using Monte Carlo
This function represents the main function of your code. After performing the Monte
Carlo method, it should call the remaining two functions listed below in order to generate
the desired output. Within this function you should:
1
pf3
pf4

Partial preview of the text

Download Monte Carlo Method Assignment for CS 257: Computing the Volume of a Surface using MATLAB - and more Assignments Computer Science in PDF only on Docsity!

Homework 7

CS 257

Assigned Date: 4/11/

Due Date: 4/19/

Purpose The purpose of this assignment is to familiarize you with the Monte Carlo Method of Integration, as well as 3-D plotting in MATLAB. This homework has two parts. Note that you need to hand in three MATLAB functions. You also should print out the figure generated and hand it in as a written part of the homework. Make sure that your figure includes the appropriate settings described below and that you do not rotate/translate your final graph in any way. Assignment In this assignment you are asked to use the Monte Carlo method to compute the volume of the interior of a surface given as follows:

(1 − x^2 )(4 − x^2 ) + y^2 + z^2 = 0 (1)

In order to complete the assignment you need to construct a 3-dimensional box surrounding the surface, and generate n random points in the box. Next, you approximate the volume of the interior by computing the fraction of points which lie inside the surface and multiplying this fraction by the volume of the entire box. Once you have computed this volume, you are asked to provide a plot of your work. Part 1: Code You need to write and turn in the following 3 functions:

  1. function v = volume(n, xmin, xmax, ymin, ymax, zmin, zmax) %INPUT: n represents the number of random points to be generated % xmin, xmax, ymin, ymax, zmin, zmax represent the limits of % the 3D box that encapsulates the surface %OUTPUT: approximate volume of the interior of the surface calculated % using Monte Carlo This function represents the main function of your code. After performing the Monte Carlo method, it should call the remaining two functions listed below in order to generate the desired output. Within this function you should:
  • Generate n random points using the Monte Carlo Method inside the 3D box and store this value in an n-by-3 matrix: xr
  • Calculate the ratio between points that lie inside the surface and n. Multiply the volume of the 3D vox with this value to get an approximate of the volume of the interior. Make sure to save the points that are inside the surface in vector format so that you can pass this vector into the function drawPoints.
  • Plot the shape and the box (call drawSurfaceandBox).
  • Plot the n random points using different colors for those inside and outside the surface (call drawPoints).
  1. function drawSurfaceandBox(xmin, xmax, ymin, ymax, zmin, zmax) %INPUT: xmin, xmax, ymin, ymax, zmin, zmax represent the % limits of the 3D box that encapsulates the surface This function should plot the surface and the 3D box (refer to lecture notes). In order to get a correct plot you should do the following:
  • The range of your meshgrid for the surface should be: − 2 ≤ x ≤ 2, − 1. 5 ≤ y ≤ 1 .5, − 1. 5 ≤ z ≤ 1 .5 with each vector having 0.05 spacing between points.
  • Use the following axes and lighting commands in the plot: camlight(-80,-10); lighting p; axis equal; view(3);
  • Use the value k = 0 when generating the plot of the surface as below: isosurface(X, Y, Z, V, k)
  • Plot both the box and the surface in the same figure. Color the surface green, color the edges of the 3D box black.
  1. function drawPoints(xr, inout_vec) %INPUT: xr represents the n-by-3 matrix which stores the x, y, and z % coordinates of the n random numbers generated % inout_vec represents the points which are inside the surface % (this vector is computed in the function volume.m) This function should separate the points inside/outside the surface and plot them: inside the surface blue, and outside the surface red. Refer to lecture notes for further explanations.

Part 2: Generating Output Test your volume function using n = 1000 points on a 3D square box where − 2 ≤ x ≤ 2, − 1. 5 ≤ y ≤ 1 .5, and − 1. 5 ≤ z ≤ 1 .5. Your figure should display the shape, the box, and the points inside and outside the shape as stated above. In the window displaying the figure, click on “Save as”, and in the dialog that pops up make sure that in “Save as type” is set to “Portable Network Graphics file (*.png)”, and name the file as {your NetID}.png. Email this file

Place it in the same directory in which your three function files are located. Run the checker from inside MATLAB by typing CheckMP7 at the command prompt.

The checker will test your volume function, and if it is correct, it will submit an email containing your three functions to the TA. You should get a vacation response email from the TA’s mail account confirming that the email was received. Multiple submissions are now perfectly fine, and the maximum grade of all submissions will be counted. The function must work exactly to our specifications; no partial credit will be given.

And lastly, don’t forget to email a PNG file of your figure to [email protected] (do NOT send it to any other account). You should receive two confirmation mails: one for the code, and one for the graph.