Computer Programming With MATLAB MM1CPM - Lecture

Transcription

University ofNottinghamComputer Programming with MATLABMM1CPM - Lecture 32D data arrays and advanced plottingDr. Roderick MacKenzieroderick.mackenzie@nottingham.ac.ukAutumn 2014www.facebook.com/mm1cpmReleased under

Overview In this lecture we will cover: Coursework Overview of last lecture Updating and editing 1D arrays 2D data 2D arrays Extracting data from 2D arrays Advanced plotting2Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Coursework: How to hand it in. Please hand in a zip file containing one .m file for eachquestion. The zip file should be named firstname surname studentID.zipQ1.mQ4.mQ7.mQ2.mQ5.mQ3.mQ6.mroderick mackenzie1234567.zipMoodleflow diagram.pdfRoderick MacKenzie3MM1CPM Computer Programming with MATLAB

Coursework: The perfect answer This wouldgive you100% forquestion 1 Note theoddcomment. We will mark your code by running the .m file. Don't include the screen shots of the plots or what MATLABprints out. Please include your e-mail address so we can give youfeedback.4Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Overview In this lecture we will cover: Coursework Overview of last lecture Updating and editing 1D arrays 2D data 2D arrays Extracting data from 2D arrays Advanced plotting5Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Recap: Complex numbers We learnt that in MATLAB complex numbers can berepresented by an i All mathematical operations that work with ordinarynumbers also work with complex numbers. a 7 i; b 8 8i;%define%define variable a%define%define variablevariable b b b 2 b b 2 c a b d a-b d a-b f b/c f b/c%raise%raise cc toto thethe powerpower of ivision6Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Recap: Complex numbers MATLAB can do very complicated multiplications for you: (3 4i)*(7 i)*(3 4i)*(7 i)*(3 4i)*(7 i)*(3 4i)*(7 i)*(3 4i)*(7 i)*(3 4i)*(7 i) enter ans 1.9361e9 2.5700e8i7Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Recap: Variables v.s. Arraysmark1 mark2 mark3 mark4 . mark10y 10We could define ten variables then take the average:72mark1 mark1 72mark1 72 enter enter mark2 40mark2 40 enter enter mark3 50mark3 50 enter enter mark4 80mark4 80 enter enter mark5 .mark5 . etc.etc. enter enter y (mark1 mark2 mark3 mark4 mark5 mark6 mark7 y (mark1 mark2 mark3 mark4 mark5 mark6 mark7 mark8 mark9 mark10)/10mark8 mark9 mark10)/10This looks like a lot of hard work.Roderick MacKenzie40mark250mark380mark4.90mark108MM1CPM Computer Programming with MATLAB

Recap: Arrays A good way to handle large amounts of data is by usingan array.7240508060Arrays – good for largedata sets (i.e. marks ofstudents or audio data):student marksAnd this is how to make an array in MATLAB, we usesquare brackets around a list of numbers: student marks student marks [[ 7272 4040 5050 8080 6060 ]][]9Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Recap: Defining arrays Defining arrays by hand time in seconds time in seconds [0[0 112233445566778899 1010 1111 1212 1313 14]14] Getting the computer to define arrays for you (less typing!): time in seconds time in seconds linspace(0,14,15)linspace(0,14,15)StartEnd Loading arrays from disk:Number ofpointsyy load('matlab music file.dat');load('matlab music file.dat');10Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Recap: Arrays - music to my earsVSound wave at 50 HzSinusoid at 50 Hz Sound data is just an array of numbers and we can getthe computer to play music, by sending an array to thesound card. x linspace(0,10000,10001)x linspace(0,10000,10001) y sin(x)y sin(x) sound(new data,44100)sound(new data,44100)11Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Overview In this lecture we will cover: Coursework Overview of last lecture Updating and editing 1D arrays 2D data 2D arrays Extracting data from 2D arrays Advanced plotting12Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Updating and editing 1D arraysVery often in Engineering you willhave defined an array to representphysical quantity: temperature temperature [[ 2020 3030 2020 9090 2020 ]]Imagine elements in the arrayrepresents the temperature fromfive sensors in a jet engine. The temperatures values the sensors measure will change sothe array will need to be updated: 2030209020Temperature data2030201000 20Updatedtemperature dataI'm now going to show you how to do this. Roderick MacKenzie13MM1CPM Computer Programming with MATLAB

Updating and editing 1D arrays –example 1 To update the array temperature temperature [[ 2020 3030 2020 9090 2020 ]] We simply type:1st 2nd 3rd 4th 5th temperature(4) 1000 temperature(4) 1000 In English this command means replace the 4thelement of array 'temperature' with the value 1000 This will give: temperature temperature [[ 2020 3030 2020 10001000 2020 ]]1st 2nd 3rd 4th 5thRoderick MacKenzie14MM1CPM Computer Programming with MATLAB

Updating and editing 1D arrays –example 2 To update the array temperature temperature [[ 2020 3030 2020 10001000 2020 ]] We simply type:1st 2nd 3rd 4th 5th temperature(2) 2000 temperature(2) 2000 This will give: temperature temperature [[ 2020 20002000 2020 10001000 2020 ]]1st 2nd 3rd 4th 5th Now we know how to update elements in arrays!15Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Reading values from arrays 1D Reading one value from an array is just the reverse process Imagine we have the array temperature temperature [[ 1818 1919 2020 2121 1010 2525 2020 3030 2222 2323 ]]1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th And I wanted to know the value of the 7th element, I wouldsimply type: temperature(7) temperature(7) enter enter 2020or the 4th element: temperature(4) temperature(4) enter enter 2121Roderick MacKenzie Now we can read andwrite individualelements to an array We will use this later inthe lecture16MM1CPM Computer Programming with MATLAB

Overview In this lecture we will cover: Coursework Overview of last lecture Updating and editing 1D arrays 2D data 2D arrays Extracting data from 2D arrays Advanced plotting17Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Limitations of 1D arrays. Until now we have only had arrays containing listsof numbers. age of students age of students [[ 1919 2020 2121 1919 2020 2121 1818 ]] stock market data stock market data [[ 50005000 50015001 49994999 ]] temperature values temperature values [[ 3030 3131 3232 3333 3434 3535 3636 3737 3838 37373636 3535 3535 35]35] price of gold price of gold [[ 2727 27.127.1 2828 2929 2727 21.121.1 2727 27.127.1 2828 29292727 21.121.1 2727 27.127.1 2828 2929 2727 21.121.1 2727 27.127.1 2828 2929 2727 21.121.1 ]] time in seconds time in seconds [0[0 336699 1212 1515 1818 2121 2424 27]27] However very often, the data you are interested inis not list of numbers.Roderick MacKenzie18MM1CPM Computer Programming with MATLAB

An example of 2D data3000 elements (y)193000 elements (x) We represent this type ofdata using a grid ofnumbers otherwiseknown as a 2D array.Roderick 98441238822211188111111112221111112211111111Black 0white 10MM1CPM Computer Programming with MATLAB

Examples of 2D data sets2D images202D map of predicted temperaturechanges due to global warmingRoderick MacKenzieWeather radar data (NASA)MM1CPM Computer Programming with MATLAB

Making 2D arrays in MATLAB is easy.If we had the 1D array defined as numbers numbers [[ 998877665544332211 ]]We can turn it in to a 2D array by simply inserting awhere we want a 'new line'.; numbers numbers [[ 998877 ;; 665544 ;; 332211 ]]2 13459 8 7 6;Roderick MacKenzie;9 8 76 5 43 2 121MM1CPM Computer Programming with MATLAB

More examples of 2D arrays; a [a [ 111111 ;; 222222 ;; 333333 ]]111222333semicolonAll you need to remember is thesemicolon means start a newline in the array.723456789 10 1112114 5 16 a [a [ 77223344 ;; 55667788 ;; 99 1010 1111 1212 ;; 11 1414 55 16]16]22Roderick MacKenzieBut that needed a lot of typing.MM1CPM Computer Programming with MATLAB

Making arrays with less typing: zeros The 'zeros' command will make a 2D array full of zeros. a zeros(4,4) a zeros(4,4)a zeros(4,4) a zeros(2,3)a zeros(2,3)aa aa 00000000000000000000000000000000000000000000 The numbers in the brackets specify the x an y size of thearray. This is a very common command - we will use it later.23Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Making arrays with less typing: rand The 'rand' command will make a 2D array full of randomnumbers between 0 and 1: a rand(2,2) a rand(2,2)a rand(2,2) a rand(2,3)a rand(2,3)aa aa 440.03440.43870.4387 The numbers in the brackets specify the x and y size of thearray.Youtube example This is all very interesting but let's look at some real data.24Roderick MacKenzieMM1CPM Computer Programming with MATLAB

The load command for 2D data Last lecture we used the load command to load a list ofnumbers into a 1D array. y y loadload ('my data.dat')('my data.dat')y y [10.0[10.0 20.120.1 10.410.4 40.540.5 .50.650.6 80.980.9 10.110.1 20.120.1 ]]25Roderick MacKenzieMM1CPM Computer Programming with MATLAB

The load command for 2D data The load command also works just the same for 2Ddata: y y loadload ('my data.dat')('my data.dat')y y 1010 2020 30304040 5050 60607070 8080 909010 20 3040 50 6070 80 90 Let's look at some real world 2D arrays.Roderick MacKenzie26MM1CPM Computer Programming with MATLAB

Let's look at some real 2D data. The Mars Global Surveyor has beenbusy mapping the surface of marssince its launch in 1996. It has produced a 2D heightprofile map of the martiansurface.27Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Mars height dataHeight dataon 2D mapHeight dataon 2D map Let's have a look at this data.Roderick MacKenzieWe can download a filecontaining this datafrom NASA:mars height data.dat28MM1CPM Computer Programming with MATLAB

Finding the size of the data The data is stored as a 2D grid of numbers The numbers represent height in km.501mars height 111122222222111111222222211111111111111024 To load this data into a 2D array we simply type:data .data .Roderick MacKenzie29MM1CPM Computer Programming with MATLAB

Finding the size of the data The data is stored as a 2D grid of numbers The numbers represent height in km.501mars height 111122222222111111222222211111111111111024 To load this data into a 2D array we simply type: data data load('mars height data.dat');load('mars height data.dat');Roderick MacKenzie30MM1CPM Computer Programming with MATLAB

Plotting 2D data using the surf command The surf command performs a surface plot of a 2Darray. data data load('mars height data.dat');load('mars height data.dat');data 6.32044data 6.32044 6.408846.40884 6.49724.6.49724.6.232046.23204 6.320446.32044 6.40884.6.40884.6.762436.76243 6.762436.76243 6.85083.6.85083.6.939236.93923 7.027627.02762 7.11602.7.11602.6.674036.67403 6.674036.67403 6.76243.6.76243. surf(data) surf(data) Let's have a look at the result.31Roderick MacKenzieMM1CPM Computer Programming with MATLAB

The surf command501 Use this tool to rotate andview the plot. We can also view thepicture from a birds eyeview by using the rotatetool.mars height data.dat00100032Roderick CPM Computer Programming with MATLAB

The surf command A birds eye view of our data.Height dataon 2D map33Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Add a color bar scale Adding a color bar scale8 data data load('mars data.dat');load('mars data.dat'); surf(data) surf(data) colorbar colorbar The height data is in km. The numbers on the x/y axisrepresent the position of thedata in the array (notdistance). How deep is the crater? How high are the mountainsin the south?Roderick MacKenzie-8Youtube34MM1CPM Computer Programming with MATLAB

Finding the size of the data data data load('mars height data.dat');load('mars height data.dat'); size(data) size(data)ansans 501501501 Up to this point we have known howbig our arrays are because we madethem or I have told you the size. For data sets made by other peoplethe first thing to do is to find out howbig the data set is. We can do this using the sizecommand size command:102410241024Roderick MacKenzie35MM1CPM Computer Programming with MATLAB

Overview In this lecture we will cover: Coursework Overview of last lecture Updating and editing 1D arrays 2D data 2D arrays Extracting data from 2D arrays Advanced plotting36Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Coordinates in 2D arrays What coordinate is the crater at? Just treat it like a map. x ?, y ?50040030020010000Roderick MacKenzie200400600800100037MM1CPM Computer Programming with MATLAB

Coordinates in 2D arrays The coordinate is x 720 y 12040030020010000Roderick MacKenzie200400600800100038MM1CPM Computer Programming with MATLAB

Extracting one element from a 2D array. To extract the depth of the crater from the array wejust type: data data load('mars data.dat');load('mars data.dat'); data(120,750) data(120,750)%array name(y,x)%array name(y,x)ans -7.4074ans -7.4074 This works exactly the same as extracting data from a1D array 7km is a deep crater!!39Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Extracting small 2D arrays from big 2D arrays. Imagine we wanted to just zoom in on one feature inthe MAP. We could do it by first extracting the region of thearray that we were interested in. then plotting it.345111312111092233412332415small arraybig array40Roderick MacKenzie40MM1CPM Computer Programming with MATLAB

Extracting small 2D arrays from big 2D arrays.We can do it like this: small array small array big array(9:13,3:5)big array(9:13,3:5)345 9:13 means from 9 to 13 along y 3:5 means from 3 to 5 along x111312111092233412332415small arraybig array41Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Let's look at a real example Between which coordinates does the mountain lie? x ?:?, and y ?:?50040030020010000Roderick MacKenzie200400600800100042MM1CPM Computer Programming with MATLAB

Let's have a closer look at this mountain The mountain lies between x 150:250 andy 300:450 data data load('mars data.dat');load('mars data.dat'); mountain data(300:450,150:250) mountain data(300:450,150:250) surf(mountain) surf(mountain) We can see that there isover 14 km between it'speak and the bottom(!).YoutubeRoderick MacKenzie43MM1CPM Computer Programming with MATLAB

Extracting 1D arrays from 2D arrays. Imagine we wantedto plot the heightprofile from thenorth of marsthrough the craterto the south pole.North pole500400300200 First we would needto find out wherethe crater lies. It lies on x 700.100002004006008001000South pole To extract this strip of data we would type: strip data(1:501,700) strip data(1:501,700)Roderick MacKenzie44MM1CPM Computer Programming with MATLAB

Extracting strips of data from arrays Here is the whole program: data data load('mars data.dat');load('mars data.dat'); strip data(1:501,700) strip data(1:501,700) plot(strip) plot(strip)North 0South pole What's wrong with this graph (there is more than45one thing)?Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Overview In this lecture we will cover: Coursework Overview of last lecture Updating and editing 1D arrays 2D data 2D arrays Extracting data from 2D arrays Advanced plotting46Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Generating an x-axis scale10670 km80-8020040060010672 km I need to generate a scale for the x axis. Relating position in the array to actual distance in km. So this is what we do.47Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Generating an x-axis scale Here is our 1D array of height data It is 501 points longy1.1 1.2 1.1 1.31st2nd3rd4th0.80.55th499th 500th 501st1.1 1.1 We need to generate a second array acting as an xscale. relating each point to an actual position in km.48Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Generating an x-axis scale Here is our 1D array of height data It is 501 points longyx1.1 1.2 1.1 1.30.80.51.1 1.11st2nd3rd4th5th499th 500th 501st02040608010630 10650 10670 New array containing actual position of data point inkm. What command would we use to make this new array?Roderick MacKenzie49MM1CPM Computer Programming with MATLAB

Generating an x-axis Here is the code data data load('mars data.dat');load('mars data.dat'); strip data(1:501,700) strip data(1:501,700) x linspace(0,10670,501) x linspace(0,10670,501) plot(x,strip) plot(x,strip)%load%load thethe datadata%extract%extract aa 1D1D stripstrip%make%make ourour scalescale%plot%plot xx againstagainst yy50Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Better51Roderick MacKenzie But we are still missing x-label, y-label and a title.MM1CPM Computer Programming with MATLAB

Adding labels to graphs data data load('mars data.dat');load('mars data.dat'); strip data(1:501,700) strip data(1:501,700) x linspace(0,10672,501) x linspace(0,10672,501) plot(x,strip) plot(x,strip) xlabel('Position xlabel('Position fromfrom SouthSouth polepole (km)')(km)') ylabel('Height ylabel('Height ofof martianmartian surfacesurface (km)')(km)') title title ('Line('Line plotplot ofof surfacesurface ofof mars')mars')52Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Summary In this lecture we will cover: Coursework Overview of last lecture Updating and editing 1D arrays 2D data 2D arrays Extracting data from 2D arrays Advanced plotting53Roderick MacKenzieMM1CPM Computer Programming with MATLAB

Roderick MacKenzie MM1CPM Computer Programming with MATLAB Making arrays with less typing: rand The 'rand' command will make a 2D array full of random numbers between 0 and 1: a rand(2,2) 24 a rand(2,2) a 0.7577 0.3922 0.7431 0.