0% found this document useful (0 votes)
23 views5 pages

Project

how matlab use

Uploaded by

Roopak Behera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Project

how matlab use

Uploaded by

Roopak Behera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MIT OpenCourseWare

http://ocw.mit.edu
Introduction to MATLAB
IAP 2008
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
IntroductiontoMatlabIdeasforIAPProject
January 2008
This is not a well-dened list of projects; they are mostly meant as starting points and ideas. You
can take any of these projects and make of it as much or as little as you feel like. However, you
came to learn a new skill, and practice makes perfect!
1 Queens on a chess board
Therstpartwasalreadyahomeworkproblem: Findaset-upof8queensonachess-boardsothat
no queen threatens another.
Now we can extend this a little:
Howdoesyoursolutionscale? Thatis,ifIaskyoutosolvenqueensonannboard,will
it be possible to do with your current solution or will you need to write a new one? (consider
writingoneforwhichthisisveryeasy). Howlongdoesyourprogramtakeasafunctionofn?
Is it linear tn? Polinomial, tn

(if so, what is ?), or exponential, t=e


an
(if so, what
is a?)? These are important questions to ask when you are coding a program that needs to
work on large data. Can you think of ways to make your code faster? Use less memory?
Find how many solutions (arrangements of queens that is) are there? You will probably have
tochangeyoucodetondthisout. Canyoualsocountwithoutincludingrotationsandmirror
symmetries?
Howmanyplacementsofnqueensonammboardarethere? Canyoundaniceformula?
how about a few of the rst examples? How about when m=n?
Whataboutotherpieces? Isitreallythateasyforknightsaswasdiscussedonthenewsgroup
(that 32 is the largest possible number) or is there a way to put 33? More? How many (truly
dierent) ways are there to put 32 knights on the board?
2 Make your own Autostereogram
Ever seen these 3D posters that work when you stare at them for a while? Did you ever care to
know how they work or wanted to make one yourself? Heres your chance!
1
First, here is the theory behind autostereograms (very non-scientic). Our mind gures out the
depthofobjectsintheworldbycomparingtheanglesatwhichoureyesseethesameobjectthrough
our eyes. We can fool this mechanism by creating a pattern that is periodic in the horizontal
direction, and then looking at it as if it is closer or further away than it actually is. By staring
or looking cross-eyed at the pattern, we make each eye focus on a dierent copy of the repetitive
pattern. However, since the patterns is repetitive the brain thinks that it is looking at object that
is located at a dierent depth. To create an illusion of a 3D object we deviate from the repetitive
patterninthefollowingprecisefashion: Eachdotinacopyismovedhorizonallywithrespecttothe
matching dot (in the repetitive pattern). The amount in which the dot is moved is proportional to
the desired depth of the illusion at the corresponding point.
In terms of coding you will need to gure out how to:
Create a random pattern of dots (with about 50% ll) in a narrow vertical rectangle
Createaverticalstripoftheautostereogram,copythepreviousdotpatternandmovethedots
inproportiontothedesireddepthatthatpoint. Rememberthatyouneedtokeepcopingthe
previous strip and not the original one
Plotyourpointstocreatethestereogram. Itisusefultoplottwoadditionallargepointsbelow
your autostereogram to indicate the spacing of repetition. This will help when trying to view
the autostereogram
Your input could be a matrix, a picture, or a mathematical formula. This doesnt mean that your
programshouldacceptalloftheseasinput,butratherthatyouconsiderthepossibilitiesandthink
about converting the input into a map, (x, y) z.
Try your program rst on a simple input that you know more or less what the answer should look
like, for example a simple square that is lifted from the background. Then try it on other simple
geometric objects, like pyramids and spheres.
Have a 3D picture in mind and create it.
Print the result and post it on your fridge.
Extra things to think about if you want:
Ifyourheightfunctionhasalargejump,youwillndthatabigareawithnopointsiscreated
andthiswillhurtthequalityoftheresult. toavoidthisyoucantrytolocatebigemptyspaces
at each repetition and ll it up with random points. These extra points should be copied and
moved with the others.
Big jumps in the height function can also cause points to overlap creating dense blobs
of points. This, too can hurt the quality of the autostereogram. Removing points that got
overlapped(notthepointsdoingtheoverlapping)isanotherwaytoimporoveyourautostereo-
gram.
2
3 Spline your name
Writeyourname
1
incursiveongraphpaper(youllneedtowriteitratherlarge). Foreachletterin
your name select 46 points in the letter that best dene the letter (letters with extra strokes, t,
i, j and x, require extra care). Write down, in order, the x and y coordinates of each of the points.
Using a spline interpolation, reconstruct your name from the measured coordinates. Create a plot
of the reconstruction. Try to do as much as possible on your own. Youll need to do the following
(each can be done with some internal matlab command):
Find the interpolating spline ploynomials
Evaluate the polynomials for other points
Plot the resulting (x,y) curve
Rememberthatsuchacurveisrarelyafunctiony(x)butrathereachofthecoordinatesisafunction
of a third hidden parameter, usually marked t. It is with respect to this paramter that x(t) and
y(t)arefound,buttheny(t)shouldbeplottedversusx(t). Themoreofthisyoudoonyourown
the better. If you dont know the equations for the spline polynomials, you can probably nd it
online.
4 Random motion (or drunken walk)
Compute and plot the paths of a set of many (about 1000) random walkers which are conned by
a pair of barriers at +B and B (we assume that they all start at x= 0
A random walk is computed by repeatedly performing the calculation
x
j+1
=x
j
+s (1)
wheresisanumberdrawnfromthestandardnormaldistribution(randninMATLAB).Forexample,
one walker taking N steps could be handled by the code fragment
x(1) = 0;
for j = 1:N
x(j+1) = x(j) + randn(1,1);
end
There are three ways in which the barriers can act:
a. Reecting- Inthiscase,whenthenewpositionisoutsidethewalls,thewalkerisbouncedback
by the amount that it exceeded the barrier. That is, when x
j+1
> B, x
j+1
= B |Bx
j+1
|
whenx
j+1
<(B),x
j+1
= (B) +|(B)x
j+1
|. Ifyouplotthepaths,youshouldnotseeany
positions that are beyond |B| units from the origin.
1
If you have a name for which the following is annoying or for any other reason, you may use a dierent name or
a drawing you make with one long smooth stroke
3
b. Absorbing- Inthiscase,ifawalkerhitsorexceedsthewallpositions,itisabsorbedandthewalk
ends. Forthiscase,itcanbeofinteresttodeterminethemeanlifetimeofawalker(i.e.,themean
and distribution of the number of steps the average walker will take before being absorbed).
c. Partially absorbing - This case is a combination of the previous two cases. When a walker
encounters a wall, a coin is ipped to see if the walker relfects or is absorbed. Assuming a
probabilityp,(0< p <1)forreection,thepseudo-codefragmentthatfollowsusestheMATLAB
uniform random-number generator to make the reect/absorb decision:
if rand < p
reflect
else
absorb
end
(of course, you will have to supply the code that does the reection or absorbtion)
What do you do with all the walks that you generate? Compute statistics, of course. Answering
questions like:
What is the average position of the walkers as a function of time?
What is the standard deviation of the position of the walkers as a function of time?
Does the absorbing or reecting character inuence these summaries?
Fortheabsorbing/partial-reectioncase,aplotofthenumberofsurvivingwalkersasafunction
of step number/time is a very interesting thing. It is useful, informative and interesting,
particularly if graphically displayed. What about the density of walkers? Can you plot it?
5 Chaos: x
n
+ 1 =rx(x1)
You already found this simple formula in a homework assigment. Nows the time to take this
homework assigment to the next level.
Findhowtomaketheplothavesmalldotswithoutplottingeachdata-pointwithasingleplot
command.
For each r the iterates converge on a nite set of values, x. Dont plot more points than you
need. Makesureyouiterateenoughforconvergance(theoriginalvalueof1000wasonlyarule
of thumb).
Can you allow the user to zoom in on your plot? once asked to see a region smaller than
(0, 4)(0, 1)youshouldprobablyincreasethedensityofyourrmeasurements,andconne
the plotting of the points so that only the requested x

s are plotted.
Can you nd out how to redraw the gure whenever the zoom tool is used?
4

You might also like