Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

matlab coursework: i need help with part k of on this matlab assignment. its question that uses previous parts so here is most of the

matlab coursework: i need help with part k of on this matlab assignment. its question that uses previous parts so here is most of the question. i just need help with the last part of these pictures with k and l. thanks

image text in transcribed

image text in transcribed

image text in transcribed

To get the marks for this question you need to have submitted worksheet 8 This example will help you practice for loops, while loops and if statements. In this question we are going to use MATLAB to simulate randomly moving gas particles in a box. To simplify the problem we are only going to simulate a 2D gas in a 2D space. Please note: This question is quite tricky and I don't expect everyone to finish it, I have put it in because it's quite fun and will challenge the people who find MATLAB easy. Try to do as much as you can. Figure 3: Gos particles in a box. Generating a 2D box a) Define a 20 array called 'a' of size 8x8 only containing zeros, this array represents an empty 2D box containing no gas particles. We are going to represent gas particles within the box by using the number 1. Set the point at 3,3 to be equal to one, this represents one gas particle at position 3,3. b) Using the imagesc make your script display the 2D matrix 'a'. This command works just like surf but only produces flat 2D images. c) just as you did in question 5 find out the size of the matrix 'a', and store its x and y size in the variable 'x_len' and 'y_len'. d) Again, just as you did in question 5, write two nested for loops which iterate through all the x and y coordinates to the array 'a. Use sprint and disp to output the following text for each point in the 2D array: At the value x=77, y=?? the value of a=?? Where ?? are replaced by the current x value, y value and corresponding value of aly.x) e) Place the disp command and the sprintf within an If statement, so that the text is only printed when value at aly.x) is equal to 1. Change the line of text to read: "A gas particle exists at x=27 y=P? where, ?? is replaced by the actual x and y coordinate of the gas particle. Moving the gas particle ) Gas particles will randomly move within any space due to thermal energy. In order to simulate this movement, we need to write some code to randomly move the gas particle in our 2D matrix. Change your statement you wrote above, so that your code finds a gas particle, it will choose a random number between 0 and 4, store this number in the variabler. Depending upon the number chosen we are going to move our gas particle up (r==0), down (r==1), right (r==2) or left (r==3). We therefore need the rand command to produce only whole numbers, we can do this by using the floor command to round 'r down to the nearest whole number. Use the floor command to make sure only whole numbers are randomly generated. g) Write an X-elself-elself statement which will print the following text using the disp command, depending upon the value of r generated: Value of r 0 Text to print Moving particle up Moving particle down Moving particle right Moving particle left 2 3 h) The above if-elself-elself tells us which direction we would like to move the particle, but does not actually move the particle in the 2D array. In this next section we will write the code to actually move the particle. Firstly, above the ik elself statement but still in the for loops, save the current value of x and y to two variables called x_new, and y_new. Now comment out the dise commands within the Welself-elself statement and replace them with the following commands: 0:y new-y_new+1 1:y new-y_new-1 2: X_new=X_new+1 3: x_new-xnew-1 This will generate a new random coordinate for the gas particle only one pixel away from where it currently resides, Hint: In the next few subsections, the script will start to produce a lot of output to the screen, printing to the screen is very slow, to speed up your code use the ; command at the end of lines. 1) The final job is to move the particle to the new coordinate we have generated. Place these commands: aly.x)=0 aly_new,x_new)=1 within the for loops but after the if-elself-elself statements to move the gas particle by one step. Make sure the imagesc command comes after both for loops, and run the program to see what happens to your particle. ]) in order to make your particle continually move you need to place the two nested for loops in a whlle loop. Make a while loop that runs 1000 times. The code may run so fast that your 2D plot of the matrix will not update, if this happens place the command pause(0.5) after the imagesc command. Your particle should now randomly move in the box until it reaches the edge. Why does the program crash when the gas particle reaches the edge of the box? Write the reason for the crash at the top of the script. k) To keep the particle within the box use four nested If statements to check it the values of y_new.x_new are within the array. Rerun the code and see what happens to the particle. Congratulations you have simulated a gas particle in a box! Now, add four more particles to your box and let the simulation run. And run the code, what is what is wrong with the simulation (place the answer at the top of the script)? D) To stop the gas particles destroying themselves when they meet. wrap the code that moves the gas particle: To get the marks for this question you need to have submitted worksheet 8 This example will help you practice for loops, while loops and if statements. In this question we are going to use MATLAB to simulate randomly moving gas particles in a box. To simplify the problem we are only going to simulate a 2D gas in a 2D space. Please note: This question is quite tricky and I don't expect everyone to finish it, I have put it in because it's quite fun and will challenge the people who find MATLAB easy. Try to do as much as you can. Figure 3: Gos particles in a box. Generating a 2D box a) Define a 20 array called 'a' of size 8x8 only containing zeros, this array represents an empty 2D box containing no gas particles. We are going to represent gas particles within the box by using the number 1. Set the point at 3,3 to be equal to one, this represents one gas particle at position 3,3. b) Using the imagesc make your script display the 2D matrix 'a'. This command works just like surf but only produces flat 2D images. c) just as you did in question 5 find out the size of the matrix 'a', and store its x and y size in the variable 'x_len' and 'y_len'. d) Again, just as you did in question 5, write two nested for loops which iterate through all the x and y coordinates to the array 'a. Use sprint and disp to output the following text for each point in the 2D array: At the value x=77, y=?? the value of a=?? Where ?? are replaced by the current x value, y value and corresponding value of aly.x) e) Place the disp command and the sprintf within an If statement, so that the text is only printed when value at aly.x) is equal to 1. Change the line of text to read: "A gas particle exists at x=27 y=P? where, ?? is replaced by the actual x and y coordinate of the gas particle. Moving the gas particle ) Gas particles will randomly move within any space due to thermal energy. In order to simulate this movement, we need to write some code to randomly move the gas particle in our 2D matrix. Change your statement you wrote above, so that your code finds a gas particle, it will choose a random number between 0 and 4, store this number in the variabler. Depending upon the number chosen we are going to move our gas particle up (r==0), down (r==1), right (r==2) or left (r==3). We therefore need the rand command to produce only whole numbers, we can do this by using the floor command to round 'r down to the nearest whole number. Use the floor command to make sure only whole numbers are randomly generated. g) Write an X-elself-elself statement which will print the following text using the disp command, depending upon the value of r generated: Value of r 0 Text to print Moving particle up Moving particle down Moving particle right Moving particle left 2 3 h) The above if-elself-elself tells us which direction we would like to move the particle, but does not actually move the particle in the 2D array. In this next section we will write the code to actually move the particle. Firstly, above the ik elself statement but still in the for loops, save the current value of x and y to two variables called x_new, and y_new. Now comment out the dise commands within the Welself-elself statement and replace them with the following commands: 0:y new-y_new+1 1:y new-y_new-1 2: X_new=X_new+1 3: x_new-xnew-1 This will generate a new random coordinate for the gas particle only one pixel away from where it currently resides, Hint: In the next few subsections, the script will start to produce a lot of output to the screen, printing to the screen is very slow, to speed up your code use the ; command at the end of lines. 1) The final job is to move the particle to the new coordinate we have generated. Place these commands: aly.x)=0 aly_new,x_new)=1 within the for loops but after the if-elself-elself statements to move the gas particle by one step. Make sure the imagesc command comes after both for loops, and run the program to see what happens to your particle. ]) in order to make your particle continually move you need to place the two nested for loops in a whlle loop. Make a while loop that runs 1000 times. The code may run so fast that your 2D plot of the matrix will not update, if this happens place the command pause(0.5) after the imagesc command. Your particle should now randomly move in the box until it reaches the edge. Why does the program crash when the gas particle reaches the edge of the box? Write the reason for the crash at the top of the script. k) To keep the particle within the box use four nested If statements to check it the values of y_new.x_new are within the array. Rerun the code and see what happens to the particle. Congratulations you have simulated a gas particle in a box! Now, add four more particles to your box and let the simulation run. And run the code, what is what is wrong with the simulation (place the answer at the top of the script)? D) To stop the gas particles destroying themselves when they meet. wrap the code that moves the gas particle

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions