首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
代做 program、代写MATLAB 编程语言
项目预算:
开发周期:
发布时间:
要求地区:
MM1CPM_CW2
COMPUTER PROGRAMMING - Coursework 2
Introduction:
This coursework is designed to help you revise your basic MATLAB knowledge
that you have gained in lectures 4-9. For each question you should write a script
that contains the answers. Save your scripts to disk with some sensible name for
example, the ffle containing the code for question one could be called Q1.m.
Once you have ffnished all the questions (or as many as you can manage), zip all
your scripts up into a single zip ffle and upload this to moodle. Name the zip ffle
using the following format Firstname_surname_studentid.zip. So for example my
In each .m ffle you generate, put on the ffrst line your name and e-mail address 每
the markers will use this e-mail address to give you feedback. For questions
which require you to write English sentences just include these as comments
using the '%' character in a script ffle. Try to include comments in the script ffles
describing what the program is doing 每 you will get marks for this.
Finally, please remember this is individual work and not group work. Work
handed in must be entirely your own and not copied from anyone else. By all
means discuss the coursework with your friends BUT then go away and write the
programs yourself.
1Question 1 (10% of total mark): This question will
help you reinforce your basic computer knowledge.
Answer, by writing comments in a script ffle Q1.m:
a) Name the ffve fundamental components of a
computer which were covered in the lecture.
b) In no more than 10 words describe what each
component does.
c) Why does it take more time to get information from
a hard disk than from the computers main memory.
d) Give three examples of an embedded computer that
you use in your daily life.
Question 2 (10% of total mark): All computers store and transmit text as
American Standard Code for Information Interchange (ASCII) code. When you
start writing programs to interface to robots in your mechatronics courses you
will use ASCII code a lot to send commands to these robots. This question helps
you practice using ASCII code in MATLAB, make a new script called Q2.m and
answer the following questions:
a) Write a short program which asks the user to enter their name.
b) Make the program convert the text the user entered to ASCII numbers.
c) Using your program convert the
sentence 'Computers store all data as
numbers' to ASCII code. Save the
resulting list of numbers in Q2.m as a
comment.
Question 3 (10% of total mark):
This question lets you practice using
the sprintf and disp commands. Very
often when dealing with embedded
computers you won't have much
screen space to display information. It
is therefore important to be able to
carefully control text output.
Using one disp command and one
sprintf command make MATLAB print
out the poem on the right.
2
Oh! I have slipped the surly bonds of earth,
And danced the skies on laughter-silvered wings;
Sunward I've climbed, and joined the tumbling
mirth
Of sun-split clouds, --and done a hundred things
You have not dreamed of -- Wheeled and soared and
swung
High in the sunlit silence. Hov'ring there
I've chased the shouting wind along, and flung
My eager craft through footless halls of air...
Up, up the long, delirious, burning blue
I've topped the wind-swept heights with easy grace
Where never lark or even eagle flew --
And, while with silent lifting mind I've trod
The high untrespassed sanctity of space.
John Gillespie Magee, Jr
Figure 1: An Intel processor
chip seen from below. The
gold pins are the electrical
contacts used for getting
information in and out of the
chip.The poem should be formatted exactly the same way as I have formatted it,
including:
*Spaces between lines
*The author's name should be right aligned using tabs.
*All special characters should be printed.
Store your answer in Q3.m.
Question 4 (10% of total mark): This question lets you practice matrix
operations in MATLAB. Write a script to do the following and save it as Q4.m:
a) Generate the following matrices:
a=
[
0 0 1
0 1 0
0 0 1]
b=
[
1 0 0
0 1 0
1 0 0]
b) Add matrix a to matrix b and store the result in 'B'.
c) Subtract matrix a from matrix b, and store the result in 'C'
d) Perform a matrix multiplication of a by b.
e) Add four to each element of matrix a, and store the result in matrix e.
f) Generate a random matrix 10x10 between the values of -10 and 10 called Z
use surf to plot this matrix.
g) Calculate the determinant of matrix Z.
Question 5 (20% of total mark): This question is about basic image
processing, it will help you practice loops and 2D arrays. We are going to use
MATLAB to load in an image, then we are going to write an algorithm to adjust
the exposure. The algorithm we are going to write is used in digital cameras and
photo processing software. From moodle download the zip ffle CW2.zip, in this
ffle you will ffnd the jpg ffle museum_london.jpg, which is a picture of the natural
history museum in London. Extract this image to your home directory, then write
a script to do the following:
a) Load the image ffle using the imread command into an array 'a'. The imread
command works just like the load command, and will produce a 2D array of
numbers. Each element in the array represents the brightness of one pixel in the
image. The lower the number the darker the pixel, the higher the number the
brighter the pixel.
3b) Display the image using the command imshow(a), the image you see should
look like ffgure 2
c) Make two copies of the array 2D 'a', into variables 'L' (for light) and 'D' (for
dark).
d) The ffrst thing to do when working with a new array is to ffnd and store its
size. Calculate the size of the array 'a', using the size command and store the x
and y size in two variables x_len and y_len.
e) We now need to start writing our code to process the image. The ffrst step is
to write two nested for loops which will iterate over each pixel in the image. To
do this write one for loop that counts using x from 1 to the x_len. Now deffne a
second for loop inside the ffrst for loop which counts from 1 to y_len using y.
You have just made a nested for loop. There are more examples of how to do
nested loops in lecture 5. Now, by using the sprinf and disp commands within
the nested for loop, make the script display the following text for each pixel:
The brightness of pixel at ?,?? is ???.
where ? represents the x position, ?? represents the y position and ??? represents
the pixel brightness.
f) Now comment out the command printing each brightness value to the screen
(but don't delete them 每 otherwise you won't get the marks). We are now going
to make the image darker (less exposed). Within the nested for loops, multiply
each value stored at each position in array 'a' by 0.3 and store the result in the
same location in array 'D'. Now at the end of your script use imshow to display
the darker version of the image. If you issue the ffgure command before
imshow, matlab will display each ffgure in a new window. Compare the original
and less exposed images.
g) Now we are going to increase the exposure of the image by making it lighter.
Within the nested for loops, multiply each value stored, at each position in the
4
Figure 2: The natural history museum in London.array 'a' by 1.5 and store the result in the same location in array 'L'. Display the
result in a new ffgure.
h) Often it is important to know how long a piece of code takes to run. MATLAB
has two built in functions to help you ffnd out how long code takes to run. These
are tic and toc functions. They work a bit like a stop watch, the tic command
starts a stop watch and toc stops the stop watch. When toc is called the time
elapsed is also printed out. Try putting tic at the beginning of your program and
toc at the end of your program. How long does it take your program to run?
Write the result as a comment in your program.
i) Now comment out the nested for loop with the '%' character so that it no
longer runs (but don't delete it 每 otherwise you won't get the marks), and replace
its functionality with matrix multiplication commands. How long does your
program now take to run 每 why is this?
Question 6 (20% of total mark): This question is designed to help you
practice writing simple functions.
a) Write a script to prompt the user for the height, width and breadth of a box.
The computer should then use the disp command and the sprintf command to
write the text:
Your box has dimensions width=??, height=??, length=??
Where ?? are replaced with the information the user entered.
b) Deffne a function called box_area which accepts three variables and returns
the area of the box.
c) Deffne another function called box_volume, which accepts three variables and
returns the volume of the box.
d) Use these functions to print the text:
The box has the area of xxx and volume of xxx.
e) Edit the script to ask the user for the units used in the measurements, then
adjust the text to read
The box has the area of xxx units_for_area and volume of xxx units_for_volume.
Where units_for_area and units_for_volume are replaced by the correct units.
Hint: You have to use %s*%s*%s, and %s*%s in the sprintf command to print the
correct units.
f) Edit the script so that if the user enters the dimensions height=5, width=30,
length=30 the program prints to the screen 'It's a pizza box!'
5Question 7 (20% of total mark): 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.
Generating a 2D box
a) Define a 2D 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 sprintf and disp to
output the following text for each point in the 2D array:
'At the value x=??, y=?? the value of a=??'
Where ?? are replaced by the current x value, y value and corresponding value of
a(y,x)
e) Place the disp command and the sprintf within an if statement, so that the
text is only printed when value at a(y,x) is equal to 1. Change the line of text to
read:
'A gas particle exists at x=??, y=??'
where, ?? is replaced by the actual x and y coordinate of the gas particle.
Moving the gas particle
f) 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 if statement you wrote above, so
that if your code finds a gas particle, it will choose a random number between 0
and 4, store this number in the variable r. Depending upon the number chosen
we are going to move our gas particle up (r==0), down (r==1), right (r==2) or
6
Figure 3: Gas particles in a box.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 if-elseif-elseif statement which will print the following text using the
disp command, depending upon the value of r generated:
Value of r Text to print
0 Moving particle up
1 Moving particle down
2 Moving particle right
3 Moving particle left
h) The above if-elseif-elseif 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 ifelseif
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 disp commands
within the if-elseif-elseif 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=x_new-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.
i) The final job is to move the particle to the new coordinate we have generated.
Place these commands:
a(y,x)=0
a(y_new,x_new)=1
within the for loops but after the if-elseif-elseif 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.
-j) In order to make your particle continually move you need to place the two
nested for loops in a while 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 if
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)?
l) To stop the gas particles destroying themselves when they meet, wrap the
code that moves the gas particle:
a(y_new,x_new)=1
a(y,x)=0
in an if statement, which will only execute if a(y_new,x_new)==0.
m) Increase the size of your box to 100 by 100, using two for loops generate a
square of 20x20 gas particles in the center of the box. Let the simulation run
and watch the gas particles spread out in the box.
n) We are now going to simulate a liquid evaporating from a surface. Comment
out the for loops you wrote in part 'n' 每 but don't delete them or you will loose
the marks. Using two for loops generate a layer of gas particles at the bottom of
your box 20 units deep, which spans the entire box. Let the simulation run and
watch the particles evaporate.
8Removing gas particles from the simulation (tricky)
o) Set the value at 4,4 in your matrix to 5. Edit your code so that if your particle
moves on to a data point with a value of 5, the gas particle is deleted. This could
represent a hole in your box though which gas escapes.
p) The problem with this simulation is that once the particles reach the top of our
box they become trapped. In reality the particles would continue moving away
from the surface forever. To prevent the particles becoming trapped in the box,
use a single for loop to set the top edge of the box to the value of 5. When a
particle reaches the top of the box it will now be deleted thus simulating
particles escaping from the top of the box.
q) Now include the effect of gravity in the simulation, by making it more probable
that the particle will choose to move towards the bottom of the box than in any
other direction.
9
Figure 4: Simulating evaperation. Your code should
produce something that looks like this.
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代写dts207tc、sql编程语言代做
2024-12-25
cs209a代做、java程序设计代写
2024-12-25
cs305程序代做、代写python程序...
2024-12-25
代写csc1001、代做python设计程...
2024-12-24
代写practice test preparatio...
2024-12-24
代写bre2031 – environmental...
2024-12-24
代写ece5550: applied kalman ...
2024-12-24
代做conmgnt 7049 – measurem...
2024-12-24
代写ece3700j introduction to...
2024-12-24
代做adad9311 designing the e...
2024-12-24
代做comp5618 - applied cyber...
2024-12-24
代做ece5550: applied kalman ...
2024-12-24
代做cp1402 assignment - netw...
2024-12-24
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!