当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Assignments_assignment 14

资源类别:文库,文档格式:PDF,文档页数:8,文件大小:828.16KB,团购合买
点击下载完整版文档(PDF)

Homework 14(Due:11:00 pm March.23) The basic description of the project is given in following detail description.But the project requirement is specified here(which is a little different from the original one): Your job in this project is to implement a simulation program for cold trap described in the following problem statement.Your job is to write a few functions called by coldtrap.m to simulate the cold trap process for a given set of conditions specified by sizes, numOfMolecules and iterationSteps.Where: sizes is defined as [N,M,ChamberH,ChamberL,PipeH,PipeL].For your initial test,it is set to[32,32,30,10,16,10]; numMolecues is the number of molecules initially existed in the left chamber. For your initial test,it is set to 100; iterationSteps specify the iteration steps for simulation.For your initial test,it is set to 1000: Functions you are going to write: a.function chamber chamberset (sizes) 8CHAMBERSET will set a array chamber (sizes(1),sizes (2))represent 8 the data could be used to generate Picture shown as Figure 11-9. s the value in each cell of the chamber should be equal to the number 8 molecules it contains (initially,they should all set to zero at 8Figure 11-9 indicated). b.function molecules moleculesSet (sizes,numMolecules) 8MOLECULESSET generates a molecules array of size [numMolecules,4] $molecules (:1)for the randomly generated molecules 8 molecules (:2)are their row indices inside the left chamber 8 molecules (:3)are their col indices inside the left chamber 8 molecules (:4)contains logical values for whether it is reached destination or not (the right most column of the big chamber. c.function [chamber,molecules]=moveMolecules .. (chamber,molecules,sizes,iterationsteps) MOVEMOLECULES will simulate the molecules move randomly from left chamber to their steady state (being trapped at the right most 8column of the big chamber. It contains a few iterations 号 for every iteration i<iterationsteps for all molecules who have not reached the destination randomly move it according to the rules specified in the problem specification Hint: You need to track the number of molecules being moved and update the chamber so that each cell of the chamber contains right number of molecules in that cell

Homework 14 (Due: 11:00 pm March. 23) The basic description of the project is given in following detail description. But the project requirement is specified here (which is a little different from the original one): Your job in this project is to implement a simulation program for cold trap described in the following problem statement. Your job is to write a few functions called by coldtrap.m to simulate the cold trap process for a given set of conditions specified by sizes, numOfMolecules and iterationSteps. Where: • sizes is defined as [N, M, ChamberH, ChamberL, PipeH, PipeL]. For your initial test, it is set to [32, 32, 30, 10, 16, 10]; • numMolecues is the number of molecules initially existed in the left chamber. For your initial test, it is set to 100; • iterationSteps specify the iteration steps for simulation. For your initial test, it is set to 1000; Functions you are going to write: a. function chamber = chamberSet (sizes) % CHAMBERSET will set a array chamber (sizes(1), sizes (2)) represent % the data could be used to generate Picture shown as Figure 11-9. % the value in each cell of the chamber should be equal to the number % molecules it contains (initially, they should all set to zero at % Figure 11-9 indicated). b. function molecules = moleculesSet (sizes, numMolecules) % MOLECULESSET generates a molecules array of size = [numMolecules, 4] % molecules (:, 1) for the randomly generated molecules % molecules (:, 2) are their row indices inside the left chamber % molecules (:, 3) are their col indices inside the left chamber % molecules (:, 4) contains logical values for whether it is reached % destination or not (the right most column of the big chamber. c. function [chamber, molecules] = moveMolecules … (chamber, molecules, sizes, iterationSteps) % MOVEMOLECULES will simulate the molecules move randomly from left % chamber to their steady state (being trapped at the right most % column of the big chamber. % It contains a few iterations % for every iteration i < iterationSteps % for all molecules who have not reached the destination % randomly move it according to the rules specified in the % problem specification % Hint: % You need to track the number of molecules being moved and % update the chamber so that each cell of the chamber contains % right number of molecules in that cell

function nTrapped coldTrap (sizes,numMolecules,iterationsteps) &COLDTRAP will simulate the cold trap algorithm described in the project &6 of Chapter 11 of ICP book 号inputs: 号 sizes is defined as IN,M,ChamberH,ChamberL,PipeH,PipeL]. 号 For your initial test,it is set to [32,32,30,10,16,10]; 号 numMolecues is the number of molecules initially existed in the left 号 chamber.For your initial test,it is set to 100; iterationsteps specify the iteration steps for simulation. 号 For your initial test,it is set to 1000; 号outputs: nTrapped:number of molecules traped to the right most columns after given number of iterations rand ('state',0); chamber chamberset (sizes); &the following code will plot the figure 11-9 N=sizes (1); M=sizes (2); plotC zeros (N+1,M+1)+4; plotc (1:N,1:M)=chamber; pcolor (1:M+1,1:N+1,plotC); colorbar; disp ('hit return to continue') pause; $wait for user's command to continue 号numMolecules=100; molecules moleculesSet (sizes,numMolecules); $the following codes will update chamber cells according to the random &distribution of those molecules for index 1:length (molecules) ii=molecules (index,2); jj=molecules (index,3); chamber (ii,jj)=chamber (ii,jj)+1; one added for boundary end &plot the initial state of the molecules distribution (Figure 11-10) plotc (1:N,1:M)=chamber; pcolor (1:M+1,1:N+1,plotc); colorbar; hold on disp ('hit return to continue') pause; $wait for user's command to continue $iterationsteps 1000; [chamber,molecules]moveMolecules (chamber,molecules,sizes, iterationsteps); nTrapped sum (molecules (:4)==1); fprintf ('&d molecules reached the destination.\n',nTrapped); plotc (1:N,1:M)=chamber; pcolor (1:M+1,1:N+1,plotC); colorbar;

function nTrapped = coldTrap (sizes, numMolecules, iterationSteps) % COLDTRAP will simulate the cold trap algorithm described in the project % 6 of Chapter 11 of ICP book % inputs: % sizes is defined as [N, M, ChamberH, ChamberL, PipeH, PipeL]. % For your initial test, it is set to [32, 32, 30, 10, 16, 10]; % numMolecues is the number of molecules initially existed in the left % chamber. For your initial test, it is set to 100; % iterationSteps specify the iteration steps for simulation. % For your initial test, it is set to 1000; % outputs: % nTrapped: number of molecules traped to the right most columns after % given number of iterations rand ('state', 0); chamber = chamberSet (sizes); % the following code will plot the figure 11-9 N = sizes (1); M = sizes (2); plotC = zeros (N+1, M+1)+4; plotC (1:N, 1:M) = chamber; pcolor (1:M+1, 1:N+1, plotC); colorbar; disp ('hit return to continue') pause; % wait for user's command to continue % numMolecules = 100; molecules = moleculesSet (sizes, numMolecules); % the following codes will update chamber cells according to the random % distribution of those molecules for index = 1:length (molecules) ii = molecules (index, 2); jj = molecules (index, 3); chamber (ii, jj) = chamber (ii, jj) + 1; % one added for boundary end % plot the initial state of the molecules distribution (Figure 11-10) plotC (1:N, 1:M) = chamber; pcolor (1:M+1, 1:N+1, plotC); colorbar; hold on disp ('hit return to continue') pause; % wait for user's command to continue % iterationSteps = 1000; [chamber, molecules] = moveMolecules (chamber, molecules, sizes, iterationSteps); nTrapped = sum (molecules (:, 4) == 1); fprintf ('%d molecules reached the destination.\n', nTrapped); plotC (1:N, 1:M) = chamber; pcolor (1:M+1, 1:N+1, plotC); colorbar;

sample results: 30 3.5 3 25 2.5 20 5 0.5 -0.5 5 10 15 20 25 30 >>s1zes=[323230101610]: >>n coldTrap (sizes,100,1000); hit return to continue hit return to continue 47 molecules reached the destination Notes for submission: For this assignment,you need pack all of your *.m files and design documents into a single file and submit it to the Sakai using attachment. Name you packed file in the following matter.The format is important,if you did not follow this rule,your homework may not be able to be grated correctly or on time. sYourlD_hwk4_1.rar Example s5073709022hwk4_1.rar

sample results: Notes for submission: For this assignment, you need pack all of your *.m files and design documents into a single file and submit it to the Sakai using attachment. Name you packed file in the following matter. The format is important, if you did not follow this rule, your homework may not be able to be grated correctly or on time. sYourID_hwk4_1.rar Example : s5073709022_ hwk4_1.rar

11.10.PROJECTS 349 6.This problem sucks! Have you ever wondered how one can make a vacuum? One can make a good vacuum,comparable to the molecular/atomic density in deep space by employing a multi-stage process.In the first stage would we would take,say, a cylinder with a piston in it (much like a combustion chamber in a car)and a pipe attached to a closed canister that you want to evacuate.With a valve in the canister (to the cylinder)open and a valve in the cylinder (to the outside)closed,withdraw the piston.The pressure in the canister and the pipe is reduced.Now,close the valve to the canister and open the valve to the outside,allow the piston to descend and repeat.One can only go so far with this method.Soon there are too few molecules in the canister and enough molecules in the mechanism of the outside valve that one gets very little or no gain on each stage of the pump cycle. The second stage (the one that this problem is concerned about)is to introduce a cold trap.A separate chamber (attached to the canister)has a cold surface in it to which molecules adhere.This further reduces the number of molecules.A third stage might involve causing the molecules to become ionized and then they may be "swept out"with an electric field.Making a vacuum is an interesting combination of a lot of different technologies-mechanical,electrical,chemical...Let's focus on the second stage.With MATLAB,first construct two chambers of identical dimensions,connected 2 20 0.5 10 15 20 25 30 Figure 11.9:Two chambers and a pipe (evacuated,value =0)surrounded by walls (solid, value =-1). together with a pipe.The ones I made were made up of 2D arrays and look like those depicted in first figure.In my example I used chambers that were 30 units high and 10 units wide.The pipe was 16 units high and 10 units wide.(A "unit"is the width of

11.10. PROJECTS 349 6. This problem sucks! Have you ever wondered how one can make a vacuum? One can make a good vacuum, comparable to the molecular/atomic density in deep space by employing a multi-stage process. In the first stage wouldwe wouldtake, say, a cylinder with a piston in it (much like a combustion chamber in a car) and a pipe attachedto a closedcanister that you want to evacuate. With a valve in the canister (to the cylinder) open and a valve in the cylinder (to the outside) closed, withdraw the piston. The pressure in the canister and the pipe is reduced. Now, close the valve to the canister and open the valve to the outside, allow the piston to descend and repeat. One can only go so far with this method. Soon there are too few molecules in the canister andenough molecules in the mechanism of the outside valve that one gets very little or no gain on each stage of the pump cycle. The secondstage (the one that this problem is concernedabout) is to introduce a cold trap. A separate chamber (attachedto the canister) has a coldsurface in it to which molecules adhere. This further reduces the number of molecules. A third stage might involve causing the molecules to become ionizedandthen they may be “swept out” with an electric field. Making a vacuum is an interesting combination of a lot of different technologies—mechanical, electrical, chemical... Let’s focus on the second stage. With MATLAB, first construct two chambers of identical dimensions, connected −1 −0.5 0 0.5 1 1.5 2 2.5 3 3.5 4 5 10 15 20 25 30 5 10 15 20 25 30 Figure 11.9: Two chambers and a pipe (evacuated, value = 0) surrounded by walls (solid, value = -1). together with a pipe. The ones I made were made up of 2D arrays and look like those depictedin first figure. In my example I usedchambers that were 30 units high and10 units wide. The pipe was 16 units high and 10 units wide. (A “unit” is the width of

350 CHAPTER 11.PROGRAMMING IN MATLAB a "cell".)Thus the entire geometry of the pipe,the two chambers and enclosing walls could be represented by a 32 x 32 array in MATLAB.The space within the walls is given the value“o”for vacuum and the walls are given the value“-l”. Now,fill one of the chambers with molecules,placed randomly.In the example in second figure,I placed 100 molecules.Note that I allowed more than 1 molecule per cell.Each cell contains a number that represents the number of molecules within the cell.Since MATLAB's random number generator seeds differently every time,your distributions will look different. 3.5 25 2.5 20 1.5 10 0.5 0.5 10 15 20 25 30 Figure 11.10:The left chamber filled with 100 molecules. Now,a time-step is executed.During this time-step,each individual molecule can move randomly in any direction by one cell,upwards,downwards,sideways,or diagonally. If the new direction would cause the molecule to hit one of the walls,the proposed step is invalid and a new direction should be picked randomly.If a molecule enters the rightmost column of cells,it "sticks"to the cold wall and can no longer be moved. The third figure shows the distribution of molecules after 2000 time-steps. Finally,the simulation ends when all the molecules are trapped in the cold trap.The final configuration of my example is shown in the fourth figure. Here is what you must do to complete this problem

350 CHAPTER 11. PROGRAMMING IN MATLAB a “cell”.) Thus the entire geometry of the pipe, the two chambers andenclosing walls couldbe representedby a 32 × 32 array in MATLAB. The space within the walls is given the value “0” for vacuum andthe walls are given the value “-1”. Now, fill one of the chambers with molecules, placedrandomly. In the example in secondfigure, I placed100 molecules. Note that I allowedmore than 1 molecule per cell. Each cell contains a number that represents the number of molecules within the cell. Since MATLAB’s random number generator seeds differently every time, your distributions will look different. −1 −0.5 0 0.5 1 1.5 2 2.5 3 3.5 4 5 10 15 20 25 30 5 10 15 20 25 30 Figure 11.10: The left chamber filledwith 100 molecules. Now, a time-step is executed. During this time-step, each individual molecule can move randomly in any direction by one cell, upwards, downwards, sideways, or diagonally. If the new direction would cause the molecule to hit one of the walls, the proposed step is invalidanda new direction shouldbe pickedrandomly. If a molecule enters the rightmost column of cells, it “sticks” to the coldwall andcan no longer be moved. The thirdfigure shows the distribution of molecules after 2000 time-steps. Finally, the simulation ends when all the molecules are trapped in the cold trap. The final configuration of my example is shown in the fourth figure. Here is what you must do to complete this problem

11.10.PROJECTS 351 % 5 25 20 0 5 10 15 20 25 30 Figure 11.11:After 2000 time steps,73 molecules have stuck to the cold wall in the cold trap and27 are still“free

11.10. PROJECTS 351 −1 0 1 2 3 4 5 6 5 10 15 20 25 30 5 10 15 20 25 30 Figure 11.11: After 2000 time steps, 73 molecules have stuck to the coldwall in the cold trap and27 are still “free

352 CHAPTER 11.PROGRAMMING IN MATLAB 30 25 5 30 3 2 5 y 5 10 15 20 25 30 Figure 11.12:After about 8000 time-steps all the molecules have stuck to the cold wall of the cold trap

352 CHAPTER 11. PROGRAMMING IN MATLAB −1 0 1 2 3 4 5 6 7 8 5 10 15 20 25 30 5 10 15 20 25 30 Figure 11.12: After about 8000 time-steps all the molecules have stuck to the coldwall of the coldtrap

11.10.PROJECTS 353 You must write a MATLAB script M-file that does the following: (a)Prompt the user for: i.Chamber height and width ii.Connecting pipe height and width. The pipe height must be less than or equal to the chamber height and greater than 0. iii.The number of molecules to simulate. (b)Display the initial geometry as in first figure. (c)Place the molecules in the left chamber and display the initial configuration as in the second figure. (d)Execute the time steps until all the molecules stick to the right most wall.Display the final configuration as in fourth figure as well as the number of time-steps it took to do the simulation. Some hints: .When testing your code,use geometries with just a few cells for height and width and just a few molecules.This will allow the simulations to complete quickly and will test the more subtle parts of the code,the wall collisions and the action of sticking to the cold wall. When testing your code,make lots of use of visual output.Put in some graph- ical output for some or all of the time-steps with a pause statement following the graphics instruction.For example,the third figure shows some intermediate graphical output.This is a very valuable way of debugging the operation of your code. You may need the following MATLAB function in your code (it is not mentioned in the book!)to place the molecules randomly at the start.Using help uint32: UINT32 Convert to unsigned 32-bit integer. I UINT32(X)converts the elements of the array X into unsigned 32-bit integers.X can be any numeric object (such as a DOUBLE).The values of a UINT32 range from 0 to 4294967295.Values outside this range are mapped to 0 or 4294967295.If X is already an unsigned 32-bit integer array,UINT32 has no effect. See also ...UINT8,UINT16,INT8,INT16,INT32

11.10. PROJECTS 353 You must write a MATLAB script M-file that does the following: (a) Prompt the user for: i. Chamber height andwidth ii. Connecting pipe height andwidth. The pipe height must be less than or equal to the chamber height andgreater than 0. iii. The number of molecules to simulate. (b) Display the initial geometry as in first figure. (c) Place the molecules in the left chamber anddisplay the initial configuration as in the secondfigure. (d) Execute the time steps until all the molecules stick to the right most wall. Display the final configuration as in fourth figure as well as the number of time-steps it took to do the simulation. Some hints: • When testing your code, use geometries with just a few cells for height and width andjust a few molecules. This will allow the simulations to complete quickly and will test the more subtle parts of the code, the wall collisions and the action of sticking to the coldwall. • When testing your code, make lots of use of visual output. Put in some graph￾ical output for some or all of the time-steps with a pause statement following the graphics instruction. For example, the thirdfigure shows some intermediate graphical output. This is a very valuable way of debugging the operation of your code. • You may needthe following MATLAB function in your code (it is not mentioned in the book!) to place the molecules randomly at the start. Using help uint32: UINT32 Convert to unsigned 32-bit integer. I = UINT32(X) converts the elements of the array X into unsigned 32-bit integers. X can be any numeric object (such as a DOUBLE). The values of a UINT32 range from 0 to 4294967295. Values outside this range are mapped to 0 or 4294967295. If X is already an unsigned 32-bit integer array, UINT32 has no effect. . . . See also ...UINT8, UINT16, INT8, INT16, INT32

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
已到末页,全文结束
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有